Django主应用程序的urls.py无法导入* app * .view

时间:2014-02-03 10:11:41

标签: python django django-urls

我正在尝试为django项目设置我的“主页”页面。 由于某种原因,我的主应用程序无法导入views.py,当我在shell中尝试该行时,它没有问题。关于我做错了什么的任何想法?

错误:

Could not import BoxItWebservice.views.Home.as_view(). Parent module BoxItWebservice.views.Home does not exist.

urls.py

    from django.conf.urls import patterns, include, url
#from BoxItWebservice import views

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

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'BoxItWebservice.views.Home.as_view()', name='home'),
    ##url(r'^login/', 'BoxItWebservice.views.Login.as_view()', name='login'),
    # url(r'^BoxItWebservice/', include('BoxItWebservice.foo.urls')),
    url(r'^files/', include('fileservice.urls', namespace="files")),
    url(r'^manage/', include('filesfrontend.urls', namespace="manage")),
    (r'', include('tokenapi.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),

)

views.py

from django.views.generic.base import View, TemplateView
from django.contrib.auth.decorators import login_required

from django.contrib.auth.models import User, Group
from django.views.decorators.csrf import csrf_exempt
from django.core import urlresolvers

#django token_api
from tokenapi.decorators import token_required
from django.utils.decorators import method_decorator

#Main
class Home(TemplateView):
    template_name = "home.html"

    @login_required(login_url='/login/')
    def dispatch(self,*args,**kwargs):
        return super(Home, self).dispatch(*args,**kwargs)
'''
class Login(View):
    """docstring for Login"""
'''

2 个答案:

答案 0 :(得分:0)

在你的urls.py中修复它:

from BoxItWebservice.views import Home
...
url(r'^$', Home.as_view(), name='home'),
...

进一步参考:docs

答案 1 :(得分:0)

好吧,我明白了,错误真的很愚蠢,我写了'BoxItWebservice.views.Home.as_view()',我不知道为什么我决定添加''。一旦我删除它们就一切顺利。