【问题标题】:Django: How can I take the choice in drop-down list and redirect to another page?Django:如何在下拉列表中进行选择并重定向到另一个页面?
【发布时间】:2021-07-17 15:22:38
【问题描述】:

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

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

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

我有学生和班级之间的一对多关系:

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

{% url "name" %}

部分代码:1)models.pyhttps://dpaste.org/eqxm2)urls.pyhttps://dpaste.org/eUEO3)views.pyhttps://dpaste.org/ap8D#L4)模板teacher_interface.htmlhttps://dpaste.org/v4m95)模板class_students.htmlhttps://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")

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

【问题讨论】:

    标签: python django redirect drop-down-menu choice


    【解决方案1】:

    好吧,你错过了一些基本的概念。

    关于你的意见.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", "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")
    

    【讨论】:

    • class = get_object_or_404(Class, pk=class_id) # this avoid internal server error. 实际上你不能使用class = 因为class是python中的保留字
    • 获得对象后,您可以做任何您想做的事情,例如:获取与该课程相关的学生列表
    • 非常感谢。你是一个巨大的帮助!我会试试的。
    猜你喜欢
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 2012-08-02
    • 2023-03-29
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多