本文介绍了Django Oscar更改URL模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了django-oscar项目,并且正在尝试配置URL。我的目标是将 /目录更改为 /目录

I have setup a django-oscar project and I'm trying to configure the URLs. My goal is to change /catalogue to /catalog.

根据文档,我在 myproject / app.py

myproject / app.py

from django.conf.urls import url, include
from oscar import app


class MyShop(app.Shop):
    # Override get_urls method
    def get_urls(self):
        urlpatterns = [
            url(r'^catalog/', include(self.catalogue_app.urls)),
            # all the remaining URLs, removed for simplicity
            # ...
        ]
        return urlpatterns


application = MyShop()

myproject / urls.py

from django.conf.urls import url, include
from django.contrib import admin
from . import views
from .app import application

urlpatterns = [
    url(r'^i18n/', include('django.conf.urls.i18n')),

    url(r'^admin/', admin.site.urls),

    url(r'', application.urls),

    url(r'^index/$',views.index, name = 'index'),
]

项目服务器运行无任何错误,但是当我尝试 localhost:8000 / catalog 时,我得到

The project server runs without any error, but when I try localhost:8000/catalog I get

NoReverseMatch不是注册的名称空间。

预期的输出是 localhost:8000 / catalog 应该返回目录页面。

The expected output is localhost:8000/catalog should return the catalogue page.

推荐答案

扩展在上指定如何替换而不是添加网址-

Expanding on c.grey's answer to specify how to replace instead of add the urls -

from django.conf.urls import url, include
from oscar import app


class MyShop(app.Shop):
    def get_urls(self):
        urls = super(MyShop, self).get_urls()
        for index, u in enumerate(urls):
            if u.regex.pattern == r'^catalogue/':
                urls[index] = url(r'^catalog/', include(self.catalogue_app.urls))
                break
        return urls


application = MyShop()

这篇关于Django Oscar更改URL模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 20:40