微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有按钮和表单的TemplateColumn仅返回记录名称而不是内容

如何解决带有按钮和表单的TemplateColumn仅返回记录名称而不是内容

我试图在表中添加具有某些功能的按钮,以便使用POST将一些信息返回到视图:

class ProductTable(django-tables2.Table):
    Template = (f"""<form action="/product_all/" method="POST">{{% csrf_token %}}
                        <button> 
                            <input type="hidden" name="store_id" value="{{ record.get_store_id }}"/>view</button>    
                    </form>""") 

view = TemplateColumn(Template)

但是在打印请求时我得到:

<QueryDict: {'store_id': ['{ record.get_store_id }'],'csrfmiddlewaretoken': ['...']}>

代替值。

解决方法

我在Django上还很新,但是我认为您需要另一套{}

Template = (f"""<form action="/product_all/" method="POST">{{% csrf_token %}}
            <button> 
                <input type="hidden" name="store_id" value="{{{ record.get_store_id }}}"/>view</button>    
            </form>""") 

所以模板的最终结果是:

<form action="/product_all/" method="POST">{% csrf_token %}
    <button> 
         <input type="hidden" name="store_id" value="{{ record.get_store_id }}"/>view</button>    
</form>

现在正在写:

<form action="/product_all/" method="POST">{% csrf_token %}
     <button> 
          <input type="hidden" name="store_id" value="{ record.get_store_id }"/>view</button>    
</form>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。