Implement Django back-end to get the value in the front-end drop-down box

Realization: By selecting the content in the drop-down box of the front-end HTML page, the back-end view layer can obtain the content value

1. The front-end HTML code is as follows:

By embedding a drop-down box in a form

Note that the submission method of the form is method="post"

Built-in select selection box, be careful not to be label tags, otherwise it will return a value of None, which will not achieve the purpose

<form action="result.html" method="post" name="tasksubmit">
        {% csrf_token %}
        <p>{#    必须添加lable标签 后端才能获取到下拉框选择的值        #}
            <label>选择工具:
                <select name="tools">
                <option selected value="ping">ping</option>
                <option value="traceroute" >traceroute</option>
                <option value="whois">whois</option>
                </select>
            </label>
        </p>
        <input type="reset">&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="提交任务">
</form>

The back-end views code is as follows:

Note: "tools" is the value of the name attribute in the front-end <select name="tools"> code, so that the initial purpose can be achieved.

def get_data(request):
    tools = request.POST.get("tools")
    print(tools)

 

Guess you like

Origin blog.csdn.net/lyw5200/article/details/112881646