很抱歉,这可能很简单,但是我学习django并遇到问题。我的导航栏在索引页面上工作非常完美。但是当我联系表单或其他帖子网址时,导航栏链接会消失。
当我进入某个帖子页面或联系表单页面时
1- {{article.title}}和href链接消失
我可以寻求帮助吗?
我的navbar.html
{% load i18n %}
<!-- Menu -->
<div class="menu-wrapper center-relative">
<nav id="header-main-menu">
<div class="mob-menu">Menu</div>
<ul class="main-menu sm sm-clean">
<li><a href="{% url "index" %}">{% trans "HomePage" %}</a></li>
<li><a href="#services">{% trans "Services" %}</a></li>
<li>
{% for article in articles %}
{% if article.slug == 'about_us' %}
<a href="{% url 'article:detail' slug=article.slug %}">
{{ article.title }}</a>
{% endif %}
{% endfor %}
</li>
<li><a href="#video">{% trans "HELLO WORLD" %}</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-1643 dropdown">
<a title="" href="">{% trans "Producs" %}</a>
<ul role="menu" class=" dropdown-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-1644 dropdown">
<a title="Level 2" href="">{% trans "Consult" %}</a>
<ul role="menu" class=" dropdown-menu">
<li >
{% for category in category %}
{% if category.name == 'consult' %}
{% for article in category.get_article %}
<a title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a>
{% endfor %}
{% endif %}
{% endfor %}</li>
</ul>
</li>
<li>
{% for category in category %}
{% if category.name == 'header' %}
{% for article in category.get_article %}
<a title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a>
{% endfor %}
{% endif %}
{% endfor %}
</li>
</ul>
</li>
<li><a href="{% url 'contact_form' %}">{% trans "İletişim" %}</a></li>
</ul>
</nav>
</div>
article / views.py
from django.shortcuts import render, get_object_or_404
from .models import Article, Category
from django.core.paginator import Paginator
from django.utils.translation import gettext as _
# Create your views here.
def articles(request):
keyword = request.GET.get("keyword")
if keyword:
articles = Article.objects.filter(title__contains=keyword)
paginator = Paginator(articles, 1)
page = request.GET.get('page')
articles = paginator.get_page(page)
return render(request,"articles.html",{"articles":articles})
articles = Article.objects.all()
paginator = Paginator(articles, 10)
page = request.GET.get('page')
articles = paginator.get_page(page)
return render(request, "articles.html", {"articles":articles})
def index(request):
articles = Article.objects.all()
category = Category.objects.all()
context = {
"articles": articles,
"category": category,
}
return render(request, 'index.html', context)
def detail(request,slug):
# article = Article.objects.filter (id = id).first()
article = get_object_or_404(Article, slug = slug)
category = Category.objects.all()
return render(request, "detail.html", {"article":article, "category":category,})
def category_detail(request,slug):
template = "category_detail.html"
category=get_object_or_404(Category,slug=slug)
article=Article.objects.filter(category=category)
context = {
'category' : category,
'article' : article,
}
return render(request,template,context)
def category_page(request):
object_list = Category.objects.all()
context = {'object_list': object_list,}
return render(request, 'detail.html', context)
contact_form / views.py
from django.views.generic.edit import FormView
from .forms import ContactForm
try:
from django.urls import reverse
except ImportError: # pragma: no cover
from django.core.urlresolvers import reverse # pragma: no cover
class ContactFormView(FormView):
form_class = ContactForm
recipient_list = None
template_name = 'contact_form/contact_form.html'
def form_valid(self, form):
form.save()
return super(ContactFormView, self).form_valid(form)
def get_form_kwargs(self):
if self.recipient_list is not None:
kwargs.update({'recipient_list': self.recipient_list})
return kwargs
def get_success_url(self):
return reverse('contact_form_sent')