2

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

但是,当我为 full_name 添加字段时,出现此错误:

ViewDoesNotExist at /phone/ Could not import accounts.views.add_phone. View does not exist in module accounts.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>

网址.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)),

)

4

3 回答 3

2

您肯定拼错了表单字段类型。最常见的是大写错误。

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

于 2013-02-13T07:45:01.727 回答
1

我拼错了“CharField”。我没有大写“F”,一旦我修复了它,它现在可以工作了。这是一个多么奇怪的错误......

于 2012-08-23T08:35:34.463 回答
0

也许您必须将视图的名称从更改add_phoneregister_complete

于 2012-08-23T08:29:37.567 回答