I'm not sure exactly how to ask this question, so apologies in advance if it doesn't make a lot of sense.
I'm creating a basic blogging-type website using pythonanywhere, and want each user to have a profile page. In the page that displays a list of all posts, I have the author of each post displayed like so in a
{% for post in posts %}:
<p>author: {{ post.author }}</p>
Assuming that all my urls, views and templates are fine, how do I add a link to that text which would lead to /accounts/[user's id]
?
The Post model's author field is defined as
author = models.ForeignKey('auth.User')
Essentially I want:
<p>author: <a href="{% url 'account_detail' pk=User.pk %}">{{ post.author }}</a></p>
But I don't know how to actually get User.pk from post.author.
Assuming that you have this model:
author=models.ForeignKey('HERE IS ENOUGH THE MODEL)
and the ForeignKey is in a model named POST
for access the id of the table just write:
post.author__pk
By the way is always better do this operation in the view.py like:
value = post.objects.values('name', 'author__pk').filter(filter if necessary)
@Ivan Miljkovic的解释方式是,足以替换以下代码行:
<p>author: <a href="{% url 'account_detail' pk = post.author.pk %}">{{ post.author }}</a></p>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.