Django Oscar change URL pattern

2020-03-07 05:15发布

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

As per the documentation I've added app.py in 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'),
]

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

NoReverseMatch at /catalog/ 'customer' is not a registered namespace.

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

3条回答
beautiful°
2楼-- · 2020-03-07 05:59

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()
查看更多
你好瞎i
3楼-- · 2020-03-07 06:02

You need to include the URLs, not reference them directly.

url(r'', include('application.urls')),
查看更多
混吃等死
4楼-- · 2020-03-07 06:12

You can try this

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

And in your urls.py you can simply add this

from myproject.app import application as shop

 url(r'', shop.urls),

Hope it help for you

查看更多
登录 后发表回答