为什么我在Django中有这个错误?

时间:2018-06-11 06:47:20

标签: python django

我正在关注目前位于第6部分的Django教程。

当我点击网页上的列表时出现错误。我查找了问题发生的地方,但我找不到它。

这是来自网络浏览器的消息:

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic
from django.utils import timezone

from .models import Choice, Question

# ...
class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        """
        Return the last five published questions (not including those set to be
        published in the future).
        """
        return Question.objects.filter(
            pub_date__lte=timezone.now()
        ).order_by('-pub_date')[:5]

class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'
    def get_queryset(self):
        """
        Excludes any questions that aren't published yet.
        """
        return Question.objects.filter(pub_date__lte=timezone.now())

class ResultsView(generic.DetailView):
    model = Question
    template_name = 'polls/results.html'

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

回溯
“GET / polls / 1 // HTTP / 1.1”404 2703

mysite的/轮询/ views.py

from django.urls import path

from . import views

app_name = 'polls'
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
    path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

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 / urls.py

{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />

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

mysite的/轮询/模板/轮询/ index.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>

mysite的/轮询/模板/轮询/ detail.html

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

<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>

<a href="{% url 'polls:detail' question.id %}">Vote again?</a>

mysite的/轮询/模板/轮询/ result.html

<form method="post" id="formFilter" action="admin-advertiser-search.php">
....
</form>

<script>
    $(function() {

    // Get the form.
    var form = $('#formFilter');

    // Set up an event listener for the contact form.
    $(form).submit(function(e) {
    .....
      }
    }

1 个答案:

答案 0 :(得分:1)

你的网址末尾有两个斜杠('//'),这应该只有一个:

http://localhost:8000/polls/1/

更具体地说,在您的民意调查/ index.html模板中,您有以下一行:

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

在该行中,此部分为您生成网址:

{% url 'polls:detail' question.id %}

变为:

http://localhost:8000/polls/1/

但是你在它后面添加了一个额外的斜杠,看看下面字符串的结尾:

"{% url 'polls:detail' question.id %}/"

删除它,我认为你的代码应该有效。