Marco Fernandes
Marco Fernandes

Reputation: 553

How to add a new view to django-oscar

I don't think I fully grasp the django-oscar documentation. I am trying to add a new view, a home view at / of the site. But whatever I seem to do it keeps going to /catalogue/ when I want to access / instead.

it says I should do the following:

from oscar.apps.offer.apps import OfferConfig as CoreOfferConfig
from .views import IndexView
from django.conf.urls import url


class OfferConfig(CoreOfferConfig):
    def ready(self):
        super().ready()
        self.index_view = IndexView

    def get_urls(self):
        urls = super().get_urls()
        urls += [
            url(r'^$', self.index_view.as_view(), name='index'),
        ]
        return self.post_process_urls(urls)

and this is in myproject/myapp/offer/apps.py. myapp was created following the django-oscar tutorial which involved running the command ./manage.py oscar_fork_app order myapp.

general breakdown of the folder:

myproject:
    - myproject
        - settings.py
        ...
    - static
    - myapp
        - order
        - offer
            - apps.py
        - __init.py
    - templates
    manage.py

my urls.py in myproject looks as follows:

from django.contrib import admin
from django.urls import path, include
from django.conf.urls import url
from django.conf.urls.static import static
from django.conf import settings
from django.apps import apps

urlpatterns = [
    path('i18n/', include('django.conf.urls.i18n')),
    url(r'^', include(apps.get_app_config('oscar').urls[0])),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

the view I am trying to add is very very basic at the moment:

from django.views.generic import TemplateView


class IndexView(TemplateView):
    template_name = "pages/index.html"

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        products = ['empy', 'for', 'now']
        context.update({
            'products': products,
        })
        return context

what am I doing wrong?

I am using django-oscar 2.0.4,django 2.2.12, python 3.7.4

Upvotes: 3

Views: 453

Answers (2)

solarissmoke
solarissmoke

Reputation: 31404

If it's just the index view you want to override, then it may be easier just to insert a URL pattern in from of Oscar's patterns:

urlpatterns = [
    path('i18n/', include('django.conf.urls.i18n')),
    path('', IndexView.as_view()),
    url(r'^', include(apps.get_app_config('oscar').urls[0])),
]

This will cause your IndexView to match ahead of Oscar's.

Alternatively, you need to override the catalogue app where the home page view is defined. In your question you have forked the offer app, which doesn't contain that view and hence you changes have no effect. If you take that approach then the correct way to override the view is to set self.catalogue_view in the ready() method of the app, rather than adding a new URL pattern:

class CatalogueConfig(CoreCatalogueConfig):
    def ready(self):
        super().ready()
        # This is all - you don't need to mess with the URLs.
        self.catalogue_view = IndexView

Upvotes: 3

pythonoscar
pythonoscar

Reputation: 29

index is in : oscar/apps/catalogue/apps.py

you have to fork catalogue

Upvotes: 0

Related Questions