python - 如何使用 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')
]
解决方案
推荐阅读
- python - Torch/numpy:groupby pandas 替代方案
- rust - "->!" 是什么意思?在 Rust 中的意思
- makefile - 是否有任何做法将状态文件生成为 Makefile 目标
- progress-bar - AndroidTV 主屏幕频道 - 进度条
- java - 在Pageable中传递给Spring控制器的排序将方向附加到属性名称,导致SQL错误
- google-cloud-platform - Google DataFlow 管道中的 BigQuery 读取速度很慢
- shell - 无法在 jenkins 中运行管道 shell 脚本
- mysql - 为什么mysql不接受阿姆哈拉语字符?
- cmake - 无法编译包含 tf2_ros 标头的 C++ 程序
- swiftui - 选择器在视图层次结构变暖中不可见