我不认为我完全掌握了django-oscar文档。我正在尝试添加一个新的视图,在网站的/主页视图。但无论我做什么,当我想要访问/时,它总是转到/catalogue/。
它说我应该做以下事情:
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)这是myproject/myapp/offer/apps.py格式的。myapp是在django-oscar教程之后创建的,该教程涉及运行命令./manage.py oscar_fork_app order myapp。
文件夹的一般明细:
myproject:
- myproject
- settings.py
...
- static
- myapp
- order
- offer
- apps.py
- __init.py
- templates
manage.py我在myproject中的urls.py如下所示:
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)目前,我试图添加的视图非常非常基础:
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我做错了什么?
我正在使用django-oscar 2.0.4、django 2.2.12、python 3.7.4
发布于 2020-05-26 12:40:56
如果只是想要覆盖的索引视图,那么只需在Oscar的模式中插入URL模式可能会更容易:
urlpatterns = [
path('i18n/', include('django.conf.urls.i18n')),
path('', IndexView.as_view()),
url(r'^', include(apps.get_app_config('oscar').urls[0])),
]这将导致您的IndexView匹配在Oscar之前。
或者,您需要覆盖catalogue应用程序where the home page view is defined。在您的问题中,您已经派生了offer应用程序,该应用程序不包含该视图,因此您所做的更改不会生效。如果您采用这种方法,那么覆盖视图的正确方法是在应用程序的ready()方法中设置self.catalogue_view,而不是添加新的URL模式:
class CatalogueConfig(CoreCatalogueConfig):
def ready(self):
super().ready()
# This is all - you don't need to mess with the URLs.
self.catalogue_view = IndexView发布于 2020-05-26 08:16:36
索引位于: oscar/apps/catalogue/apps.py
你必须分叉目录
https://stackoverflow.com/questions/61975513
复制相似问题