简体   繁体   中英

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 (ie 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 https://dpaste.org/eqxm 2) urls.py https://dpaste.org/eUEO 3) views.py https://dpaste.org/ap8D#L 4) template teacher_interface.html https://dpaste.org/v4m9 5) template class_students.html https://dpaste.org/0gXK

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.

Ok, you are missing some basic conpects.

on your views.py

def teacher_interface(request):
    class_queryset = Class.objects.order_by("class_number", "group")
    context = {
        "class_queryset": class_queryset,
    }
    return render(request, "teacher_interface.html", context)

this is correct, you will pass you query to your template

on your template change some things to look like this:

<form method="POST" >{% csrf_token %}
<select name="input1">
    {% for class in class_queryset %}
    <option value="{{ class.id }}">{{ class }}</option>
    {% endfor %}
</select>
<input type="submit" value="Students"/>
</form>

then you need to change your teacher_interface view: You need to import redirect on your views.py

def teacher_interface(request):
    class_queryset = Class.objects.order_by("class_number", "group")
    context = {
        "class_queryset": class_queryset,
    }
    if request.method == 'POST':
        class_id = request.POST.get('input1') # I'm not sure if this will get the {{class.id}} value, if don't, print(request.POST.get) and check how to get the value
        return redirect('class_students', class_id=class_id) # will make a get request on the class_students view
    return render(request, "teacher_interface.html", context)

def class_students(request, class_id):
    # the parameter need to be 'class_id' because this is what you put on your urls '<int:class_id>', if possible, remove that /class.
    # ADD CLASS ID AS PARAMETER, THAT WILL ENABLE YOU TO ACESS AN SPECIFIC CLASS
    # Import get_object_or_404 (google it and you will find easily)
    class = get_object_or_404(Class, pk=class_id) # this avoid internal server error.
    # pass your class on the context
    return render(request, "class_students.html")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM