问题描述
当我浏览 localhost:8000 / profiles 它工作得很好
但是当我为个人资料应用程序 ./ manage.py test profile
运行测试时,会失败,并出现以下异常
文件< stdlib> /site-packages/django/core/urlresolvers.py,第342行,RegexURLResolver.urlconf_module
self =< RegexURLResolver'yoda.urls'(无:无)^ />
340 return self._urlconf_module
341除了AttributeError:
- > 342 self._urlconf_module = import_module(self.urlconf_name)
343返回self._urlconf_module
344
文件< stdlib> /site-packages/django/utils/importlib.py,行35,in import_module
name ='yoda.urls'
package = None
32 break
33 level + = 1
34 name = _resolve_name(name [level: ],package,level)
---> 35 __import __(name)
36 return sys.modules [name]
ImportError:没有名为urls的模块
main urls.py
from django.conf.urls import patterns,include,url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'',include 'profile.urls',namespace ='profile')),
url(r'^ admin /',include(admin.site.urls)),
)
个人资料应用程序网址
from django .conf.urls import patterns,url
urlpatterns = patterns('profile.views',
url(r'^ u /(?P< username> [\ -_\.\w\d] +)/ $',view ='profile_detail',name ='detail'),
url(r'^ profiles / $',view ='profile_list' ,name ='list'),
)
profile.views
from django.shortcuts import render
from django.contrib.auth.decorators import log_required
from django.db.models.signals import post_save
从配置文件导入信号
从profile.models import User
@login_required
def profile_list(request,template ='profiles / profile_list.html'):
个人资料列表
return render(request,模板,{'profiles':[]})
@login_required
def profile_detail(request,username,template ='profiles / profile_detail.html'):
个人资料详细信息
返回呈现(请求,模板)
#连接信号和发件人
post_save.connect(signals.user_created,sender =用户)
profile.tests
from django.core.urlresolvers import reverse
/ pre>
from django.test import TestCase
from profile.tests.factories import UserFactory
class UrlsTest(TestCase):
测试类别应用程序的URL
def setUp(self):
self.user = UserFactory.build()
def get(self,url,follow = False):
return self.client.get(url,follow = follow)
def test_list_profiles(self):
具有配置文件列表的页面
self.client.login(username = self.user.username,password ='pass')
print(reverse('profile:list'))
res = self.get(reverse ('profile:list')
self.assertTrue(res.status_code,200)
self.assertTemplateUsed('profile / profile_list.html')
设置
import os
import sys
here = lambda * x:os.path.abspath(os.path.join(os.path.abspath(os.path.dirname(__ file__)),* x))
PROJECT_NAME ='yoda'
PROJECT_ROOT = here('../ ..')
root = lambda * x:os.path.abspath(os.path.join (os.path.abspath(PROJECT_ROOT),* x))
sys.path.insert(0, root('yoda'))
sys.path.insert(0,PROJECT_ROOT)
sys.path.insert(0,root('apps'))
...
ROOT_URLCONF ='yoda.urls'
可能会导致这样的问题?在具有相同配置和Django 1.4.5的项目中,它的工作正常。
调试Django内部
试图调试Django
django.core.urlresolvers:263
。当ROOT_URLCONF ='yoda.urls'
它失败,错误显示在上面,但我试过yoda.yoda.urls
和有效。我对这个问题感到困惑,还在努力修复。ipdb> __import __('yoda.yoda.urls')
< module'yoda'from'/Users/sultan/.virtualenvs/yoda/yoda/__init__.pyc'>
小更新
收到ROOT_URLCONF从设置我们有以下错误
> /Users/sultan/.virtualenvs/yoda/lib/python2.7/site-packages/django/core/urlresolvers.py(351)url_patterns()
350 def url_patterns(self):
- > 351 patterns = getattr(self.urlconf_module,urlpatterns,self.urlconf_module)
352 try:
ipdb> l
346 self._urlconf_module = import_module(self.urlconf_name)
347 return self._urlconf_module
348
349 @property
350 def url_patterns(self):
- > 351 pattern = getattr(self.urlconf_module,urlpatterns,self.urlconf_module)
352 try:
353 iter(patterns)
354除了TypeError:
355 raise不正确的配置(包含的urlconf%s中没有任何模式%self.urlconf_name
356返回模式
ipdb> self.urlconf_module
*** ImportError:没有名为urls的模块
解决方案如果您在名为yoda的应用程序之外创建了
urls.py
文件,则设置ROOT_URLCONF ='urls'
如果您创建了
urls.py
在模块中,然后设置ROOT_URLCONF ='modulename.urls'
尝试这个
I've started project with Django 1.5 I've the following urls, views, and tests of the profile app.
When I browse
localhost:8000/profiles
it works just finebut when I run test for profile app
./manage.py test profile
it fails with the following exceptionFile "<stdlib>/site-packages/django/core/urlresolvers.py", line 342, in RegexURLResolver.urlconf_module self = <RegexURLResolver 'yoda.urls' (None:None) ^/> 340 return self._urlconf_module 341 except AttributeError: --> 342 self._urlconf_module = import_module(self.urlconf_name) 343 return self._urlconf_module 344 File "<stdlib>/site-packages/django/utils/importlib.py", line 35, in import_module name = 'yoda.urls' package = None 32 break 33 level += 1 34 name = _resolve_name(name[level:], package, level) ---> 35 __import__(name) 36 return sys.modules[name] ImportError: No module named urls
main urls.py
from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', url(r'', include('profile.urls', namespace='profile')), url(r'^admin/', include(admin.site.urls)), )
profiles app urls
from django.conf.urls import patterns, url urlpatterns = patterns('profile.views', url(r'^u/(?P<username>[\-_\.\w\d]+)/$', view='profile_detail', name='detail'), url(r'^profiles/$', view='profile_list', name='list'), )
profile.views
from django.shortcuts import render from django.contrib.auth.decorators import login_required from django.db.models.signals import post_save from profile import signals from profile.models import User @login_required def profile_list(request, template='profiles/profile_list.html'): """ Profiles list """ return render(request, template, {'profiles': []}) @login_required def profile_detail(request, username, template='profiles/profile_detail.html'): """ Profile details """ return render(request, template) # Connect signals and senders post_save.connect(signals.user_created, sender=User)
profile.tests
from django.core.urlresolvers import reverse from django.test import TestCase from profile.tests.factories import UserFactory class UrlsTest(TestCase): """ Testing urls for category application """ def setUp(self): self.user = UserFactory.build() def get(self, url, follow=False): return self.client.get(url, follow=follow) def test_list_profiles(self): """ Page with the list of profiles """ self.client.login(username=self.user.username, password='pass') print(reverse('profile:list')) res = self.get(reverse('profile:list')) self.assertTrue(res.status_code, 200) self.assertTemplateUsed('profile/profile_list.html')
settings
import os import sys here = lambda * x: os.path.abspath(os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)) PROJECT_NAME = 'yoda' PROJECT_ROOT = here('../..') root = lambda * x: os.path.abspath(os.path.join(os.path.abspath(PROJECT_ROOT), *x)) sys.path.insert(0, root('yoda')) sys.path.insert(0, PROJECT_ROOT) sys.path.insert(0, root('apps')) ... ROOT_URLCONF = 'yoda.urls'
What may cause problems like this? In the project with pretty same config and on Django 1.4.5 it works fine.
Debugging Django internals
Tried to debug Django
django.core.urlresolvers:263
. WhenROOT_URLCONF='yoda.urls'
it fails with the errors show above but I triedyoda.yoda.urls
and it worked. I'm confused about this problem, still trying to fix.ipdb> __import__('yoda.yoda.urls') <module 'yoda' from '/Users/sultan/.virtualenvs/yoda/yoda/__init__.pyc'>
Small update
When it receives ROOT_URLCONF from settings we have the following error
> /Users/sultan/.virtualenvs/yoda/lib/python2.7/site-packages/django/core/urlresolvers.py(351)url_patterns() 350 def url_patterns(self): --> 351 patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 352 try: ipdb> l 346 self._urlconf_module = import_module(self.urlconf_name) 347 return self._urlconf_module 348 349 @property 350 def url_patterns(self): --> 351 patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 352 try: 353 iter(patterns) 354 except TypeError: 355 raise ImproperlyConfigured("The included urlconf %s doesn't have any patterns in it" % self.urlconf_name) 356 return patterns ipdb> self.urlconf_module *** ImportError: No module named urls
解决方案If you created the
urls.py
file outside of app named yoda, then set theROOT_URLCONF = 'urls'
if you created
urls.py
in a module then setROOT_URLCONF = 'modulename.urls'
Try with this
这篇关于Django 1.5.1'ImportError:运行测试时没有名为urls的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 02:53