gpt4 book ai didi

python - 运行测试时 Django 1.5.1 'ImportError: No module named urls'

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:56 25 4
gpt4 key购买 nike

我已经开始使用 Django 1.5 进行项目,我有以下 url、 View 和配置文件应用程序的测试。

当我浏览 localhost:8000/profiles 时,它工作正常 runserver

但是当我为配置文件应用程序 ./manage.py test profile 运行测试时,它失败并出现以下异常

  File "<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

主要网址.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)),
)

配置文件应用 url

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'),
)

个人资料.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)

配置文件.测试

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')

设置

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 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

最佳答案

如果您在名为 yoda 的应用程序外部创建了 urls.py 文件,则设置

ROOT_URLCONF = 'urls'

如果您在模块中创建了 urls.py 然后设置

ROOT_URLCONF = 'modulename.urls'

试试这个

关于python - 运行测试时 Django 1.5.1 'ImportError: No module named urls',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15758494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com