如何从 url 字符串中删除目录

How to remove catalogue from url string

使用此 url 结构 namedomain.com/ 而不是名称域的最佳方法是什么。com/catalogue 对于 Django Oscar。

我是否必须创建新的应用程序调用首页,或者我可以编辑 myshop/urls.py 这样的行吗?

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

您需要自定义用于 Oscar 的应用程序配置以修改它的 url,如 How to add views or change URLs or permissions [Oscar Docs]. Firstly inherit from oscar.config.Shop in some suitable file of yours and override its get_urls method. You can simply copy the urls from its source code [GitHub] 所示,甚至只需使用列表切片删除前两个 url 并在其位置添加您自己的 url:

from django.urls import path, reverse_lazy

from oscar import config


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
        
        urls = [
            # Removed the redirect
            path('', self.catalogue_app.urls), # modify this pattern so it is the homepage
            path('basket/', self.basket_app.urls),
            path('checkout/', self.checkout_app.urls),
            path('accounts/', self.customer_app.urls),
            path('search/', self.search_app.urls),
            path('dashboard/', self.dashboard_app.urls),
            path('offers/', self.offer_app.urls),

            # Password reset - as we're using Django's default view functions,
            # we can't namespace these urls as that prevents
            # the reverse function from working.
            path('password-reset/',
                login_forbidden(
                    auth_views.PasswordResetView.as_view(
                        form_class=self.password_reset_form,
                        success_url=reverse_lazy('password-reset-done'),
                        template_name='oscar/registration/password_reset_form.html'
                    )
                ),
                name='password-reset'),
            path('password-reset/done/',
                login_forbidden(auth_views.PasswordResetDoneView.as_view(
                    template_name='oscar/registration/password_reset_done.html'
                )),
                name='password-reset-done'),
            path('password-reset/confirm/<str:uidb64>/<str:token>/',
                login_forbidden(
                    auth_views.PasswordResetConfirmView.as_view(
                        form_class=self.set_password_form,
                        success_url=reverse_lazy('password-reset-complete'),
                        template_name='oscar/registration/password_reset_confirm.html'
                    )
                ),
                name='password-reset-confirm'),
            path('password-reset/complete/',
                login_forbidden(auth_views.PasswordResetCompleteView.as_view(
                    template_name='oscar/registration/password_reset_complete.html'
                )),
                name='password-reset-complete'),
        ]
        return urls

接下来在 INSTALLED_APPSoscar.config.Shop 将其替换为此自定义应用程序配置:

INSTALLED_APPS = [
    ...
    <s>'oscar.config.Shop',</s> # Remove this
    'path.to.MyShop', # Add this
    ...
]