Django Oscar更改URL模式

时间:2019-02-08 09:51:06

标签: django django-oscar

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

根据我在app.py中添加了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时会得到

  

/ catalog /“客户”处的NoReverseMatch不是注册的名称空间。

预期输出为localhost:8000/catalog,应返回目录页面。

3 个答案:

答案 0 :(得分:4)

您可以尝试

在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):
        urls = [
            url(r'^catalog/', include(self.catalogue_app.urls)),
            # all the remaining URLs, removed for simplicity
            # ...
        ]
        urls = urls + super(MyShop,self).get_urls()
        return urls


application = MyShop()

并在您的urls.py中 您只需添加

from myproject.app import application as shop

 url(r'', shop.urls),

希望对您有帮助

答案 1 :(得分:2)

c.grey's answer上扩展以指定替换而不是添加网址的方法-

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()

答案 2 :(得分:0)

您需要包括URL,而不是直接引用它们。

foo