【问题标题】:Navigation bar in DjangoDjango中的导航栏
【发布时间】:2016-08-25 16:38:24
【问题描述】:

我是 Django 新手并开始学习。我想在 [Menu]、[Home]、[Contact] 等不同页面中添加导航栏。

如何使用 Django 模板来做到这一点? 我的模板的一部分。

    <nav>
        <a href="{% url 'polls/detail' %}">Detail</a>
        <a href ="{% url 'polls/index'%}">Home</a>
        <a href ="{%url 'polls/contact'%}">Contact</a>
    </nav>

</header>
<section>
    {% if latest_question_list %}
    <ul id="choice">
        {% for question in latest_question_list %}
            <li><a href="/polls/{{ question.id }}/">{{ question.question_test }}</a></li>
        {%  endfor %}

    </ul>

下面是我的 url.conf`

    patterns = [
    url(r'^index/$',views.index,name='index'),

    url(r'^(?P<question_id>[0-9]+)/$',views.detail,name='detail'),
    url(r'^(?P<question_id>[0-9]+)/result/$',views.result,name='result'),
    url(r'^(?P<question_id>[0-9]+)/vote/$',views.vote,name='vote'),
    url(r'^contact/$',views.contact,name='contact'),

]` 以下是我的观点.py

from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse ,Http404,HttpResponseRedirect
from .models import Question ,Choice
from django.core.urlresolvers import reverse
# Create your views here.


def index(request):
    try:
        latest_question_list = Question.objects.all()
    except:
        raise Http404("Question does not exist")
    return render(request, 'polls/index.html', context=  {'latest_question_list': latest_question_list})



def detail(request,question_id):
    try:
        question = Question.objects.get(pk=question_id)
        return render(request,'polls/detail.html', context=  {'question':question})
    except Question.DoesNotExist:
        raise Http404("Question does not exists")




def vote(request, question_id):
    question = Question.objects.get(pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except(KeyError,Choice.DoesNotExist):
        return render(request,'polls/detail.html',{
        'question':question,
        'error_message':'You did not select a choice'
    })
    else:
        selected_choice.votes+=1
        selected_choice.save()
        return HttpResponseRedirect(reverse('result',args=(question_id)))



def result(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/result.html', {'question': question})

def contact(request):
    return render(request,'polls/contact.html')

下面是错误,我得到了 找不到带有参数“()”和关键字参数“{}”的“民意调查/详细信息”。尝试了 0 个模式:[]

【问题讨论】:

  • 你有什么问题?
  • 它给了我类似没有反向网址的错误
  • 请求网址:127.0.0.1:8080/static/polls/index 'polls\index' 找不到
  • 你使用了错误的静态和 url 标签。老实说,你最好做教程

标签: django django-templates django-views


【解决方案1】:

尝试在 urls.py 中为 URL 分配一个名称,然后在模板中通过它们的名称调用它们。

urls.py:

urlpatterns = [
    url(r'^/polls/about/', views.about.as_view(), name='about'),
    url(r'^/polls/contact/', views.contact.as_view(), name='contact'),
    ...
]

模板: {% url 'about' %}

有关详细信息,请参阅文档:

https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#url

https://docs.djangoproject.com/ja/1.9/topics/http/urls/#url-namespaces

更新的答案: 您的详细信息、投票和结果页面都指向您的问题模型的特定实例。这由 urls.py 中的 (?P&lt;question_id&gt;[0-9]+) 行显示,并作为视图函数变量的一部分。

您需要将视图上下文字典中的有效 question_id 传递给您的模板,然后将该 question_id 包含在您的 {% url %} 标记中。

例如,如果您想在索引页面上添加指向第一个问题详细信息的链接,则需要进行以下更改:

在views.py中:

def index(request):
    try:
        latest_question_list = Question.objects.all()
        question = latest_question_list[0]
    except:
        raise Http404("Question does not exist")
    return render(request, 'polls/index.html', context ={'latest_question_list': latest_question_list, 'question':question})

然后在您的模板中,您可以链接到此特定问题的详细信息,如下所示:

<a href="{% url 'detail' question_id=question.id %}">Detail</a>

【讨论】:

  • NoReverseMatch at /polls/index/ Reverse for 'detail' with arguments '()' 和关键字参数 '{}' 未找到。尝试了 1 种模式:['polls/(?P[0-9]+)/$'] 请求方法:GET 请求 URL:127.0.0.1:8080/polls/index Django 版本:1.9.4 异常类型:NoReverseMatch 异常值:未找到带有参数“()”和关键字参数“{}”的“详细信息”。尝试了 1 种模式:['polls/(?P[0-9]+)/$']
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
  • 2017-08-31
  • 2015-03-05
  • 2017-04-13
  • 2017-11-24
  • 2016-09-02
  • 1970-01-01
相关资源
最近更新 更多