我已经使用cookiecutter-django模板启动了一个项目。之后,我翻译成4种语言,并为URL添加了i18n,以便根据访问每种语言,将其翻译为另一种语言。例如:
问题是我开始应用TDD并首先进行了测试:
myapp / tests.py :
from django.test import TestCase
from django.urls import resolve
from django_sftpbrowser.views import browse_page
# Create your tests here.
class BrowsePageTest(TestCase):
def test_browse_url_resolves_to_browse_page(self):
found = resolve('browse/') # Tried also en/browse/
self.assertEqual(found.func, browse_page)
但是,尽管URL似乎可以在浏览器中运行,但是在运行此测试时,我发现了此错误:
======================================================================
ERROR: test_browse_url_resolves_to_browse_page (django_sftpbrowser.tests.BrowsePageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/madtyn/PycharmProjects/corelli/django_sftpbrowser/tests.py", line 9, in test_browse_url_resolves_to_browse_page
found = resolve('browse/')
File "/home/madtyn/venvs/corelli/lib/python3.6/site-packages/django/urls/base.py", line 24, in resolve
return get_resolver(urlconf).resolve(path)
File "/home/madtyn/venvs/corelli/lib/python3.6/site-packages/django/urls/resolvers.py", line 568, in resolve
raise Resolver404({'path': path})
django.urls.exceptions.Resolver404: {'path': 'browse/'}
我的网址如下。您会注意到i18n网址格式,我认为它会以某种方式引发所描述的错误:
config / urls.py :
from django.conf import settings
from django.urls import include, path
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic import TemplateView
from django.views import defaults as default_views
from django.conf.urls.i18n import i18n_patterns
urlpatterns = i18n_patterns(*[
path("", TemplateView.as_view(template_name="pages/home.html"), name="home"),
path(
"about/", TemplateView.as_view(template_name="pages/about.html"), name="about"
),
# Django Admin, use {% url 'admin:index' %}
path(settings.ADMIN_URL, admin.site.urls),
# User management
path("users/", include("corelli.users.urls", namespace="users")),
# This relies on the 'django-allauth' pip3 package
path("accounts/", include("allauth.urls")),
# Your stuff: custom urls includes go here
path("browse/", include("myapp.urls", namespace="myappbrowser-root")),
path("browse/<path:input_path>", include("myapp.urls", namespace="myappbrowser-resource")),
# i18n: sets a user’s language preference and redirects to a given URL
# or, by default, back to the previous page
path("i18n/", include('django.conf.urls.i18n')),
]) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
# This allows the error pages to be debugged during development, just visit
# these url in browser to see how these error pages look like.
urlpatterns += [
path(
"400/",
default_views.bad_request,
kwargs={"exception": Exception("Bad Request!")},
),
path(
"403/",
default_views.permission_denied,
kwargs={"exception": Exception("Permission Denied")},
),
path(
"404/",
default_views.page_not_found,
kwargs={"exception": Exception("Page not Found")},
),
path("500/", default_views.server_error),
]
if "debug_toolbar" in settings.INSTALLED_APPS:
import debug_toolbar
urlpatterns = [path("__debug__/", include(debug_toolbar.urls))] + urlpatterns
myapp / urls.py :
from django.urls import path
from .views import browse_page
app_name = 'myappbrowser'
urlpatterns = [
path('', browse_page, name='myapp-root'),
#path('<path:input_path>', browse_page, name='myapp-resource'),
]
myapp / views.py :
def browse_page(request, input_path=None):
pass
我不知道哪里出了问题或应该怎么做。我尝试阅读有关resolve()的django文档,并尝试了解resolve()的django源代码。我尝试在调试时强制在resolve()方法中为urlconf设置一个值,但是失败了。
请帮助。
PS:我已经读过this,并尝试过django.utils.translation.activate()和django.utils.translation.trans_real.activate()都没有运气。我猜这并不适用,或者Django版本之间有很大差异。