【发布时间】:2025-11-28 17:40:01
【问题描述】:
我认为我没有完全掌握 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
【问题讨论】:
标签: django python-3.x django-views django-urls django-oscar