如何在Django-Oscar中实现页面内部重定向?无需修改urls.py与views.py
Got it! Since you want to avoid modifying urls.py or views.py in Django-Oscar, the cleanest approach is to override the navbar template to adjust the link's target directly. Here's a step-by-step breakdown:
- First, locate the default navbar template in Django-Oscar's core files. Typically, this lives at
oscar/templates/oscar/partials/navbar.html(the exact path might vary slightly based on your Oscar version, but it'll be under theoscar/partialstemplate directory). - In your Django project's
templatesfolder, create a matching directory structure to override the template:your_project/templates/oscar/partials/. - Copy the entire content of the original
navbar.htmlinto this new directory'snavbar.htmlfile. - Find the line that renders the "Product" link. It’ll look something like this:
<a href="{% url 'catalogue:index' %}" class="nav-link">Product</a> - Replace the
hrefvalue with the URL name of your Services page. For example, if your Services page uses the URL nameoscar:services(adjust this to match your actual URL name), update it to:<a href="{% url 'oscar:services' %}" class="nav-link">Product</a> - Save the modified template, restart your Django development server, and test the link—clicking "Product" will now take you straight to the Services page.
This method leverages Django-Oscar’s built-in template overriding system, which is the recommended way to customize the framework without altering core files like urls.py or views.py.
内容的提问来源于stack exchange,提问作者david johnson
相关产品推荐
相关产品推荐

