如何自动超链接从查询集返回的项目
我有一个模型
class Transaction(models.Model):
sender = models.ForeignKey(MyBankAccount, unique=False, related_name="transactions_sent")
receiver = models.ForeignKey(RecipientBankAccount, unique=False, related_name="transactions_received")
created = models.DateField('transfer request date')
amount = models.FloatField()
createdby=models.ForeignKey(User,null=True)
,使用
<li>{{ item.sender }} {{ item.receiver }} {{ item.amount }}</li>
我希望 item.sender 自动超链接到
http://example.com 显示/johnsmith/trans/ 其中 johnsmith 是创建事务的用户的名称(也称为 createby)。我已经实现了这个url的视图。我只需要链接到它。
I have a model
class Transaction(models.Model):
sender = models.ForeignKey(MyBankAccount, unique=False, related_name="transactions_sent")
receiver = models.ForeignKey(RecipientBankAccount, unique=False, related_name="transactions_received")
created = models.DateField('transfer request date')
amount = models.FloatField()
createdby=models.ForeignKey(User,null=True)
which I display using
<li>{{ item.sender }} {{ item.receiver }} {{ item.amount }}</li>
I d like item.sender to be automatically hyperlinked to
http://example.com/johnsmith/trans/ where johnsmith is the name of the user that creates the transaction (aka createdby). I already implemented the view of this url. I just need to link to it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,大概您的 url.py 看起来像这样:
您的视图是:
那么您的模板片段是:
或者您可以在
Transaction
类中定义以下内容(必须保留 urls.py 文件):然后你的模板看起来像:
但是天哪,这都是 Django 101。
So, presumably your url.py looks something like:
Your view is:
Then your template fragment is:
Or you could define the following in your
Transaction
class (the urls.py file must remain):And then your template would look like:
But geez, this is all Django 101.