使用django表单的ViewDoesNotExist / Import错误

时间:2012-08-23 08:24:18

标签: django django-forms

当我添加一个表单来添加电话号码时,一切正常。

然而,当我为full_name添加字段时,我收到此错误:

ViewDoesNotExist at /phone/ Could not import accounts.views.add_phone. View does not exist in module accounts.views..

这是代码:

views.py

class PhoneForm(forms.Form):
    phone = forms.IntegerField()
    full_name = forms.Charfield()



def add_phone(request):

if request.method == 'POST':
    form = phone_name_form(request.POST)

    if form.is_valid():

        phone = form.cleaned_data['phone'] 
        #fullname = form.cleaned_data['fullname']

        user = request.user

        #store phone number to user profile
        user.get_profile().phone = phone
        user.get_profile().save()

        #store fullname in the "firstname" field for User
       # print "fullname"
        #print fullname

        return HttpResponseRedirect("/menu/")

else:
    form = phone_name_form() #an unbound form

return render_to_response("phone_form.html", {'form' : form}, context_instance=RequestContext(request))

phone_form.html

<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>

urls.py

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


from django.contrib import admin 
admin.autodiscover()

urlpatterns = patterns('',
url(r'^signup/$', 'accounts.views.signup'),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^phone/$', 'accounts.views.add_phone'),
url(r'^users/', 'accounts.views.register_complete'),


url(r'^$', 'food.views.menu'),
url(r'^menu/$', 'food.views.menu'),
url(r'^menu/(?P<hour>\d+)/$', 'food.views.menu_hour'),
url(r'^menu/(?P<hour>\d+)/(?P<food>\w+)/$', 'cart.views.add_to_cart_view'),

url(r'^images/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
url(r'^cart/', include('cart.urls')),

url(r'^checkout/', 'cart.views.checkout'),


#url(r'^menu/12/$', 'food.views.menu_hour'),

# Examples:
# url(r'^$', 'foodchute.views.home', name='home'),
# url(r'^foodchute/', include('foodchute.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)),

3 个答案:

答案 0 :(得分:2)

您的表单字段类型确实拼写错误。最常见的是资本化是错误的。

Django应该更准确地报告这一点。

答案 1 :(得分:1)

我拼错了“CharField”。我没有将“F”大写,并且一旦我修复了它,它现在有效。那是一个奇怪的错误......

答案 2 :(得分:0)

也许您必须将视图名称从add_phone更改为register_complete