简体   繁体   English

Django-从HTML下拉菜单+按钮获取价值

[英]Django - Getting value from HTML dropdown menu + button

I've done some searching already. 我已经做了一些搜索。

I have a program which will do some analysis based on a given country. 我有一个程序,可以根据给定的国家进行一些分析。 The HTML form is: HTML形式为:

<p>Select a country:</p>

<select form="countrySelection" id="countrySelect" required>
    <option value=""></option>
    <option value="BELGIUM">Belgium</option>
    <option value="FRANCE">France</option>
    <option value="ITALY">Italy</option>
</select>

<form action="/analysis" id="countrySelection" method="GET">
    <br>
    <input class="button" name="countrySubmit" type="submit" value="Start">
</form>

In my urls.py I have: 在我的urls.py中,我有:

path('', views.index, name='index'),
path('analysis/', views.run, name='run'),

I want to get the value of the dropdown menu (BELGIUM/FRANCE/ITALY) upon clicking the button and then send it to the view run : 我想在单击按钮后获取下拉菜单的值(比利时/法国/意大利),然后将其发送到视图运行

def run(request):

    country = ...

    [...]

    return HttpResponse("Completed.")

I tried to use "country = request.GET('countrySelection')" and got "TypeError: 'QueryDict' object is not callable" on that line, due to name="countrySubmit" and value="Start" . 由于name =“ countrySubmit”和value =“ Start”,我试图在该行上使用“ country = request.GET('countrySelection')”并收到“ TypeError:'QueryDict'对象不可调用 However, removing the tags results in "No GET data". 但是,删除标签将导致“没有GET数据”。

If you want to get the value of the selected option without submitting the actual form, you can use change event listener from plain JavaScript. 如果要在不提交实际表单的情况下获取所选选项的值,则可以使用纯JavaScript的change event listener

var countriesOBject = document.getElementById("countrySelect");

activities.addEventListener("change", function() {
    // function/code to send the values to the back-end Django URL
    // "countriesOBject.value" will return the value of the select element

});

Your request.get('key') shoud be request.get['key'] as get is not a callable function. 您的request.get('key')应该是request.get ['key'],因为get不是可调用的函数。 You also have to put your select inside the form. 您还必须将选择内容放入表单中。 Here is the runnable code: 这是可运行的代码:

HTML: HTML:

<form action="/analysis" id="countrySelection" method="GET">
    <p>Select a country:</p>

    <select form="countrySelection" name="countrySelect" id="countrySelect" required>
        <option value=""></option>
        <option value="BELGIUM">Belgium</option>
        <option value="FRANCE">France</option>
        <option value="ITALY">Italy</option>
    </select>
    <br>
    <input class="button" type="submit" value="Start">
</form>

views.py views.py

from django.http import HttpResponse
from django.shortcuts import render
from django.template.loader import get_template

# Create your views here.
def run(request):
    country = request.GET['countrySelect']
    print(country)
    return HttpResponse("Completed."+country)

def index(request):
   return render(request, "index.html", {})

urls.py urls.py

path('', views.index, name='index'),
path('analysis/', views.run, name='run'),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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