簡體   English   中英

如何通過URL將Django模板中的多個論點傳遞給View?

[英]How can I pass multiple arguements from Django Template to View via URL?

我正在嘗試一件簡單的事情用戶單擊顯示的問題。 單擊該鏈接時,將輸入問題ID,並將其作為自變量傳遞以顯示相關的選擇。 要求用戶選擇一個選項,然后下一頁將顯示有多少人對該選項進行了投票(包括用戶當前的決定)。 我收到的錯誤是當我使用127.0.0.1:8000/polls/啟動應用程序時,它顯示了問題。 然后,當我單擊問題時,URL變為127.0.0.1:8000/polls/3/,這是正確的,因為3是問題ID。 因此,預計將顯示問題ID的選擇。 但是它沒有顯示。 錯誤是:

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(錯誤表示問題在href行上)

{% 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 %}

results.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拋出錯誤? 為什么不采用名為爭論的兩個關鍵字並將其傳遞給結果?

嘗試這個:

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

Django將按照給定的順序自動分配args。

聚苯乙烯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM