Home:ALL Converter>Django-oscar : how to change the URL from root APP?

Django-oscar : how to change the URL from root APP?

Ask Time:2019-09-10T19:20:13         Author:Mairkur

Json Formatter

I'm developing an ecommerce website using django-oscar 2.0.1.

I would like to change the url to access catalogue (as described here in the documentation), but I always get this error :

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

I know I'm not the only one who get this error (Django Oscar change URL pattern) but the solution proposed is this post doesn't work for me (perhaps because django-oscar version was 1.6.5 at this time ?).

So, here is the step I followed based on django-oscar documentation :

  • I created a new project in a python venv with django-oscar and all the dependencies.
  • I forked the catalogue app
python manage.py oscar_fork_app catalogue forkedApps
  • Edited the installed Apps
    #'oscar.apps.catalogue',
    'forkedApps.catalogue.apps.CatalogueConfig',
    'oscar.apps.catalogue.reviews',

Here is the structure of the project :

project
    > forkedApps
        > catalogue
            . __init__.py
            . admin.py
            . apps.py
            . models.py
    > project
        . __init__.py
        . settings.py
        . urls.py
        . wsgi.py
  • I tried to change catalogue base URL from /catalogue to /cata :

Forked catalogue

    # forkedApps/catalogue/apps.py

    import oscar.apps.catalogue.apps as apps
    from oscar import config
    from django.conf.urls import url

    from django.views.generic.base import RedirectView
    from django.urls import reverse_lazy

    class CatalogueConfig(apps.CatalogueConfig):
        name = 'forkedApps.catalogue'

    class MyShop(config.Shop):

        # Override get_urls method
        def get_urls(self):
            from django.contrib.auth import views as auth_views

            from oscar.views.decorators import login_forbidden
            urlpatterns = [
                url(r'^cata/', self.catalogue_app.urls),
                url(r'^$', RedirectView.as_view(url=reverse_lazy('catalogue:index')), name='home'),
                url(r'^basket/', self.basket_app.urls),
                #...all other urls
            ]
            return urlpatterns

Project init

    #project/project/__init__.py

    default_app_config = 'forkedApps.catalogue.apps.MyShop'

Project url

    #project/project/urls.py

    from django.apps import apps
    from django.urls import include, path
    from django.contrib import admin

    urlpatterns = [
        path('i18n/', include('django.conf.urls.i18n')),

        path('admin/', admin.site.urls),
        #path('', include(apps.get_app_config('oscar').urls[0])),
        path('', include(apps.get_app_config('catalogue').urls[0])),
        ### Does catalogue refers to my forked catalogue ? ###
    ]

And when I run the server I got this error :

raise NoReverseMatch("%s is not a registered namespace" % key) django.urls.exceptions.NoReverseMatch: 'customer' is not a registered namespace

Do you have any idea about what I missed ?

SOLUTION :

I created an APP called 'oscar_url' (arbitrary name) and I added it to INSTALLED_APPS

oscar_url init

    #project/oscar_url/__init__.py

    default_app_config = 'oscar_url.apps.MyShop'

oscar_url apps

    #project/oscar_url/apps.py
    import oscar.config as apps
    from django.views.generic.base import RedirectView
    from django.urls import reverse_lazy
    from django.conf.urls import url

    class MyShop(apps.Shop):
        name = 'oscar_url'

        def get_urls(self):
            from django.contrib.auth import views as auth_views

            from oscar.views.decorators import login_forbidden
            urlpatterns = [
                url(r'^cata/', self.catalogue_app.urls),
                url(r'^$', RedirectView.as_view(url=reverse_lazy('catalogue:index')), name='home'),
                url(r'^basket/', self.basket_app.urls),
                url(r'^checkout/', self.checkout_app.urls),
                #all others urls...
            ]
            return urlpatterns

And I modified the project urls :

Project url

    #project/project/urls.py

    urlpatterns = [
        path('i18n/', include('django.conf.urls.i18n')),

        path('admin/', admin.site.urls),

        #path('', include(apps.get_app_config('oscar').urls[0])),
        path('', include(apps.get_app_config('oscar_url').urls[0])),
    ]

Author:Mairkur,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57869711/django-oscar-how-to-change-the-url-from-root-app
yy