Django导航栏内容链接消失

时间:2018-11-14 09:23:58

标签: django python-3.x

我卡住了我的django博客项目。

我创建了一个标准博客,但现在遇到了问题。当我在所有帖子中添加导航栏时,我会看到此帖子索引页面。但是当我转到另一个页面(如联系表单)时,导航栏帖子链接消失了。

让我解释一下; 我创建了三篇文章,并将这些文章添加到“音乐”类别中

然后我在导航栏中使用此代码;

 {% for category in category %}
    {% if category.name == 'Music' %}
       {% for article in category.get_article %}
          <li class="nav-item" > <a class="nav-link" title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a></li>
        {% endfor %}
    {% endif %}
{% endfor %}

所以我在这里要做的是将音乐类别中的文章标题带到菜单中。我不确定这是否是正确的方法,但是它可以在索引页面上工作。

在此之后,如果我进入联系页面,此链接将消失。 (也联系表单是一个单独的应用https://github.com/maru/django-contact-form-recaptcha。)

同一时间,我使用另一种方法,但是这些链接也消失了;

{% for article in articles %}
    {% if article.slug == 'about_us' %}
       <a href="{% url 'article:detail' slug=article.slug %}">
            {{ article.title }}</a>

     {% endif %}
       {% endfor %}

为什么消失此链接?我该如何解决?我可以寻求帮助吗?

我的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 == 'music' %}
       {% 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 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')

感谢帮助...

1 个答案:

答案 0 :(得分:2)

正如@dirkgroten所提到的,视图仅在该视图所服务的页面上管理上下文。

要在不同视图所服务的页面上各处都具有相同的项目,可以使用context_processor

您基本上可以采用index中的代码,使其返回context而不是渲染,并将其路径放入{{1}中context_processors的{​​{1}} }。 上下文处理器会加载每个请求,并且来自其返回字典的项目会自动包含在上下文中。

作为最佳实践,最好将上下文处理器保存在单独的文件中。

示例TEMPLATES可以与settings.pycontext_processors.py放在同一目录中:

views.py

models.py中找到from .models import Article, Category def navigation(request): articles = Article.objects.all() category = Category.objects.all() return { "articles": articles, "category": category, } 选项,该选项可能类似于 ```

settings.py

TEMPLATES之后添加TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ]

用文件所在目录的名称替换'APP_NAME.context_processors.navigation'