python - 基于没有 JS 的单个 html 输入的自定义 url
问题描述
因此,对于我无法解决的 html 表单,我可能有最简单的任务。
我想在搜索栏中输入品牌名称,点击提交,它会重定向到 url/brand/[input]
我已经有了 django 中 url 设置的工作视图。我将如何构建表单以实际创建我想要的 url?
<form method="GET" action="">
<input type="text" name="brand" id="brand" placeholder="Search..." aria-label="Search">
<button type="submit">Search</button>
</form>
视图.py
class GetByBrand(ListView):
def get(self, request, brand):
网址.py
urlpatterns = [
path('', GetListView.as_view(), name='home'),
path('brands/<str:brand>/', GetByBrand.as_view())
]
解决方案
或在您的 HTML 文件中使用 Javascript 中的 AJAX
$('#brand').on('submit',function(e){
var brand = $('input[name=brands]')
$.get("brands/"+brand, function(data, status){
alert("Data: " + data + "\nStatus: " + status);
}); })
推荐阅读
- arrays - 字符串数组不会打印
- django - 如何在django中传递外键
- html - 如何使容器响应?
- shell - 期望不适用于非英语语言
- php - Is this the most effective way to check that there are values returned?
- adobe - Adbobe after effect: Missing 3D rendering
- javascript - 我可以使用具有相同路径和不同参数名称的多个获取请求吗?
- go - How to Set Read Only for Certain Lines in VSCode
- javascript - 通过单击del按钮删除复选框时遇到问题
- python - 在 Pandas 数据框中,如何根据满足不同条件的起始行和结束行过滤一组行?