为什么 Django 项目 URL 并非全部可供 Django 测试客户端使用?

发布于 2024-09-15 11:30:43 字数 1749 浏览 6 评论 0原文

我一直在尝试将 django-lean 应用程序添加到我的项目中。 django-lean 应用程序不位于我正在处理的项目中,它位于 PYTHONPATH 上。

我无法通过 django-lean 测试。

问题似乎在于 TestCase 定义了 url 的值:

 urls = 'django_lean.experiments.tests.urls'

据我所知,测试仅获取位于 @ 'django_lean.experiments.tests.urls' 的 url,而不是 项目其余部分的 url。

这会导致错误消息,例如:

NoReverseMatch: Reverse for 'index' with arguments '()' and keyword arguments '{}' not found.

这些是由项目中的 {% url %} 模板标记触发的。

如何确保项目的所有 url 都可用于测试?

编辑: 有人向我展示了一个打印可见 URL 的脚本:

import urls

def show_urls(urllist, depth=0):
  for entry in urllist:
    print "  " * depth, entry.regex.pattern
    if hasattr(entry, 'url_patterns'):
        show_urls(entry.url_patterns, depth + 1)

我从 ipdb 调用了该脚本,这是输出:

   ipdb> import urls
   ipdb> show_urls(urls.urlpatterns)
    ^test-experiment/(?P<experiment_name>.*)$
    ^test-clientsideexperiment/(?P<experiment_name>.*)$
    ^admin/
      ^(?P<experiment_name>.+)/$
      ^$
    ^main-app/
      ^goal/(?P<goal_name>.*)$
      ^confirm_human/$

这对应于位于 @ 'django_lean.experiments.tests.urls' 的

urlpatterns = patterns('django_lean.experiments.tests.views',
  url(r'^test-experiment/(?P<experiment_name>.*)$', 'experiment_test'),
  url(r'^test-clientsideexperiment/(?P<experiment_name>.*)$', 'clientsideexperiment_test'))

urlpatterns += patterns('',
   url(r'^admin/', include('django_lean.experiments.admin_urls')),
   url(r'^main-app/', include('django_lean.experiments.urls')),

URL我的问题是,我的测试全部失败,因为项目中其他应用程序的命名 url 由 URL 模板标签调用,测试无法访问。

我正在运行 Python 2.7 和 Django 1.2.1

I've been trying to add the django-lean app to my project.
The django-lean app is not located in the project I'm working on, it is on the PYTHONPATH.

I have not been able to get the django-lean tests to pass.

It seems the issue is that the TestCase defines a value for urls:

 urls = 'django_lean.experiments.tests.urls'

As best as I can tell, the tests are only getting the urls located @ 'django_lean.experiments.tests.urls', but not
the urls from the rest of the project.

This is causing error messages like:

NoReverseMatch: Reverse for 'index' with arguments '()' and keyword arguments '{}' not found.

These are triggered by {% url %} template tags in the project.

How can I make sure that the all the urls of the project are available for the tests?

EDIT:
Someone showed me a script to print the visible URLs:

import urls

def show_urls(urllist, depth=0):
  for entry in urllist:
    print "  " * depth, entry.regex.pattern
    if hasattr(entry, 'url_patterns'):
        show_urls(entry.url_patterns, depth + 1)

I called this script from ipdb, this was the output:

   ipdb> import urls
   ipdb> show_urls(urls.urlpatterns)
    ^test-experiment/(?P<experiment_name>.*)$
    ^test-clientsideexperiment/(?P<experiment_name>.*)$
    ^admin/
      ^(?P<experiment_name>.+)/$
      ^$
    ^main-app/
      ^goal/(?P<goal_name>.*)$
      ^confirm_human/$

This corresponds to the urls located @ 'django_lean.experiments.tests.urls'

urlpatterns = patterns('django_lean.experiments.tests.views',
  url(r'^test-experiment/(?P<experiment_name>.*)

The issue that I'm having is that my tests all fail because of the named urls from other apps in the project are called by URL template tags are not accessible to the tests.

I'm running Python 2.7 with Django 1.2.1

, 'experiment_test'), url(r'^test-clientsideexperiment/(?P<experiment_name>.*)

The issue that I'm having is that my tests all fail because of the named urls from other apps in the project are called by URL template tags are not accessible to the tests.

I'm running Python 2.7 with Django 1.2.1

, 'clientsideexperiment_test')) urlpatterns += patterns('', url(r'^admin/', include('django_lean.experiments.admin_urls')), url(r'^main-app/', include('django_lean.experiments.urls')),

The issue that I'm having is that my tests all fail because of the named urls from other apps in the project are called by URL template tags are not accessible to the tests.

I'm running Python 2.7 with Django 1.2.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛溺水了 2024-09-22 11:30:43

解决方案非常简单。只需将主项目中的 URL 导入到应用程序的 urls.py 中即可。

from forum.urls import urlpatterns

或者对于更通用的解决方案:

from settings import ROOT_URLCONF as project_urls
urlpatterns = __import__('forum.urls').urls.urlpatterns

The solution was pretty simple. Just import the URLs from the main project into the urls.py for the app.

from forum.urls import urlpatterns

or for a more generic solution:

from settings import ROOT_URLCONF as project_urls
urlpatterns = __import__('forum.urls').urls.urlpatterns
染墨丶若流云 2024-09-22 11:30:43

要列出 django 知道的所有 url 模式,您可以使用此处建议的答案
从您的测试中运行它并打印/记录输出。

请注意,最好明确说明从何处导入 url,

 from myproject import urls

因为您可能还有一些包含 url 文件的其他模块。

To list all the url patterns your django knows you can use the answer suggested here.
Run this from your tests and print/log the output.

Just note that its better to explicitly state where to import the urls from like

 from myproject import urls

because you probably have some other modules containing urls files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文