简体   繁体   English

Django - 在 HTML 中嵌入 python 以触发两个表单操作

[英]Django - Embed python inside HTML to trigger two form actions

All I want to do is to have a condition in order when the user clicks FOR SALE button to be redirected to url 'salesearch' or when clicks TO RENT to be redirected to url 'rentsearch' .我要做的就是在用户单击FOR SALE按钮以重定向到url 'salesearch'或单击TO RENT以重定向到url 'rentsearch'时设置一个条件。

Here's the code:这是代码:

 <div class="search">
  <form action="{% url 'salesearch' %}">
   <div class="form-row">
    <div class="col-md-4 mb-3">
     <label class="sr-only">
      Keywords
     </label>
     <input class="form-control" name="keywords" placeholder="Keyword (Pool, Garage, etc)" type="text">
    </div>
    <div class="col-md-4 mb-3">
     <label class="sr-only">
      City
     </label>
     <input class="form-control" name="city" placeholder="City" type="text">
    </div>
    <div class="form-row">
     <div class="col-md-6 mb-3">
      <button class="btn btn-secondary btn-block mt-4" name="For Sale" type="submit">
       For Sale
      </button>
     </div>
     <div class="col-md-6 mb-3">
      <button class="btn btn-secondary btn-block mt-4" name="To Rent" type="submit">
       To Rent
      </button>
     </div>
    </div>
   </div>
  </form>
 </div>

With the above example I am redirected always at url 'salesearch path.在上面的例子中,我总是被重定向到 url 'salesearch 路径。 Is there any way to have a conditional to determine the url's path?有什么方法可以有条件地确定 url 的路径吗?

Sure is!当然是!

You can set formaction on button elements :您可以button元素上设置formaction

<button name="For Sale" type="submit" formaction="{% url 'salesearch' %}">
For Sale
</button>
<button name="To Rent" type="submit" formaction="{% url 'rentsearch' %}">
To Rent
</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM