关于django:/ registration __init __()的TypeError得到了一个意外的关键字参数’null’

TypeError at /registration __init__() got an unexpected keyword argument 'null'

请求方法:GET
请求URL:http://127.0.0.1:8000 /注册
Django版本:1.3.1
异常类型:TypeError
例外价值:

__init__()得到了一个意外的关键字参数'null'

Exception Location:
/usr/local/lib/python2.7/dist-packages/django/forms/fields.py in
init, line 196 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: Fri, 27 Jan 2012 11:11:22 -0600

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
view

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


def user_details(request):
    if request.method=="POST":
        uform=UserForm(request.POST)
        if uform.is_valid():
            profile=uform.save(commit=False)
            profile.save()
        else:
            uform=UserForm()
        return render_to_response('career.html',{'uform':uform},context_instance=RequestContext(request))

#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
#forms

from django import forms
from registration.models import UserDetails


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

#urls

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 %}
    {{ uform.as_p }}
   <input type="submit" ....>
</form>

这是我写的代码..


表单字段没有nullblank参数。 这些仅适用于模型字段。 对于表单字段,您只需required

但是,您应该使用ModelForm,它将从模型中为您创建表单字段,并允许您以后保存它。


不是肯定的,但我不认为你可以在forms.CharField()上使用null=Trueblank=True,你将null作为参数传递给forms.CharField的init()并且解释器抛出了这个错误。 尝试从以下位置删除null=True

将表单更改为:

1
2
3
4
5
6
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)

根据django站点Django表单字段,CharField有两个可选的验证参数:max_length和min_length,以及required。