Reputation: 17
I am trying to create a form which will allow users to update their name, I am using a forms.form instead of ModelForm because it gives you more control over the styling of the form because you can use widgets. when I go to save the form it says that updateNameForm has no attribute save
view
def UpdateName(request,user_id):
if request.method == "POST":
form = UpdateNameForm(request.POST,initial=initial)
if form.is_valid():
name = form.cleaned_data['name']
form.save()
else:
form = UpdateNameForm(initial=initial)
forms.py
class UpdateNameForm(forms.Form):
name = forms.CharField(
required=True,
label="*Status",
widget=forms.widgets.Select(attrs={'class' : 'span6 small-margin-top small-margin-bottom'})
)
Upvotes: 0
Views: 625
Reputation: 17
def UpdateName(request,user_id):
user = get_user(user_id)
if request.method == "POST":
form = UpdateNameForm(request.POST,initial=initial)
if form.is_valid():
name = form.cleaned_data['name']
user.name = name
user.save()
else:
form = UpdateNameForm(initial=initial)
Upvotes: 0
Reputation: 599490
A standard form class indeed has no save
attribute - it doesn't know anything about any models to save to. You need to use a ModelForm.
Note also you need to pass the instance
argument to the form to make it update an existing instance, rather than create a new one. You probably don't need initial
at all.
Upvotes: 3