使用模式名称 + RedirectVew 从 Django 中的另一个应用程序调用 URL 名称

问题描述 投票:0回答:2

我正在研究一些 URL 路径的重定向。

我最终需要将一些路径移动到另一个应用程序。

我在调用模式名称以引用 RedirectView 中的其他 URL 时遇到问题。这是我尝试重定向的位于“交易”应用程序中的网址:

 path('<slug:slug>/', RedirectView.as_view(pattern_name='deal_detail', permanent=False))

注意,我还从引用的应用程序导入视图,如下所示:

from dealmazing.views import *

我希望它重定向到的新网址位于我的核心应用程序目录中,如下所示:

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

更新

将“dealmazing”添加到我的网址模式名称后,我的新网址可以正常工作——但有一个问题。

这是我的“交易”应用程序中的代码

path('<slug:slug>/', RedirectView.as_view(pattern_name='dealmazing:deal_detail', permanent=False)),

以及我的“dealmazing”应用程序中的 url 路径

path('<slug:slug>/', deal_by_detail, name='deal_detail'),

如果转到旧链接,我会收到

'dealmazing' is not a registered namespace
错误。请注意,我的 dealmazing urls 文件中没有设置 app_name 或命名空间。我尝试过,但仍然不起作用。这是我收到的错误:

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

这是我的交易网址文件:

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

和我的核心(dealmazing)网址文件:

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)
python django redirect
2个回答
1
投票

如果您在

app_name
中设置了
core/urls.py
或在包含 url 时设置了命名空间,那么您需要包含此内容。例如:

path('<slug:slug>/', RedirectView.as_view(pattern_name='core:deal_detail', permanent=False))

0
投票

我的解决方案,与文件夹的正确依赖关系相比

/My_Project
./apps
    ../api
    ../core
    ../other_app

    

在 api/apps.py 和其他 apps.py 中添加:

class CoreConfig(AppConfig):
    label='api'
    name = 'apps.api'

在 api/url 中

app_name='apps.api'

...
path('path/',api_views.ApiView.as_view(), name='some-view'),
...

在 My_project 网址中:

 path('base-path/',RedirectView.as_view(pattern_name='apps.api:some-view'), 
 name='redirect-view'),

在已安装应用程序的settings.py中

...
##  Local Apps
'apps.core',
'apps.api',
...
© www.soinside.com 2019 - 2024. All rights reserved.