在Django上编辑用户照片,需要通过请求.POST+request.FILES在一起



我有点困在这里,编码新手,从头开始我的第一个Django项目。看了几个答案,尝试了几个方法,但仍然不起作用。我正在尝试允许用户编辑他们的照片。我想好了如何允许他们在注册中上传它,我知道它涉及"request.FILES",但我不知道如何将其包含在编辑配置文件的表单值中。这是我得到的错误

AttributeError at /update_account/10/
'Profile' object has no attribute 'get'
Request Method: POST
Request URL:    http://localhost:8000/update_account/10/
Django Version: 3.0
Exception Type: AttributeError
Exception Value:    
'Profile' object has no attribute 'get'
Exception Location: /Users/papichulo/Desktop/DatingApp/11_env/lib/python3.7/site-packages/django/utils/functional.py in inner, line 225
Python Executable:  /Users/papichulo/Desktop/DatingApp/11_env/bin/python
Python Version: 3.7.3
Python Path:    
['/Users/papichulo/Desktop/DatingApp',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
'/Users/papichulo/Desktop/DatingApp/11_env/lib/python3.7/site-packages']
Server time:    Fri, 13 Mar 2020 04:30:40 +0000

这是我的update_account视图:

def update_account(request, profile_id):
#Edit an existing profile 
profile = request.user

if request.method != 'POST':
#Initial request; prefil form with current entry
update_form = ProfileUpdateForm(instance=profile)
else:
#POST data submitted;process data. 
update_form = ProfileUpdateForm(profile, request.POST, request.FILES)
if update_form.is_valid():
update_form.save()
return HttpResponseRedirect(reverse('dating_app:profile', args=[profile.id]))
context = {'profile' : profile, 'update_form' : update_form}
return render(request, 'dating_app/update.html', context)

这是我的表单.py,其中包括用于注册和更新的表单:

from django import forms
from django.contrib.auth.forms import UserCreationForm
from dating_app.models import Profile


class RegistrationForm(UserCreationForm):

class Meta:
model = Profile 
fields = ("username","email","description","photo","password1","password2")


class ProfileUpdateForm(forms.ModelForm):
class Meta:
model = Profile 
fields = ('username','description','photo')

def clean_username(self):
user = self.user
if self.is_valid():
username = self.cleaned_data['username']
#makes sure no two users have same username
try:
profile = Profile.objects.exclude(pk=self.instance.pk).get(username=username)
except Profile.DoesNotExist:
return username
raise forms.ValidationError('Username "%s" is already in use. Please pick another username!' % profile.username)
def clean_description(self):
if self.is_valid():
#doesn't matter if descriptions are the same
description = self.cleaned_data['description']
return description

def clean_photo(self):
if self.is_valid():
photo = self.cleaned_data['photo']
return photo

并更新.html:

{% extends "dating_app/base.html" %}
{% block content %}
<br>
<h1>Update Profile</h1>

<form action="{% url 'dating_app:update_account' user.id %}" method='post' enctype="multipart/form-data">
{% csrf_token %}
{% for field in update_form %}
<p>
{{field.label_tag}}
{{field}}
{% if field.help_text %}
<small style="color:grey;">{{field.help_text}}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red;">{{error}}"</p>
{% endfor %}
</p>
{% endfor %}
<button type="submit">Save Changes</button>
</form>

{% endblock content %}

提前感谢!!

views.py

def update_account(request, profile_id):
#Edit an existing profile 
profile = request.user

if request.method != 'POST':
#Initial request; prefil form with current entry
update_form = ProfileUpdateForm(instance=profile)
else:
#POST data submitted;process data. 
update_form = ProfileUpdateForm(instance = profile,data= request.POST, files= request.FILES)
if update_form.is_valid():
update_form.save()
return HttpResponseRedirect(reverse('dating_app:profile', args=[profile.id]))
context = {'profile' : profile, 'update_form' : update_form}
return render(request, 'dating_app/update.html', context)

最新更新