2016-11-12 139 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中改变正则表达式 –