为什么我的urls.py没有从模块中拉出来?

时间:2012-02-16 17:21:55

标签: python django django-views django-urls

我的Django urls.py有:

from django.conf.urls.defaults import patterns, include, url
import admin
import settings
import pim_calendar
import pim_scratchpad
import pim.views

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

urlpatterns = patterns('',
    # Examples:
    url(r'^$', pim.views.home, name = 'home'),
    url(r'^save_calendar$', pim_calendar.views.save_calendar, name =
      'save_calendar'),
    url(r'^save_scratchpad$', pim_scratchpad.views.save_scratchpad, name =
      'save_scratchpad'),
    url(r'^view_calendar$', pim_calendar.views.view_calendar, name =
      'view_calendar'),
    url(r'^view_scratchpad$', pim_scratchpad.views.view_scratchpad, name =
      'view_scratchpad'),

    url(r'^accounts/login/$', 'django.contrib.auth.views.login'),
    # 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')),
)

if settings.DEBUG:
    urlpatterns += patterns('django.views.static',
      (r'^%s(?P<path>.*)$' % (settings.MEDIA_URL[1:],),  'serve', {
        'document_root': settings.MEDIA_ROOT,
        'show_indexes': True }),)

我得到的错误是在主页后面的第一个urlpattern,一个是pim_calendar.views.save_calendar:

AttributeError at /accounts/login/
'module' object has no attribute 'views'
Request Method: GET
Request URL:    http://localhost:8000/accounts/login/?next=/
Django Version: 1.3.1
Exception Type: AttributeError
Exception Value:    
'module' object has no attribute 'views'
Exception Location: /Users/jonathan/pim/../pim/urls.py in <module>, line 15
Python Executable:  /usr/local/bin/python
Python Version: 2.7.0
Python Path:    
['/Users/jonathan/pim',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/distribute-0.6.14-py2.7.egg',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/pip-0.8.1-py2.7.egg',
 '/usr/local/Cellar/python/2.7/lib/python27.zip',
 '/usr/local/Cellar/python/2.7/lib/python2.7',
 '/usr/local/Cellar/python/2.7/lib/python2.7/plat-darwin',
 '/usr/local/Cellar/python/2.7/lib/python2.7/plat-mac',
 '/usr/local/Cellar/python/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/usr/local/Cellar/python/2.7/lib/python2.7/lib-tk',
 '/usr/local/Cellar/python/2.7/lib/python2.7/lib-old',
 '/usr/local/Cellar/python/2.7/lib/python2.7/lib-dynload',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/PIL',
 '/usr/local/lib/python2.7/site-packages',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
Server time:    Thu, 16 Feb 2012 11:12:30 -0600
Traceback Switch to copy-and-paste view

/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
                        request.path_info) ...
▶ Local vars
/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py in resolve
            for pattern in self.url_patterns: ...
▶ Local vars
/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py in _get_url_patterns
        patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) ...
▶ Local vars
/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py in _get_urlconf_module
            self._urlconf_module = import_module(self.urlconf_name) ...
▶ Local vars
/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/utils/importlib.py in import_module
    __import__(name) ...
▶ Local vars
/Users/jonathan/pim/../pim/urls.py in <module>
    url(r'^save_calendar$', pim_calendar.views.save_calendar, name = ...
▶ Local vars

该项目的tarball位于http://JonathansCorner.com/project/pim.tgz

1 个答案:

答案 0 :(得分:6)

导入pim_calendar不会自动导入pim_calendar.views

更改您的导入声明

import pim_calendar

import pim_calendar.views

然后重复pim_scratchpad.views