surabhi gupta
surabhi gupta

Reputation: 55

Reverse for 'results' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried

I am getting the following error while running the django server :

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

Note : The error goes off if I remove this line from index.html file.

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

But without this, I cannot do the post request .

This is my result.html template:

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

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})
the index.htmp template is this

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

I have tried various things , but none is working. I can't remove the form action from the code since that would make it useless.

Upvotes: 0

Views: 1446

Answers (2)

Prakhar Trivedi
Prakhar Trivedi

Reputation: 8526

The problem is index.html file.

You are passing student.id into form action but there student is not a defined variable,you should move the action and form tag inside the for loop.The resulting code would be like this :

{% 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 %}

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599648

student doesn't exist at that point, so there is no value for student.id.

You should move your for tag so it is before the form element - and the matching endfor needs to come after the form close.

Upvotes: 1

Related Questions