/ registration /'UserForm'对象的AttributeError没有属性'save'

时间:2012-01-30 09:37:31

标签: django django-forms django-templates django-views

Request Method:     POST
Request URL:    http://127.0.0.1:8000/registration/
Django Version:     1.3.1
Exception Type:     AttributeError
Exception Value:    

'UserForm' object has no attribute 'save'

Exception Location:     /home/forent/myprograms/mysite7/registration/views.py in user_details, line 15
Python Executable:  /usr/bin/python
Python Version:     2.7.2
Python Path:    

['/home/forent/myprograms/mysite7',
 '/usr/local/lib/python2.7/dist-packages/oauth2-1.5.211-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/local/lib/python2.7/dist-packages/python_twitter-0.8.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/ipython-0.12-py2.7.egg',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PIL',
 '/usr/lib/python2.7/dist-packages/gst-0.10',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.7',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/lib/python2.7/dist-packages/ubuntuone-client',
 '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel',
 '/usr/lib/python2.7/dist-packages/ubuntuone-couch',
 '/usr/lib/python2.7/dist-packages/ubuntuone-installer',
 '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']

Server time:    Mon, 30 Jan 2012 03:24:22 -0600

我得到了这样的错误。我不能保存我的表格。请帮我保存表格

我的代码如下

#view 

from django.shortcuts import render_to_response
from registration.models import UserDetails
from forms import UserForm
from django import forms
from django.template import RequestContext
from django.http import HttpResponseRedirect

def user_details(request):
    if request.method == 'POST':
        form = UserForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/thanx/')

    else:
        form = UserForm()        
    return render_to_response("career.html", {"form": form},context_instance=RequestContext(request))


#form 

    from django import forms
from registration.models import UserDetails


class UserForm(forms.Form ):
    fname=forms.CharField(max_length=20)
    lname=forms.CharField(max_length=20)
    email = forms.EmailField()
    address = forms.CharField(max_length=50)
    country = forms.CharField(max_length=20)

#model 

from django.db import models

class UserDetails(models.Model):
    fname=models.CharField(max_length=20)
    lname=models.CharField(max_length=20)
    email = models.EmailField()
    address = models.CharField(max_length=50)
    country = models.CharField(max_length=20)
    def __unicode__(self):
        return self.fname
        return self.lname
        return self.email
        return self.address
        return self.country


#url

from django.conf.urls.defaults import patterns, include, url

from django.contrib import admin
admin.autodiscover()
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
     url(r'^registration/$', 'registration.views.user_details', name='user_details'),
    # url(r'^mysite7/', include('mysite7.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
      url(r'^admin/', include(admin.site.urls)),
)

# template 

<form enctype="multipart/form-data" method="post">{% csrf_token %}
    {{ form.as_p }}
   <input type="submit" ....>
</form>

我还希望将ajaxcode与我的代码一起添加到一页中的表格的前3个文件中。其余2个字段的下一页。并且页面想要在单一窗口中显示。并且还必须验证字段。但我对django的ajax没有明确的想法。我怎样才能在我的代码中应用所有这些东西

3 个答案:

答案 0 :(得分:3)

您无法保存表单。表单用于显示模型(DB)到用户(通常)的数据。您可以使用forms.ModelForm

从表单保存模型的实例

https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method

<强> forms.py

class UserForm(forms.ModelForm):
    pass

    class Meta:
        model = UserDetails

P.S。 Django已经内置了身份验证,因此无需复制代码。请查看django.contrib.auth https://docs.djangoproject.com/en/dev/topics/auth/

答案 1 :(得分:1)

您定义的UserForm不是ModelForm,因此does not have a save() method

使用ModelForm例如

class UserForm(forms.ModelForm):
    class Meta:
        model = UserDetails

答案 2 :(得分:0)

我在Django 3中遇到了'UserForm' object has no attribute 'save'错误(2020年5月)。这是因为在我的表格中,我只给出了forms.Form

我通过这种方式更正了

class my_form_name(forms.Form,forms.ModelForm)

注意:元类很重要。