Django应用出现TemplateSyntaxError:无法解析'{{doc.id}}'剩余内容
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_feedview properly accepts thepkargument, fetches theFeedobject, deletes it, and returns a response. - The URL pattern correctly captures an integer
pkand maps to thedelete_feedview with the namedelete_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

