我正在为一些URL路径进行重定向。
我最终需要将一些路径移动到另一个应用程序。
我在调用模式名称以引用重定向视图中的其他URL时遇到问题。以下是我试图重定向的url,它位于“deals”应用程序中:
path('<slug:slug>/', RedirectView.as_view(pattern_name='deal_detail', permanent=False))
注意,我还从引用的应用程序导入视图,如下所示:
from dealmazing.views import *
我希望重定向到的新url位于我的核心应用程序目录中,如下所示:
path('<slug:slug>/', deal_by_detail, name='deal_detail'),
问题是我遇到了以下错误:
Reverse for 'deal_detail' not found. 'deal_detail' is not a valid view function or pattern name.
是否无法引用外部pattern\u名称?
使现代化
在我的URL pattern\u名称中添加“Deamazing”后,我的新URL正常工作,但有一个问题。
以下是我的“交易”应用程序中的代码
path('<slug:slug>/', RedirectView.as_view(pattern_name='dealmazing:deal_detail', permanent=False)),
以及我的“Deamazing”应用程序中的url路径
path('<slug:slug>/', deal_by_detail, name='deal_detail'),
如果转到旧链接,我会得到
'dealmazing' is not a registered namespace
错误注意,在我的Deamazing URL文件中没有设置app\u名称或命名空间。我试过了,但还是不起作用。这是我得到的错误:
Traceback:
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\base.py" in reverse
74. extra, resolver = resolver.namespace_dict[ns]
During handling of the above exception ('dealmazing'), another exception occurred:
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\base.py" in get
180. url = self.get_redirect_url(*args, **kwargs)
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\base.py" in get_redirect_url
170. url = reverse(self.pattern_name, args=args, kwargs=kwargs)
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\base.py" in reverse
84. raise NoReverseMatch("%s is not a registered namespace" % key)
Exception Type: NoReverseMatch at /deals/steak-fries-beer-only-999-walkabout-wednesday/
Exception Value: 'dealmazing' is not a registered namespace
这是我的交易URL文件:
from django.urls import include, path
from django.contrib import admin
from django.views.generic.base import RedirectView
from .views import *
from dealmazing.urls import *
from dealmazing.views import *
from deals.views import DealListView
from django.conf import settings
app_name = "deals"
urlpatterns = [
path('', Home.as_view(), name="deals"),
path('latest-deals', DealListView.as_view(), name="latest-deals"),
path('hot-deals', DealHotView.as_view(), name="hot-deals"),
path('results/', search, name='deal-search'),
path('category/<str:category>', RedirectView.as_view(pattern_name='dealmazing:category', permanent=False)),
path('<slug:slug>/', RedirectView.as_view(pattern_name='dealmazing:deal_detail', permanent=False)),
path('<int:pk>/like', like, name='like'),
path('<int:pk>/favorite', favorite, name='favorite'),
path('<int:pk>/remove_favorite', remove_favorite, name='remove_favorite'),
]
和我的核心(Deamazing)URL文件:
from django.conf.urls import url, include
from django.contrib.sitemaps.views import sitemap
from django.views.generic import TemplateView
from .sitemaps import *
from django.urls import path
from django.contrib import admin
from .views import *
from django.conf import settings
from deals.models import Deal
from deals.views import *
from django.conf.urls.static import static
sitemaps = {
'static': StaticViewSitemap,
'blog': BlogSitemap,
'blog-category': BlogCategorySitemap,
'deals': DealSitemap,
'deals-category': DealCategorySitemap,
'retailers': RetailerSitemap
}
urlpatterns = [
url(r'^$', Home.as_view(), name="home"),
url(r'^oauth/', include('social_django.urls', namespace='social')),
url(r'^admin/', admin.site.urls),
url(r'^blog/', include("blog.urls", namespace="blog")),
url(r'^accounts/', include("accounts.urls", namespace="accounts")),
url(r'^about/', about, name="about"),
url(r'^contact/', contact, name="contact"),
url(r'^disclosure/', disclosure, name="disclosure"),
url(r'^terms/', terms, name="terms"),
url(r'^privacy/', privacy, name="privacy"),
url(r'^submit_deal/', submit_deal, name="submit_deal"),
url(r'^thanks/', thanks, name="thanks"),
url(r"^deals/", include("deals.urls", namespace="deals")),
path('<slug:slug>/', deal_by_detail, name='deal_detail'),
path('deals/<slug:slug>', deals_by_retailer, name='retailer'),
path('category/<str:category>', deals_by_category, name='category'),
url(r"^newsletter/", include("newsletters.urls", namespace="newsletter")),
url(r'^ckeditor/', include('ckeditor_uploader.urls')),
url(r'^robots.txt$', TemplateView.as_view(template_name="robots.txt", content_type="text/plain"), name="robots_file"),
path('sitemap.xml', sitemap,
{'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
path('', include('django.contrib.auth.urls')),
]
if settings.DEBUG:
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)