Как сделать отправку формы постоянной в моем проекте django?
в моем проекте django models.py =
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils import timezone
class User(AbstractUser):
profile_pic = models.ImageField(upload_to='profile_pic/')
bio = models.TextField(max_length=160, blank=True, null=True)
cover = models.ImageField(upload_to='covers/', blank=True)
whatsapp = models.CharField(max_length=25, blank=True, null=True)
def __str__(self):
return self.username
def serialize(self):
return {
'id': self.id,
"username": self.username,
"profile_pic": self.profile_pic.url,
"first_name": self.first_name,
"last_name": self.last_name
}
class Post(models.Model):
creater = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
date_created = models.DateTimeField(default=timezone.now)
content_text = models.TextField(max_length=140, blank=True)
content_image = models.ImageField(upload_to='posts/', blank=True)
likers = models.ManyToManyField(User,blank=True , related_name='likes')
savers = models.ManyToManyField(User,blank=True , related_name='saved')
comment_count = models.IntegerField(default=0)
def __str__(self):
return f"Post ID: {self.id} (creater: {self.creater})"
def img_url(self):
return self.content_image.url
def append(self, name, value):
self.name = value
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
commenter = models.ForeignKey(User, on_delete=models.CASCADE, related_name='commenters')
comment_content = models.TextField(max_length=90)
comment_time = models.DateTimeField(default=timezone.now)
def __str__(self):
return f"Post: {self.post} | Commenter: {self.commenter}"
def serialize(self):
return {
"id": self.id,
"commenter": self.commenter.serialize(),
"body": self.comment_content,
"timestamp": self.comment_time.strftime("%b %d %Y, %I:%M %p")
}
class Follower(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='followers')
followers = models.ManyToManyField(User, blank=True, related_name='following')
def __str__(self):
return f"User: {self.user}"
views.py =
urls.py =
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
path("home", views.index, name="index"),
path("n/login", views.login_view, name="login"),
path("n/logout", views.logout_view, name="logout"),
path("n/register", views.register, name="register"),
path("<str:username>", views.profile, name='profile'),
path("n/following", views.following, name='following'),
path("n/saved", views.saved, name="saved"),
path("n/createpost", views.create_post, name="createpost"),
path("n/post/<int:id>/like", views.like_post, name="likepost"),
path("n/post/<int:id>/unlike", views.unlike_post, name="unlikepost"),
path("n/post/<int:id>/save", views.save_post, name="savepost"),
path("n/post/<int:id>/unsave", views.unsave_post, name="unsavepost"),
path("n/post/<int:post_id>/comments", views.comment, name="comments"),
path("n/post/<int:post_id>/write_comment",views.comment, name="writecomment"),
path("n/post/<int:post_id>/delete", views.delete_post, name="deletepost"),
path("<str:username>/follow", views.follow, name="followuser"),
path("<str:username>/unfollow", views.unfollow, name="unfollowuser"),
path("n/post/<int:post_id>/edit", views.edit_post, name="editpost")
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
html-файл находится в папке с именем network, которая находится в папке templates profile.html=
сосредоточьтесь на этой части кода
<div class="mbs-pec" id="mbs-pec" >
<form method="POST" class="mbs-pecc" style="display: block;" enctype="multipart/form-data">
{% csrf_token %}
<div id="mbs-ec" class="mbs-ec">
<div id="mbs-ecc" class="mbs-ecc1">
<p>cover image</p>
<div class="c-image" style="background-image: url(/media/{{username.cover}})"></div>
<input type="file" name="cover">
</div>
<div id="mbs-ecc" class="mbs-ecc2">
<p>profile image</p>
<div class="p-image" style="background-image: url(/media/{{username.profile_pic}})"></div>
<input type="file" name="profile_pic">
</div>
</div>
<div id="mbs-pecb" class="mbs-pecb a">
<p>First Name</p>
<input type="text" name="first_name" value="{{username.first_name}}">
</div>
<div id="mbs-pecb" class="mbs-pecb a">
<p>Last Name</p>
<input type="text" name="last_name" value="{{username.last_name}}">
</div>
<div id="mbs-pecb" class="mbs-pecb a">
<p>Username</p>
<input type="text" name="username" value="{{username.username}}">
</div>
<div id="mbs-pecb" class="mbs-pecb a">
<p>Bio</p>
<input type="text" name="bio" value="{{username.bio}}">
</div>
<div id="mbs-pecb" class="mbs-pecb a">
<p>Email</p>
<input type="email" name="email a" value="{{username.email}}">
</div>
<div id="mbs-pecb" class="mbs-pecb a">
<p>Whatsapp</p>
<input type="text" name="whatsapp" value="{{username.whatsapp}}">
</div>
<div id="mbs-pecb" class="mbs-pecb " >
<button class="mbs-done btn btn-outline-success" id="mbs-done">Done</button>
<button class="mbs-close btn btn-outline-success" id="mbs-close">Close</button>
</div>
</form>
</div>
здесь отображается имя пользователя {{username.first_name}}
фамилия {{username.last_name}}
имя пользователя {{username.username}}
биография {{username.bio}}
email {{username.email}}
whatsapp {{username.whatsapp}}
фотография профиля <div class="p-image" style="background-image: url(/media/{{username.profile_pic}})"></div> and cover picture in <div class="c-image" style="background-image: url(/media/{{username.cover}})"></div>
теперь я хочу, чтобы при обновлении информации в имени, фамилии, имени пользователя, биографии, email, whatsapp и нажатии на кнопку "Done" Done она изменила информацию навсегда.
Не знаю, правильно ли я понял ваш вопрос, но, по-моему, то, что вам нужно, описано здесь: https://docs.djangoproject.com/en/5.0/topics/forms/