Ich habe einige der genannten Möglichkeiten ausprobiert und so mein Problem gelöst:
#forms.py
class EditProfileForm(forms.ModelForm):
first_name = forms.CharField(label='First Name',
widget=forms.TextInput(
attrs={'class': 'form-control'}),
required=False)
last_name = forms.CharField(label='Last Name',
widget=forms.TextInput(
attrs={'class': 'form-control'}),
required=False)
# username = forms.CharField(widget=forms.TextInput(
# attrs={'class': 'form-control'}),
# required=True)
address = forms.CharField(max_length=255, widget=forms.TextInput(
attrs={'class': 'form-control'}),
required=False)
phoneNumber = forms.CharField(max_length=11,
widget=forms.TextInput(
attrs={'class': 'form-control'}),
required=False)
photo = forms.ImageField(label='Change Profile Image', required=False)
class Meta:
model = User
fields = ['photo', 'first_name', 'last_name', 'phoneNumber', 'address']
# 'username',
#views.py
def edit_user_profile(request, username):
user = request.user
username = User.objects.get(username=username)
user_extended_photo = UserExtended.objects.get(user=user.id)
form = EditProfileForm(request.POST or None, request.FILES, instance=user)
user_extended = UserExtended.objects.get(user=user)
if request.method == 'POST':
if form.is_valid():
# photo = UserExtended(photo=request.FILES['photo'] or None, )
user.first_name = request.POST['first_name']
user.last_name = request.POST['last_name']
user_extended.address = request.POST['address']
user_extended.phoneNumber = request.POST['phoneNumber']
user_extended.photo = form.cleaned_data["photo"]
# username = request.POST['username']
user_extended.save()
user.save()
context = {
'form': form,
'username': username,
'user_extended_photo': user_extended_photo,
}
return render(request, 'accounts/profile_updated.html', context)
else:
photo = user_extended.photo
first_name = user.first_name
last_name = user.last_name
address = user_extended.address
phoneNumber = user_extended.phoneNumber
form = EditProfileForm(
initial={'first_name': first_name, 'last_name': last_name,
'address': address, 'phoneNumber': phoneNumber,
'photo': photo})
context = {
'form': form,
'username': username,
'user_extended_photo': user_extended_photo,
}
return render_to_response('accounts/edit_profile.html', context,
context_instance=RequestContext(request))
#edit_profile.html
<form action="/accounts/{{ user.username }}/edit_profile/" method="post" enctype='multipart/form-data'>
{% csrf_token %}
<div class="col-md-6">
<div class="form-group">
{{ form.as_p }}
</div>
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<button type="submit" value="Update Profile" class="btn btn-info btn-fill pull-right">Update Profile</button>
<div class="clearfix"></div>
</form>
Ich versuche dies so zu erklären, dass Anfänger es leichter verstehen können. Achten Sie genau auf dieelse:
photo = user_extended.photo
first_name = user.first_name
last_name = user.last_name
address = user_extended.address
phoneNumber = user_extended.phoneNumber
form = EditProfileForm(
initial={'first_name': first_name, 'last_name': last_name,
'address': address, 'phoneNumber': phoneNumber,
'photo': photo})
Es ist das, was den Wert zuschreibt, zB:
<p><label for="id_first_name">First Name:</label> <input class="form-control" id="id_first_name" name="first_name" type="text" value="Emmanuel" /></p>
<p><label for="id_last_name">Last Name:</label> <input class="form-control" id="id_last_name" name="last_name" type="text" value="Castor" /></p>