我正在使用管理面板创建子页面。然后我使用循环将它们添加到主菜单。它们仅显示在主页上。如果我切换到其中之一,它们将不再显示在菜单中(与语言前缀相同)。
所以可以说我们有带菜单的home.html:
首页|关于| createdsubpage1 | createdsubpage2 | EN | DE | PL
然后我转到'createdsubpage1'或任何其他页面,并且仅在菜单左侧:
首页|关于
一些视图:
def index(request, *args, **kwargs):
multilanguage = Multilanguage.objects.all()
sub_links = Subpage.objects.all()
return render(request, 'home.html', {'multilanguage':multilanguage, 'sub_links':sub_links})
def generated_page(request, slug):
unique_subpage = get_object_or_404(Subpage, slug=slug)
sub = Subpage.objects.get(slug=slug)
if sub.is_active:
context = {
'unique_subpage': unique_subpage,
}
return render(request, 'subpage.html', context)
else:
return render(request, '404.html', {'sub':sub})
应用网址:
urlpatterns = [
path('', views.index, name='index'),
path('o-nas', views.about, name='about'),
path('oferta', views.offer, name='offer'),
path('kontakt', views.contact, name='contact'),
path('subpage', views.subpage, name='subpage'),
path('<slug:slug>', views.generated_page, name='generated_page'),
]
header.html:
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li><a href="{% url 'index' %}">{% trans 'Strona główna' %}</a></li>
<li><a href="{% url 'offer' %}">{% trans 'Oferta' %}</a></li>
<li><a href="{% url 'about' %}">{% trans 'O Nas' %}</a></li>
{% for sub in sub_links %}
{% if sub.is_active %}
<li><a href="{% url 'generated_page' sub.slug %}">{% trans sub.title %}</a></li>
{% endif %}
{% endfor %}
<li><a href="{% url 'contact' %}">{% trans 'Kontakt' %}</a></li>
<li>
<ul>
<li><a href="{{page_url}}">PL</a></li>
{% for lang in multilanguage %}
{% if lang.multilanguage_marker %}
<li><a href="{{site_identity.site_url}}/{{lang.multilanguage_prefix}}">{{lang.multilanguage_prefix|upper}}</a></li>
{% endif %}
{% endfor %}
</ul>
</li>
</ul>
项目网址:
urlpatterns = [
path(r'^i18n/', include('django.conf.urls.i18n')),
path('admin/', admin.site.urls),
path('ckeditor/', include('ckeditor_uploader.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += i18n_patterns(
path('', include('pages.urls')),
prefix_default_language=False,
)