Django2.0“删除模板中的硬编码网址”无法正常工作

时间:2018-05-22 16:14:11

标签: python django django-models django-templates django-views

我在本教程后第一次检查django [https://docs.djangoproject.com/en/2.0/intro/tutorial03/]

当我从以下位置更改硬编码的网址时

<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>

为:

<li><a href="{% url 'detail' poll.id %}">{{ poll.question }}</a></li>

我收到如下错误:

我的urls.py文件是:

from django.urls import path

from . import views

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

我的views.py文件是:

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse

from .models import Question,Choice

def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question':question})

def results(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/results.html', {'question':question})

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 HttpRespinsedirect 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,)))

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list':latest_question_list}
    return render(request, 'polls/index.html', context)

我的index.html文件是:

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="{% url 'detail' question_id %}">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

我的details.html文件是:

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

{% if error_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>

所以,我很困惑删除模板中的硬编码网址不起作用。 有人能帮助我吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

您应该在网址中使用名称间距。这就是您将app_name设置为轮询的原因。你的网址应该是这样的,

<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>

现在你的网址还不知道你在谈论什么细节。

这是url conf docs,并在底部讨论了名称间距。 https://docs.djangoproject.com/en/2.0/topics/http/urls/