Django:如何在下拉列表中进行选择并重定向到另一个页面?

问题描述

我目前正在学习 Django,并且正在制作电子成绩单。我已经尝试了一切,阅读了所有文档,但没有任何帮助。似乎我在某处错过了一个简单的逻辑。我需要制作两页:

一个“teacher_interface”是一个简单的教师界面,只有一个下拉列表,教师选择必要的班级(即1C、2B、4C)和按钮“学生”,它应该以某种方式选择class 从下拉列表输入并重定向到第二页“class_students”。

第二个“class_students”与“teacher_interface”类似,但带有所选班级的学生表。

我在类 Student 和 Class 之间有一对多的关系:

首先,我尝试从“teacher_interface”重定向到“class_students”,在模板中使用:

{% url "name" %}

部分代码:1) models.py https://dpaste.org/eqxm 2) urls.py https://dpaste.org/eUEO 3) views.py https://dpaste.org/ap8D#L 4) 模板teacher_interface.html https://dpaste.org/v4m9 5)模板 class_students.html https://dpaste.org/0gXK

但它告诉我: 'class_students' 没有找到任何参数的反向。尝试了 1 个模式:['school/teacher/(?P[0-9]+)/class/$']

我尝试了所有方法,但没有任何帮助,这个和类似的:Django - getting Error "Reverse for 'detail' with no arguments not found. 1 pattern(s) tried:" when using {% url "music:fav" %} 我明白这两个重定向选项在我的情况下可能不起作用:

{% url 'class_students' class.id %}

{% url 'class_students' class_id %}

我也不知道是否可以在同一页面上进行。

所以我决定使用来自 django.shortcuts 的重定向进行重定向。我更改了我的 Teacher_interface 视图,以便在请求方法为 POST 并重定向时采用教师类选择的 ID。我也在我的模板“teacher_interface.html”中进行了此更改:

来自

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

action=""

更改视图:

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)

但是当我选择课程并单击“学生”按钮时,它显示:无法将关键字“i”解析到字段中。选项有:class_number、课程表、学科、组、id、学生、任务、type_of_class、type_of_class_id。 Id 当然是一个键,但它试图只解析“i”。

我尝试/阅读了这里的所有内容,但没有任何效果

我什至这样写了认值:

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

我确定我只是没有正确理解如何获得老师的选择,将其传递给另一个或相同的函数重定向,保存这些信息。我将非常感谢您的帮助,即使您只是建议我可以阅读以解决问题。

解决方法

好的,您缺少一些基本概念。

在您的 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)

这是正确的,您会将查询传递给您的模板

在您的模板上将一些内容更改为如下所示:

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

然后您需要更改您的 teacher_interface 视图: 您需要在您的 views.py 上导入重定向

def teacher_interface(request):
    class_queryset = Class.objects.order_by("class_number",}
    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,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")