не могу загрузить изображение в профиль пользователя. Код прилагаю ниже.
models.py
from django.db import models
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, related_name='profile')
avatar = models.ImageField(User, upload_to='media/', default='no_image.jpg', null=True, blank=True)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
forms.py
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('user', 'avatar')
views.py
<b>def avatar_img(request):
profile = request.user
form = ProfileForm(instance=request.user)
if request.method == 'POST':
form = ProfileForm(request.POST, request.FILES, instance=profile)
if form.is_valid():
form.save()
context = {'form': form}
return render(request, 'user.html', context)</b>
html.py
<img src="{{ request.user.profile.url }}" class="avatar">
<form method="POST" action="{% url 'user_profile:avatar' %}" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<input class="btn btn-primary" type="submit">
</form>
urls.py
path("avatar_upload", views.avatar_img, name="avatar"),