You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django应用出现TemplateSyntaxError:无法解析'{{doc.id}}'剩余内容

Fixing TemplateSyntaxError for Django Delete Button

The error you're seeing comes from incorrect syntax in your template's url tag. Inside Django's template tags (wrapped in {% %}), you don't use the double curly brace {{ }} syntax to reference variables—you can pass the variable directly.

The Problematic Line:

<a href="{% url 'delete_feed' pk={{doc.id}} %}"><button type="button" class="btn btn-danger">Delete Item</button></a>

Corrected Template Code:

Replace the above line with this:

<a href="{% url 'delete_feed' pk=doc.id %}"><button type="button" class="btn btn-danger">Delete Item</button></a>

Why This Works:

When using the {% url %} tag, Django expects variable names without the {{ }} wrapper. Removing those braces lets the template engine correctly parse doc.id as the value for the pk parameter in your URL pattern.

Quick Check of Other Files:

Your views.py and urls.py look correct:

  • The delete_feed view properly accepts the pk argument, fetches the Feed object, deletes it, and returns a response.
  • The URL pattern correctly captures an integer pk and maps to the delete_feed view with the name delete_feed.

A Note on Best Practices:

While this fixes the syntax error, it's worth noting that using a GET request (via an anchor tag) for deletion isn't ideal for security. For better practice, consider using a POST form instead. This prevents accidental deletions from crawlers or users clicking links unintentionally.

内容的提问来源于stack exchange,提问作者mtkguy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:56:23