Я бы хотел, чтобы можно было обновить поле portfolioie_site и profile_image, поэтому я создал ProfileUpdateForm, но у меня возникают проблемы с сохранением входных данных
Я хотел бы получить помощь, я понятия не имею, чтоЯ делаю. :)
Обновить!
СЕЙЧАС: Таким образом, я могу обновить поле url, но не поле изображения
Я обновил код и добавил печать, чтобы более четко видеть происходящее. Вот что я сейчас получаю в командной строке:
Командная строка
GET profile.html
[17/Oct/2019 11:12:51] "GET /accounts/profile/ HTTP/1.1" 200 1600
POST sending you back HOME
[17/Oct/2019 11:12:59] "POST /accounts/profile/ HTTP/1.1" 302 0
[17/Oct/2019 11:12:59] "GET / HTTP/1.1" 200 1100
Models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
portfolio_site = models.URLField(blank=True)
profile_image = models.ImageField(upload_to='swap_me/profile_image',blank=True)
def __str__(self):
return self.user.username
Forms.py
class ProfileUpdateForm(forms.ModelForm):
class Meta():
model = Profile
fields = ("portfolio_site","profile_image")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["portfolio_site"].label = "Site"
self.fields["profile_image"].label = "Image"
Views.py
@login_required
def profile_update(request):
user = request.user
form = ProfileUpdateForm(request.POST or None, instance=user.profile)
if request.method == 'POST':
if form.is_valid():
form.save()
print("POST sending you back HOME")
return HttpResponseRedirect(reverse('home'))
else:
print("error")
else:
print("GET profile.html")
context = {
"profile_updateform":form,
}
return render(request, 'profile.html', context)
Profile.html
{% extends "base.html" %}
{% load staticfiles %}
{% block body_block %}
{% if user.is_authenticated %}
<form enctype="multipart/form-data" method="POST">
{% csrf_token %}
{{ profile_updateform.as_p }}
<input type="submit" name="" value="Update">
</form>
{% else %}
<h1>You need to login!</h1>
{% endif %}
{% endblock %}