视图在模块中不存在

2024-05-23 14:53:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Kubuntu 12.04上用Python 2.7运行Django 1.4。

我这是我的views.py

from __future__ import unicode_literals
from django.shortcuts import render_to_response
from django.core.context_processors import csrf
from rsb.forms import RegisterForm

def index(request):
    return render_to_response("index.html")

def services(request):
    return render_to_response("services.html")

def login(request):
    return render_to_response("login.html")

def contact(request):
    return render_to_response("contact.html")

def about(request):
    return render_to_response("about.html")

def registerUser(request):
    form = RegisterForm()
    data = {}
    data.update(csrf(request))
    data.update({ 'form' : form })
    return render_to_response("register.html", data)

def addUser(request):
    return render_to_response("added_user.html")

这是我的urls.py

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

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

urlpatterns = patterns('',
    url(r'^services/', 'rsb.views.services'),
    url(r'^login/', 'rsb.views.login'),
    url(r'^register/', 'rsb.views.registerUser'),
    url(r'^contact/', 'rsb.views.contact'),
    url(r'^about/', 'rsb.views.about'),
    url(r'^addUser/', 'rsb.views.addUser'),

    url(r'^admin/', include(admin.site.urls)),

)

这是我的forms.py

class RegisterForm(forms.Form):
    client_type = ('Personal', 'Company')
    countries = Countries.objects.all()
    unitedStates = UnitedStates.objects.all()

    country_choices = []
    for item in countries:
        country_choices.append(countries.name)

    state_choices = []
    for item in unitedStates:
        state_choices.append(unitedStates.name)

    rsb_client_type = forms.ChoiceField(widget = forms.Select(), choices = client_type, required = True)
    rsb_first_name = forms.CharField(max_length = 25, required = True)
    rsb_last_name = forms.CharField(max_length = 25, required = True)
    rsb_company_name = forms.CharField(max_length = 25)
    rsb_address1 = forms.CharField(max_length = 50, required = True)
    rsb_address2 = forms.CharField(max_length = 50)
    rsb_city = forms.CharField(max_length = 50, required = True)
    rsb_country = forms.ChoiceField(widget = forms.Select(), choices = country_choices, required = True)

    if (rsb_country == 'United States'):
        rsb_state = forms.ChoiceField(widget = forms.Select(), choices = state_choices, required = True)
    else:
        rsb_state = forms.CharField(max_length = 50, required = True)

    rsb_zip_code = forms.CharField(max_length = 25, required = True)
    rsb_phone_number = USPhoneNumberField(label = "Phone", widget = USPhoneNumberMultiWidget(), required = True)
    rsb_email = forms.EmailField(required = True)

请注意,这远没有抛光。我只是在访问我的视图时遇到了问题。我运行python manage.py runserver并尝试http://127.0.0.1:8000/register/,收到以下错误:

Could not import rsb.views.registerUser. View does not exist in module rsb.views.

无论尝试访问哪个视图,我都会收到类似的错误。

请帮忙。

编辑1:

对不起,这是回溯:

Traceback:
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.1-py2.7.egg/django/core/handlers/base.py" in get_response
  101.                             request.path_info)
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.1-py2.7.egg/django/core/urlresolvers.py" in resolve
  300.                     sub_match = pattern.resolve(new_path)
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.1-py2.7.egg/django/core/urlresolvers.py" in resolve
  209.             return ResolverMatch(self.callback, args, kwargs, self.name)
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.1-py2.7.egg/django/core/urlresolvers.py" in callback
  216.         self._callback = get_callable(self._callback_str)
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.1-py2.7.egg/django/utils/functional.py" in wrapper
  27.         result = func(*args)
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.1-py2.7.egg/django/core/urlresolvers.py" in get_callable
  101.                     (lookup_view, mod_name))

Exception Type: ViewDoesNotExist at /register/
Exception Value: Could not import rsb.views.registerUser. View does not exist in module rsb.views.

Tags: todjangoinpyimporttruereturnresponse
2条回答

这是由于我如何使用Django的ORM获取一些数据库信息的潜在问题。这些错误似乎并没有指向它……但我已经弄清楚出了什么问题。谢谢你的帮助!

根据PEP 8rsb.views.registerUser重命名为rsb.views.register_user

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

现在来谈谈这个问题。你有:

country_choices = []
for item in countries:
    country_choices.append(countries.name)

state_choices = []
for item in unitedStates:
    state_choices.append(unitedStates.name)

应该是:

country_choices = []
for item in countries:
    country_choices.append(item.name)

state_choices = []
for item in unitedStates:
    state_choices.append(item.name)

相关问题 更多 >