首页 > 解决方案 > 如何使用 django 将下拉列表的值从 html 传递到 python

问题描述

我确信这是一个我无法解决的简单语法问题。我刚开始在 django 工作,试图从 tkinter 转移到我的 python 爱好项目。下面的列表是我的视图、模板和 url 文件。

我想要的前端 GUI 只是让用户选择文件路径、区域和输出类型。我计划将这些值传递给 python 来做一些逻辑。

当我点击提交按钮时,我在输出页面上得到了这一行调试输出:

Using the URLconf defined in WebFetcher.urls, Django tried these URL patterns,
in this order:
    
[name='home']
[name='page_objects']
admin/
The current path, views, didn't match any of these.

我的看法:

from django.shortcuts import *
from django.http import *
# Create your views here.

def home(request):
    return render(request, 'home.html')

def page_objects(request):
    Area = request.GET["Area"]
    Output = request.GET["Output"]
    print(Area)
    print(Output)
    return render(request, 'home.html')

我的模板:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

    <h2>Please Select Your Document, and the Area</h2>
    <p>Move the mouse over the button to open the dropdown menu.</p>
    <p>Then hit submit!</p>
    <p>
        <label for="myfile">Select a file:</label>
        <input type="file" id="myfile" name="myfile">
    </p>


    <form action="/views" method="post">
        <select name="Area">
                <option selected="selected" disabled>Objects on page:</option>
                <option value="P10">P10</option>
                <option value="P20">P20</option>
                <option value="P30">P30</option>
                <option value="P40">P40</option>
                <option value="P50">P50</option>
        </select>
        <select name="Output">
                <option selected="selected" disabled>Objects on page:</option>
                <option value="List">List</option>
                <option value="Address">Address</option>
        </select>
        <input type="submit" value="Select">
    </form>
</body>
</html>

我的网址:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name = 'home'),
    path('', views.home, name = 'page_objects')
]

标签: pythondjango

解决方案


推荐阅读