首页 > 解决方案 > 基于没有 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())
]

标签: pythonhtmldjango

解决方案


或在您的 HTML 文件中使用 Javascript 中的 AJAX

$('#brand').on('submit',function(e){
 var brand = $('input[name=brands]')
 $.get("brands/"+brand, function(data, status){
 alert("Data: " + data + "\nStatus: " + status);

}); })


推荐阅读