使用参数'('',)'和关键字参数'{}'找不到“结果”。尝试了1种模式

时间:2016-11-16 08:28:24

标签: python django forms exception post

运行django服务器时出现以下错误:

Reverse for 'results' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'library/(?P<student_id>[0-9]+)/results/$']

注意:如果我从index.html文件中删除此行,则错误消失。

<form action="{% url 'library:results' student.id %}" method="post">

但是如果没有这个,我就不能做帖子请求了。

这是我的result.html模板:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
 hey {{ student.name }}
</body>
</html>
  我的views.py文件

from django.http import Http404
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
from .models import Student, Choice
from django.urls import reverse


def vote(request, student_id):
    student = get_object_or_404(Student, pk=student_id)
    try:
        selected_choice = Student.Choice_set.get(pk=request.POST['Choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the question voting form.
        return render(request, 'library/results.html', {
            'student': student,
            'error_message': "You didn't select a choice.",
        })
    else:
        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('library:results', args=(student.id,)))


def index(request):
    students_list = Student.objects.all()
    template = loader.get_template('library/index.html')
    context = {
        'students_list': students_list,
    }
    return HttpResponse(template.render(context, request))


def detail(request, student_id):
    try:
        student = Student.objects.get(pk=student_id)
    except Student.DoesNotExist:
        raise Http404("Student does not exist")
    return render(request, 'library/detail.html', {'student': student})


def results(request, student_id):
    student = get_object_or_404(Student, pk=student_id)
    return render(request, 'library/results.html', {'student': student})
 index.htmp模板是这个

<h1>{{ student.name }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'library:results' student.id %}" method="post">
    {% csrf_token %}
    {% for student in students_list %}

    <input type="checkbox" name="student" id="student{{ forloop.counter }}" value="{{ student.id }}"/>
    <label for="student{{ forloop.counter }}">{{ student.name }}</label><br/>
    {% endfor %}
<input type="submit" value="Vote" />
</form>

我尝试了各种各样的东西,但没有一个是有效的。我无法从代码中删除表单操作,因为这会使它无用。

2 个答案:

答案 0 :(得分:1)

student此时不存在,因此student.id没有任何价值。

你应该移动你的for标签,使它在表单元素之前 - 并且匹配的endfor需要在表单关闭之后。

答案 1 :(得分:1)

问题是index.html文件。

您正在将student.id传递给表单操作但是学生不是已定义的变量,您应该在for循环中移动操作和表单标记。结果代码将如下所示:

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

{% for student in students_list %}

    <h1>{{ student.name }}</h1>

    <form action="{% url 'library:results' student.id %}" method="post"> {% csrf_token %}

        <input type="checkbox" name="student" id="student{{ forloop.counter }}" value="{{ student.id }}"/>
        <label for="student{{ forloop.counter }}">{{ student.name }}</label><br/>
        <input type="submit" value="Vote" />

    </form>

{% endfor %}