Когда я нажимаю кнопку редактирования профиля, я получаю ошибку значения [duplicate]
Пытаюсь сделать страницу редактирования, где можно загрузить картинку профиля и добавить различные вещи в свой профиль, но когда я нажимаю на кнопку редактирования профиля, то получаю "ValueError at /profile/ Атрибут 'profile_image' не имеет связанного с ним файла." models.py
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
full_name = models.CharField(max_length=50, null= True)
designation = models.CharField(max_length=100, null= True)
profile_image = models.ImageField(null=True, blank = True, upload_to="static/images/")
profile_summary = models.TextField(max_length=300, null= True)
city = models.CharField(max_length=100, null= True)
state = models.CharField(max_length=100, null= True)
county = models.CharField(max_length=100, null= True)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
urls.py
from .views import HomeView, UserProfile, register, user_login, user_logout
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path("", HomeView.as_view(),name="home-page"),
path("profile/", UserProfile.as_view(), name="user-profile"),
path("register/", register, name="register"),
path("login/", user_login, name="login"),
path("logout/", user_logout, name="logout"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
views.py
profile.html
{% load static %}
{% block content %}
<link rel="stylesheet" href="{% static 'css/profile.css' %}">
<div class="profile-page">
<div class="profile-container">
<div class="profile-container-pic">
{% if request.user.profile.profile_image %}
<img src="{{ request.user.profile.profile_image.url }}" alt="Profile Image">
{% else %}
<img src="{% static 'images/mypic.png' %}" alt="Default Image">
{% endif %}
</div>
<div class="profile-details">
<h3>{{ request.user.profile.full_name }}</h3>
<hr>
<div class="text">
<div class="email">
<img src="{% static 'images/email.png' %}" alt="">
<h5>{{ request.user.email }}</h5>
</div>
<div class="home">
<img src="{% static 'images/house.png' %}" alt="">
<h5>{{ request.user.profile.city }}, {{ request.user.profile.state }}, {{ request.user.profile.country }}</h5>
</div>
</div>
</div>
</div>
</div>
<div class="profile-form">
<form action="#" method="post" enctype="multipart/form-data">
{% csrf_token %}
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="{{ form_data.name }}" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" value="{{ form_data.email }}" required>
<label for="profile_image">Profile Image:</label>
<input type="file" id="profile_image" name="profile_image">
<label for="profile_summary">Profile Summary:</label>
<textarea id="profile_summary" name="profile_summary" rows="4" required>{{ form_data.profile_summary }}</textarea>
<label for="city">City:</label>
<input type="text" id="city" name="city" value="{{ form_data.city }}" required>
<label for="state">State:</label>
<input type="text" id="state" name="state" value="{{ form_data.state }}" required>
<label for="country">Country:</label>
<input type="text" id="country" name="country" value="{{ form_data.country }}" required>
<button type="submit">Submit</button>
</form>
</div>
{% endblock content %}
Планируется, что страница редактирования позволит вам выбрать фотографию профиля, но вместо этого будет выбрасываться ошибка ValueError.