Reputation:
I try to fill in a standard form. Only the saving of the model does not work. It then changes to the default error page on save ().
The error says:
'WohnungHinzufuegenForm' object has no attribute 'save'
What could be the mistake?
urls.py:
path('user/ereignis/wohnungHinzufuegen', views.wohnungHinzufuegen,name="wohnungHinzufuegen"),
models.py:
class Wohnungseinheiten(models.Model):
wohnungsnummer = models.AutoField(primary_key=True)
strasseHausnummer = models.CharField("strasseHausnummer",max_length=100)
adresszusatz = models.CharField("adresszusatz",max_length=100)
plz = models.CharField("plz",max_length=100,blank=True)
ort = models.CharField("ort",max_length=100)
views.py:
@login_required
def wohnungHinzufuegen(request):
if request.method == 'POST':
form4 = WohnungHinzufuegenForm(request.POST)
if form4.is_valid():
#Here he definitely goes in and fills in the test variables:
tmpadresszusatz=form4.cleaned_data['strasseHausnummer'] #contains values
tmpadresszusatz=form4.cleaned_data['adresszusatz'] #contains values
try:
form4.save() #this doesn't save -> it shows the default error page then
except Exception as e:
return HttpResponse(str("done."+e)) #returns never
return HttpResponse(str("done."+tmpLogin +"|"+tmpPassword))
return redirect('user/ereignis')
forms.py:
class WohnungHinzufuegenForm(forms.Form):
strasseHausnummer = forms.CharField(required=True,max_length=100)
adresszusatz = forms.CharField(required=False,max_length=100)
plz = forms.CharField(required=True,max_length=100)
ort = forms.CharField(required=False,max_length=100)
class Meta:
model = Wohnungseinheiten
fields = ('strasseHausnummer','adresszusatz','plz','ort')
Upvotes: 0
Views: 66
Reputation: 1261
Form
class does not implement save()
method. You need ModelForm
class.
class WohnungHinzufuegenForm(forms.ModelForm): # forms.ModelForm instead of forms.Form
strasseHausnummer = forms.CharField(required=True,max_length=100)
adresszusatz = forms.CharField(required=False,max_length=100)
plz = forms.CharField(required=True,max_length=100)
ort = forms.CharField(required=False,max_length=100)
class Meta:
model = Wohnungseinheiten
fields = ('strasseHausnummer','adresszusatz','plz','ort')
Hope, it helps you.
Upvotes: 1
Reputation:
Only based forms "ModelForm" have a built-save method. For forms based on "Form", you have to create a save method.
Upvotes: 0