在Django中,如何将选定的下拉值从模板传递到视图?

时间:2022-12-03 07:39:56

I searched for so long for a solution to this, but still can't find any. I have a big form in my template, which is actually composed of a bunch of model forms. One field in that big form is not part of a form, but is a single dynamic drop down menu populated from a table called "Institutions" in views.py as such: Institutions.objects.all()

我搜索了这么久的解决方案,但仍然找不到任何解决方案。我的模板中有一个很大的表单,实际上是由一堆模型表单组成的。该大表单中的一个字段不是表单的一部分,而是一个单独的动态下拉菜单,填充在views.py中名为“INSTIT”的表中,如:INSTIT.objects.all()

Here is the part from views.py:

这是来自views.py的部分:

def submission_form(request):

    institutions = Institution.objects.all()

    if request.method == 'POST':
        abstractform = AbstractForm(request.POST)
        authorform = AuthorForm(request.POST)

        # Here I want code: if selected institution is this, then do that

        if abstractform.is_valid() and authorform.is_valid()
            new_abstract = abstractform.save()
            new_author = authorform.save()


     else:  
         return render(request, 'records/submission_form.html', {'abstractform': abstractform, 'authorform': authorform, 'institutions':institutions })

This is the drop down in my template:

这是我的模板中的下拉菜单:

 <select id="ddlInstititions">
    <option value="%">---------</option>
    {% for entry in institutions %} 
     <option value="{{ entry.id }}">{{ entry.name }}</option>
    {% endfor %}
</select>

My question is: Is it possible to pass that selected entry.name to the view so I can use it there? If not, what do you recommend doing instead?

我的问题是:是否可以将选定的entry.name传递给视图,以便我可以在那里使用它?如果没有,你建议做什么呢?

Any help would be much appreciated!

任何帮助将非常感激!

2 个解决方案

#1


9  

In order for any form element to be sent in the POST, you need to have a name attribute. So it should be <select id="ddlInstititions" name="institutions">.

为了在POST中发送任何表单元素,您需要具有name属性。所以它应该是

What's passed to the view in the POST is the value attribute of each option element. Currently, you've set that to entry.id, so it's the ID that will be in the POST. You can either use that to look up the Institution object and get the name, or you can change the form so you put entry.name directly in the value attribute.

在POST中传递给视图的是每个选项元素的value属性。目前,您已将其设置为entry.id,因此它将是POST中的ID。您可以使用它来查找Institution对象并获取名称,也可以更改表单,以便将entry.name直接放在value属性中。

#2


5  

You can use jQuery's $.ajax() for this.

你可以使用jQuery的$ .ajax()。

In your Javascript, you can bind an event handler to #ddlInstititions via

在您的Javascript中,您可以将事件处理程序绑定到#ddlInstititions via

$("#ddlInstitions").on("change", function(){
  var selectedValue = $(this).text();

  $.ajax({
    url : "insititionsSelectHandler/",
    type : "GET",
    data : {"name" : selectedValue},
    dataType : "json",
    success : function(){

    }
  });
});

What this will do is when you make a select event on the dropdown, it will fire this event handler. You will have to define this URL in your `urls.py' like

这将做的是当您在下拉列表上创建一个select事件时,它将触发此事件处理程序。您必须在`urls.py'中定义此URL

(r'^/institionsSelectHandler/$', views.insititionsSelectHandler),

and you can get the value inside the view method like

你可以在view方法中获取值

def insititionsSelectHandler(request):
   key = request.GET["name"]
   ...
   ...
   ...
   #and return your results as a HttpResponse object that contains a dict
   return HttpResponse(simplejson.dumps({"success" : "true", "message" : ... }, mimetype = "application/json")

#1


9  

In order for any form element to be sent in the POST, you need to have a name attribute. So it should be <select id="ddlInstititions" name="institutions">.

为了在POST中发送任何表单元素,您需要具有name属性。所以它应该是

What's passed to the view in the POST is the value attribute of each option element. Currently, you've set that to entry.id, so it's the ID that will be in the POST. You can either use that to look up the Institution object and get the name, or you can change the form so you put entry.name directly in the value attribute.

在POST中传递给视图的是每个选项元素的value属性。目前,您已将其设置为entry.id,因此它将是POST中的ID。您可以使用它来查找Institution对象并获取名称,也可以更改表单,以便将entry.name直接放在value属性中。

#2


5  

You can use jQuery's $.ajax() for this.

你可以使用jQuery的$ .ajax()。

In your Javascript, you can bind an event handler to #ddlInstititions via

在您的Javascript中,您可以将事件处理程序绑定到#ddlInstititions via

$("#ddlInstitions").on("change", function(){
  var selectedValue = $(this).text();

  $.ajax({
    url : "insititionsSelectHandler/",
    type : "GET",
    data : {"name" : selectedValue},
    dataType : "json",
    success : function(){

    }
  });
});

What this will do is when you make a select event on the dropdown, it will fire this event handler. You will have to define this URL in your `urls.py' like

这将做的是当您在下拉列表上创建一个select事件时,它将触发此事件处理程序。您必须在`urls.py'中定义此URL

(r'^/institionsSelectHandler/$', views.insititionsSelectHandler),

and you can get the value inside the view method like

你可以在view方法中获取值

def insititionsSelectHandler(request):
   key = request.GET["name"]
   ...
   ...
   ...
   #and return your results as a HttpResponse object that contains a dict
   return HttpResponse(simplejson.dumps({"success" : "true", "message" : ... }, mimetype = "application/json")