我是Django的新手,開始學習。我想將導航欄添加到不同的頁面,如[菜單],[主頁],[聯繫人]。Django導航欄
我該如何使用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>
下面ismy 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'),
]` 下面是我的views.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')
下面是錯誤的,我得到 反轉e用於'polls/detail'參數'()'和關鍵字參數'{}'未找到。嘗試了0個模式:[]
你有什麼問題? –
它給了我錯誤沒有反向網址 – shankar
請求網址:\t http://127.0.0.1:8080/static/polls/index 'polls \ index'找不到 – shankar