2016-04-30 186 views
-2

我是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個模式:[]

+0

你有什麼問題? –

+0

它給了我錯誤沒有反向網址 – shankar

+0

請求網址:\t http://127.0.0.1:8080/static/polls/index 'polls \ index'找不到 – shankar

回答

0

嘗試爲您的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<question_id>[0-9]+)行以及視圖函數變量的一部分顯示。

您需要在您的視圖語境詞典傳遞一個有效的question_id到您的模板,然後在您的{%URL%}標記,question_id。

因此,舉例來說,如果你想增加你的索引頁上的鏈接的第一個問題的細節,你需要做以下修改:

在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> 
+0

NoReverseMatch在/民意調查/指數/ 反向'細節'的參數'()'和關鍵字參數'{}'未找到。嘗試1個模式:['polls /(?P [0-9] +)/ $'] 請求方法:\t GET 請求URL:\t http://127.0.0.1:8080/polls/index/ Django的版本:\t 1.9.4 異常類型: \t NoReverseMatch 異常值:\t 未找到參數'()'和關鍵字參數'{}'的'detail'的反轉。嘗試1種模式:['polls /(?P [0-9] +)/ $'] – shankar