【问题标题】:How to send data from index.html to views.py in django如何在 django 中将数据从 index.html 发送到 views.py
【发布时间】:2020-06-18 20:06:59
【问题描述】:

我有一张表,其中包含五十家公司(项目)的列表,旁边有一个按钮。我想从单击用户的表中发送公司名称。

index.html的代码:

<table class="table table-striped table-dark" cellspacing="0">
   <thead class="bg-info">
      <tr>
         <th>Company's Symbol</th>
         <th>Current Price</th>
         <th>View Current chart</th>
         <th>Action</th>
      </tr>
   </thead>
   <tbody>
      {% for a,b in stocks %}
      <tr>
         <th scope="row" class="comp_name">{{ a }}</th>
         <td>{{ b }}</td>
         <td>
            <input type="submit" class="btn graph-btn" name="_graph" value="View Graph">
         </td>
         <td>
            <input type="submit" class="btn predict-btn" name="_predict" value="Predict Closing Price">
         </td>
      </tr>
      {% endfor %}
   </tbody>
</table>

不同的 URL 有两个按钮。就像用户在.graph-btn 上点赞时一样,它会转到不同的 URL。

帮助。

【问题讨论】:

  • 你是什么意思去不同的网址。您是否希望用户根据连续单击的按钮重定向到不同的页面?如果您希望用户重定向,您可以使用&lt;a&gt; 标记并将其设置为按钮。如果数据来自数据库,href 应该可以通过 django url helpers 轻松构建

标签: javascript python jquery django ajax


【解决方案1】:

如果你想去另一个需要变量的路径(Route)

在您的模板中,您可以使用如下所示的 url 函数:

<a href="{% url 'route_name' var %}">.....</a>

如果你想从你的 python 代码用户反向方法中得到相同的结果

    return HttpResponseRedirect(reverse('url_path', args(var1)))

更多请查看 django 的文档here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2020-10-08
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    相关资源
    最近更新 更多