2016-11-12 87 views
0

我正在嘗試一個簡單的事情用戶點擊顯示的問題。點擊時的鏈接將接收問題ID並將其作爲參數傳遞以顯示相關的選擇。用戶被要求選擇一個選擇,然後下一頁將顯示有多少人投票選擇了該選項(包括用戶當前的決定)。 我得到的錯誤是當我使用127.0.0.1:8000/polls/啓動應用程序時,它顯示了問題。然後,當我點擊這個問題的網址變成127.0.0.1:8000/polls/3/這是正確的,因爲3是問題ID。因此預計會顯示問題ID的選項。但它沒有顯示。 錯誤是:如何將Django模板中的多個參數傳遞給通過URL查看?

NoReverseMatch at /polls/3/ 

Reverse for 'results' with arguments '()' and keyword arguments '{'q_id': 3, 'c_test': 'May Be'}' not found. 1 pattern(s) tried: ['polls/(?P<q_id>[0-9]+)/(?P<c_test>\\w+)/results/$'] 

Request Method:  GET 
Request URL: http://127.0.0.1:8000/polls/3/ 
Django Version:  1.10.3 
Exception Type:  NoReverseMatch 
Exception Value:  

Reverse for 'results' with arguments '()' and keyword arguments '{'q_id': 3, 'c_test': 'May Be'}' not found. 1 pattern(s) tried: ['polls/(?P<q_id>[0-9]+)/(?P<c_test>\\w+)/results/$'] 

Exception Location:  /home/usr/.local/lib/python3.5/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 392 
Python Executable: /usr/bin/python3.5 
Python Version:  3.5.2 

我的代碼是:

views.py

class IndexView(generic.ListView): 
    template_name = "polls/index.html" 
    context_object_name = "latest_question_list" 
    model = Question 

def get_queryset(self): 
    return Question.objects.filter(
     pub_date__lte=timezone.now() 
    ).order_by('-pub_date')[:5] 


class DetailView(ListView): 
    model = Choice 
    context_object_name = "latest_choice_list" 
    template_name = "polls/detail.html" 

def get_queryset(self): 
    print(self.args[0]) 
    '''Return list of choices''' 
    return Choice.objects.filter(question_id=self.args[0]) 
    # return Choice.objects.all() 

def pollvote(request, q_id, c_test): 

if c_test: 
    p = Choice.objects.filter(question_id=q_id).get(choice_test=c_test) 
    count = p.votes 
    count += 1 
    p.votes = count 
    p.save() 

return HttpResponseRedirect('/polls/%s/%s/results/' % c_test, q_id) 

detail.html

{% load staticfiles %} 
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" /> 
{% if latest_choice_list %} 
    <p>Cast your Choice</p> 
<ul> 


    {% for choice in latest_choice_list %} 
      <li><a href="{% url 'polls:results' q_id=choice.question_id c_test=choice.choice_test%}">{{ choice.choice_test }}</a></li> 


    {% endfor %} 
</ul> 


{% else %} 
<p>No choice are available.</p> 
{% endif %} 

結果(誤差在href行說的問題) .html:

{% load staticfiles %} 
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" /> 
{% if latest_choice_list %} 
    <p>Choices Made So Far</p> 
<ul> 
    {% for choice in latest_choice_list %} 
    <li>{{ choice.choice_test }} - {{ choice.votes }} </li> 
    {% endfor %} 
</ul> 

{% else %} 
<p>No choice are available.</p> 
{% endif %} 

urls.py

urlpatterns = [ 
url(r'^$', views.IndexView.as_view(), name='index'), 
url(r'^([0-9]+)/$', views.DetailView.as_view(), name='detail'),  
url(r'^(?P<q_id>[0-9]+)/(?P<c_test>\w+)/results/$', views.pollvote, name='results'),] 

爲什麼detail.html拋出錯誤?爲什麼不把兩個名爲arguement的關鍵字傳遞給結果呢?

+0

也許這是Ctrl + V的問題,但view.py中的縮進顯然是不正確的 – svfat

+0

沒有在真實代碼中是正確的。在發佈主題時,這是一個複製粘貼問題。 – debayan89

回答

0

試試這個:

<a href="{% url 'polls:results' choice.question_id choice.choice_test%}">{{ choice.choice_test }}</a> 

Django會自動分配ARGS在給定的順序。

P.S.

+0

嗨,我做了你所說的。但仍然是一樣的。唯一改變的是現在它說 反向'結果'與參數'(3,'五月是')'和關鍵字參數'{}'找不到。嘗試1個模式:['polls /(?P [0-9] +)/(?P \\ w +)/ results/$'] 而不是關鍵字參數,它現在僅用於參數。而仍然SYS錯誤detail.html 而我的選擇表已經得到了兩個領域。 (「ID」))的數據類型的數據類型的數據類型的數據類型。 ; – debayan89

+0

我沒有看過你的評論,但我想\ w只匹配字符和下劃線。它不符合空間。所以在urls.py中改變正則表達式 –

相關問題