Reputation:
So I have a student model which inherits from AbstractUser. I used 2 forms in one view for registration since I needed email, name and surname to be in my student database (as well as other fields). Now I'm trying to make an update profile view, with 2 forms that I made especially for updating the info. But I think I'm getting it wrong.. might need a little help here. I need the student to be able to update his email (which is from User model) and his photo, phone, name and surname (which are in Student model).
<form method="POST" action="{% url 'profile_edit' %}" class="" >
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
def profile_edit(request):
user = request.user
form1 = UserEditForm(request.POST or None, initial={'email': user.email,
})
form2 = StudentEditForm(request.POST or None, initial={'name': user.student.name,
'surname': user.student.surname,
'phone': user.student.phone,
'photo': user.student.photo})
if request.method == 'POST':
if form1.is_valid() and form2.is_valid():
user.email = request.POST['name']
user.student.name = request.POST['name']
user.student.surname = request.POST['surname']
user.student.phone = request.POST['phone']
user.student.photo = request.POST['photo']
user.save()
return render(request, 'index.html')
context = {
"form1": form1,
"form2": form2
}
return render(request, "registration/profile_edit.html", context)
class UserForm(forms.ModelForm):
email = forms.EmailField(required=True)
password = forms.CharField(label='Password', max_length=32, required=True, widget=forms.PasswordInput)
confirm_password = forms.CharField(label='Confirm', max_length=32, required=True, widget=forms.PasswordInput,
help_text="Passwords must match!")
class StudentForm(forms.ModelForm):
name = forms.CharField(max_length=30, required=True)
surname = forms.CharField(max_length=50, required=True)
student_ID = forms.CharField(required=True, max_length=14, min_length=14)
photo = forms.ImageField(required=True)
phone = forms.CharField(max_length=15, required=True)
class Meta:
model = Student
fields = ('name', 'surname', 'phone', 'student_ID', 'photo')
class UserEditForm(forms.ModelForm):
email = forms.EmailField(required=False)
class Meta:
model = User
fields = ('email',)
class StudentEditForm(forms.ModelForm):
name = forms.CharField(max_length=30)
surname = forms.CharField(max_length=50)
photo = forms.ImageField(required=False)
phone = forms.CharField(max_length=15, required=False)
class Meta:
model = Student
fields = ('name', 'surname', 'phone', 'photo')
Problem is that I am getting no form, so I am either doing something wrong in the view, either the forms.
Upvotes: 0
Views: 94
Reputation: 50
You don't need two forms. I have answered your previous question which led to this question.
It will save you from a lot of unnecessary Code.
Upvotes: 0
Reputation: 27503
<form method="POST" action="{% url 'profile_edit' %}" class="" >
{% csrf_token %}
{{ form1.as_p }}
{{ form2.as_p }}
<button type="submit">Save</button>
</form>
according to your context the names of your forms are form1
and form2
, so form
only wont display any form
Upvotes: 1