Django:无法导入名称 [英] Django: cannot import name

查看:273
本文介绍了Django:无法导入名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站部署新的更新时,我遇到了一个非常烦人的问题。我有两个单独的网站,而其中一个是开发版本。现在,当我想对生产进行更改时,它将无法正常工作,因为出现错误信息:


文件
/ usr / lib / python2.6 / dist-packages / django / core / handlers / base.py,
第99行,在get_response中

request.path_info)



文件
/usr/lib/python2.6/dist-packages/django/core/urlresolvers.py,
line 249,解析$ b中的模式$ b self.url_patterns:



文件
/usr/lib/python2.6/dist-packages/django/core/urlresolvers.py,
行278,在_get_url_patterns中

patterns =
getattr(self.urlconf_module,
urlpatterns,self.urlconf_module)


$ b $文件
/usr/lib/python2.6/dist-packages/django/core/urlresolvers.py,
第273行,在_get_urlconf_module中

self。 _urlconf_module =
import_module(self.urlconf_name)



文件
/usr/lib/python2.6/dist-packages/django/utils/ importlib.py,
行35,in import_module

import (name)



文件
/ srv / sites / spelutveckla_se / urls.py,
第21行,在

(r'^ account / login / $',
LoginView.as_view()),



NameError:name'LoginView'不是
定义


没关系,如果我删除该应用程序,视图,类或模块,因为它会抱怨另一个模块。 LoginView IS在import语句中定义。这些文件只是在另一个子域上运行的开发文件(不包括settings.py)的一个新的副本,没有任何问题。我已经检查了settings.py几次,确保设置了正确的设置(不同的目录路径)。我还检查过apache2 www-data用户有权访问这些文件。我也重新启动了apache几次,并重复复制了一遍又一遍的文件,但没有任何作用。



我绝望,并没有什么问题的线索可能是...?



这是我的urls.py在顶部的样子:

从django.views.generic导入DetailView,ListView,TemplateView
从django导入
从django.conf.urls.defaults导入。 contrib从project.models导入admin
项目
从project.views导入详细信息,EditView作为EditProject,CreateProjectView,EditProjectLinksView,EditProjectFeedsView,EditProjectTagsView,EditMultimediaView,PostForumTopic,AudioFormset
from frontpage.views import FrontpageView
从userprofile.views导入UserRegistrationView,UserRegistrationActivationView,LogoutView,UserProfileView,LoginView,EditProfileView,CreateUserProfileView
从registration.views导入激活为UserActivatedView


解决方案

您最有可能在模块中有一个循环导入 LoginView 被定义,即当您导入定义 LoginView 视图模块时,某些声明反过来导入还有一些其他模块仍在等待完全解读。



以下是一个比较好的例子:

 #myapp.urls 

from django.conf.urls.defaults import *
from myapp import views

urlpatterns = patterns('',
#...


#myapp.views

from django.core.urlresolvers import reverse
from django.views.generic.edit import CreateView

class SomeCreateView(CreateView):

#BOOM!
success_url = reverse('myapp:some-url')

一次 myapp.views 被导入,并且 SomeCreateView 类型被分配给内存, reverse('myapp:some-url ')将被执行,您的 myapp.urls 最终将由Django导入,只有这样才能实现,因为 myapp.urls 将无限期等待 myapp.views 导入。


I've just encountered a very annoying problem while deploying new updates to my website. I have two seperate websites whereas one of them is a development version. Now when I want to apply my changes to the production it won't work because of error message:

File "/usr/lib/python2.6/dist-packages/django/core/handlers/base.py", line 99, in get_response
request.path_info)

File "/usr/lib/python2.6/dist-packages/django/core/urlresolvers.py", line 249, in resolve for pattern in self.url_patterns:

File "/usr/lib/python2.6/dist-packages/django/core/urlresolvers.py", line 278, in _get_url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

File "/usr/lib/python2.6/dist-packages/django/core/urlresolvers.py", line 273, in _get_urlconf_module
self._urlconf_module = import_module(self.urlconf_name)

File "/usr/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
import(name)

File "/srv/websites/spelutveckla_se/urls.py", line 21, in
(r'^account/login/$', LoginView.as_view()),

NameError: name 'LoginView' is not defined

It doesn't matter if I remove that app, view, class or module because it will complain about an other module instead. LoginView IS defined in an import statement. The files are just a fresh copy of the development files (except for settings.py) that is running on an another subdomain without any problem. I've checked the settings.py several times and made sure the correct settings are set (differs by a couple of directory paths). I've also checked that the apache2 www-data user has permission to access the files. i've also restarted apache a couple of times and re-copied the files over and over but nothing works.

I'm desperate and have no clue of what the problem might be...?

Here's what my urls.py looks like at the top:

from django.conf.urls.defaults import *
from django.conf import settings
from django.views.generic import DetailView, ListView, TemplateView
from django.contrib import admin
from project.models import Project
from project.views import Detail, EditView as EditProject, CreateProjectView, EditProjectLinksView, EditProjectFeedsView, EditProjectTagsView, EditMultimediaView, PostForumTopic, AudioFormset
from frontpage.views import FrontpageView
from userprofile.views import UserRegistrationView, UserRegistrationActivationView, LogoutView, UserProfileView, LoginView, EditProfileView, CreateUserProfileView
from registration.views import activate as UserActivatedView

解决方案

You most likely had a circular import in the module LoginView was defined in, i.e. when you were importing the views module that defined LoginView, some statement somehow in turn imported some other module that was still waiting to get fully interpreted.

Here's an example to give you a better idea:

# myapp.urls

from django.conf.urls.defaults import *
from myapp import views

urlpatterns = patterns('',
    # ...
)

# myapp.views

from django.core.urlresolvers import reverse
from django.views.generic.edit import CreateView

class SomeCreateView(CreateView):

    # BOOM!
    success_url = reverse('myapp:some-url')

Once myapp.views gets imported and the SomeCreateView type gets allocated to memory, reverse('myapp:some-url') will get executed and your myapp.urls will eventually be imported by Django, only that that won't ever be possible since myapp.urls will indefinitely wait for myapp.views to get imported.

这篇关于Django:无法导入名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆