Django: How can I take the choice in drop-down list and redirect to another page?

I’m currently just learning Django and I’m doing electronic grade book. I have tried everything, have read all the documentation, but nothing helps. It seems I miss a simple logic somewhere. I need to make two pages:

The first one “teacher_interface” is a simple inteface for the teacher with just one drop-down list, teacher chooses the necessary class (i.e 1C, 2B, 4C) and the button “Students”, which should somehow take the chosen class from drop-down list input and redirect to the second page “class_students”.

The second “class_students” is alike the “teacher_interface”, but with the table of students of the chosen class.

I have the One-to-many relation between classes Student and Class:

Firstly, I tried redirecting from “teacher_interface” to “class_students”, using in template:

{% url "name" %}

Parts of code: 1) models.py dpaste/eqxm (Python) 2) urls.py dpaste/eUEO (Python) 3) views.py https://dpaste.org/ap8D#L 4) template teacher_interface.html dpaste/v4m9 (HTML + Django/Jinja) 5) template class_students.html dpaste/0gXK (HTML + Django/Jinja)

But it shows me: Reverse for ‘class_students’ with no arguments not found. 1 pattern(s) tried: [‘school/teacher/(?P<class_id>[0-9]+)/class/$’]

I tried everything, but nothing helped, this and the similar: Django - getting Error “Reverse for ‘detail’ with no arguments not found. 1 pattern(s) tried:” when using {% url “music:fav” %} I understood maybe this two options of redirect will not work in my case:

{% url 'class_students' class.id %}

{% url 'class_students' class_id %}

I also don’t know if it’s possible to do on the same page.

So I decided to redirect using redirect from django.shortcuts. I changed my teacher_interface view, so that it took the id of the chosen by the teacher class if request method is POST and redirected. I also made this change in my template “teacher_interface.html”:

from

action="{% url 'class_students' %}"

to

action=""

Changed view:

def teacher_interface(request):
class_queryset = Class.objects.order_by("class_number", "group")
class_id = None
if request.method == "POST":
    class_id = Class.objects.get("id")
    return redirect("class_students", class_id)
context = {
    "class_queryset": class_queryset,
    "class_id": class_id,
}
return render(request, "teacher_interface.html", context)

But when I choose the class and click the “Students” button, it shows me: Cannot resolve keyword ‘i’ into field. Choices are: class_number, curriculum, discipline, group, id, student, task, type_of_class, type_of_class_id. Id is certainly is a key, but it tries to resolve only “i”.

I tried/read everything here, but nothing works.

I even wrote the default like this:

class_id = Class.objects.get("id", "default")

I am sure I just don’t understand properly how to get teacher’s choice, pass it to another or the same function and redirect, saving this information. I will be really grateful for you help, even if you just advise what I can read to figure it out.

This view

def class_students(request):
    return render(request, "class_students.html")

needs an argument class_id for this route

    path('teacher/<int:class_id>/class/', views.class_students, name="class_students"),

to hit. Right now your view is not accepting a parameter for the class id. If your view is expecting an id, make sure your url tag in your template is actually the id and not something else too. The error message is trying to tell you this in a roundabout way.

Maybe, at least. Django can be hard to debug, so maybe posting the whole project on repl.it would make it easier to debug.

Thanks a lot. I will try.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.