Django 1.11 django.urls.exceptions.NoReverseMatch:

时间:2017-07-14 03:44:08

标签: python django frameworks django-views

我一直试图找到一个像我想要的相同的问题,但这个问题看起来并不像我想要的那样。我仍然开始学习Django,Python for Framework。我跟随django文档中的tutoial,当我尝试了解Generic View时,我陷入困境。我会展示我的代码:

urls.py

from django.conf.urls import url

from mulai.views import IndexView, DetailView, ResultsView, votes

app_name = "start"
urlpatterns = [
    url(r'^$', IndexView.as_view(), name='index'),
    url(r'^(?P<pk>[0-9]+)/$', DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>[0-9]+)/results/$', ResultsView.as_view(), name='results'),
    url(r'^(?P<choice_question_id>[0-9]+)/votes/$', votes, name='votes')

]

模板/ detail.html

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'start:votes' question_list.id %}" method="post">
{% csrf_token %}
{% for choice in question_list %}
    <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label>
    <br/>
{% endfor %}
<input type="submit" value="vote">
</form>

view.py

class DetailView(generic.DetailView):
    model = Question
    template_name = 'mulai/detail.html'

def votes(request, choice_pertanyaan_id):
    # return HttpResponse("votes of the question : %s." % choice_pertanyaan_id)
    question_vote = get_object_or_404(Question, pk=choice_pertanyaan_id)

    try:
        click_choice = question_vote.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        return render(request, 'mulai/detail.html', {
            'question_vote': question_vote,
            'pesan_error': "You must select one of them choice.",
        })

    else:
        click_choice.choice_vote += 1
        click_choice.save()
        return HttpResponseRedirect(reverse('practice:results', args=(question_vote.id,)))

我从detail.html得到的错误,错误是:

django.urls.exceptions.NoReverseMatch: Reverse for 'votes' not found. 'votes' is not a valid view function or pattern name.

树文件夹项目:

├── db.sqlite3
├── manage.py
├── mulai
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       ├── 0001_initial.cpython-36.pyc
│   │       └── __init__.cpython-36.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-36.pyc
│   │   ├── apps.cpython-36.pyc
│   │   ├── __init__.cpython-36.pyc
│   │   ├── models.cpython-36.pyc
│   │   ├── tests.cpython-36.pyc
│   │   ├── urls.cpython-36.pyc
│   │   └── views.cpython-36.pyc
│   ├── templates
│   │   └── mulai
│   │       ├── detail.html
│   │       ├── index.html
│   │       └── results.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
└── start
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-36.pyc
    │   ├── settings.cpython-36.pyc
    │   ├── urls.cpython-36.pyc
    │   └── wsgi.cpython-36.pyc
    ├── settings.py
    ├── urls.py
    └── wsgi.py

3 个答案:

答案 0 :(得分:0)

这一行:

url(r'^(?P<choice_question_id>[0-9]+)/votes/$', votes, name='votes')

对应

<form action="{% url 'start:votes' question_list.id %}" method="post">

正确的行动应该是:

{% url 'start:votes' choice_question_id=question_list.id %}

答案 1 :(得分:0)

您没有足够密切关注the tutorial。在详细信息视图中,您没有变量question_list,而是question。要获得选择,请循环遍历问题的选择集。

<form action="{% url 'start:votes' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
...

请注意,choice_pertanyaan_id视图中的参数votes与您的网址格式中的choice_question_id不符。最好在教程中使用question_id

答案 2 :(得分:0)

我假设您的项目名称已经开始。正确吗?

简化这将是您文件的正确结构(尤其是url.py和您的观点)

├── manage.py
└── start
   ├── settings.py
   ├── urls.py
├── mulai
   ├── admin.py
   ├── models.py
   ├── templates
   ├── urls.py
   └── views.py

start/urls.py是主要的urls.py.看起来基本上是这样的:

 urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^mulai/', include('mulai.urls')),

然后在mulai/urls.py中定义 urls ,就像这样

urlpatterns = [
    url(r'^$', IndexView.as_view(), name='index'),
    url(r'^(?P<pk>[0-9]+)/$', DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>[0-9]+)/results/$', ResultsView.as_view(), name='results'),
    url(r'^(?P<choice_question_id>[0-9]+)/votes/$', votes, name='votes')
]

反向使用的命名空间start将在文件start/urls.py中定义,如下所示:

    url(r'^mulai/', include('mulai.urls', namespace='start')),
相关问题