【问题标题】:My django didnt work(I'm studying in tutorial)我的 django 没用(我正在学习教程)
【发布时间】:2020-10-19 14:40:17
【问题描述】:

我对 django 很陌生。所以我正在使用教程网站学习。 我认为在现场输入完全相同,但它不起作用。 所以请给我建议。

mysite/urls.py

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

mysite/polls/urls.py

from django.urls import path

from . import views

app_name = 'polls'
urlpatterns = [
    path('', views.index, name='index'),   #url : base/polls/
    path('<int:question_id>/', views.detail, name='detail'),
    path('<int:question_id>/results/', views.results, name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

mysite/polls/templates/polls/detail.html

<h1>{{ question.question_text }}</h1>

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

<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
    <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>

mysite/polls/views.py 中 def 投票的一部分

def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the question voting form.
        return render(request, 'polls/detail.html', {
            'question': question,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

错误

NoReverseMatch at /polls/1/
Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/vote/$']
Request Method: GET
Request URL:    http://127.0.0.1:8000/polls/1/
Django Version: 3.0.7
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/vote/$']
Exception Location: C:\Users\hdh45\crawling_practice\firstP\venv\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 677
Python Executable:  C:\Users\hdh45\crawling_practice\firstP\venv\Scripts\python.exe
Python Version: 3.8.3
Error during template rendering
In template X:\python\practice_well\polls\templates\polls\detail.html, error at line 5

我猜投票网址不起作用。也许有问题。但我不知道确切的答案。我的谷歌搜索能力没用..帮助我。

【问题讨论】:

  • in mysite/polls/templates/polls/detail.htmll {% if errer_message %}&lt;p&gt;&lt;strong&gt;{{ error_message }}&lt;/strong&gt;&lt;/p&gt;{% endif %} 我相信你拼写错误。如果这对您的问题有帮助,我想知道,但无论如何它可能会导致您出错(对不起,如果您打算这样拼写)

标签: python django django-views django-urls django-reversion


【解决方案1】:
<form action="{% url '***polls***:vote' question.id %}" method="post">

polls 是一个字符串,而您的 urls.py 在 path('**&lt;int:question_id&gt;**/vote/', views.vote, name='vote') &lt;int:question_id&gt;int。我认为这是一个错误

我也是初学者

【讨论】:

    猜你喜欢
    • 2018-07-16
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    相关资源
    最近更新 更多