Django - 从管理员变更页面访问_set [英] Django - Accessing _set from Admin change page

查看:105
本文介绍了Django - 从管理员变更页面访问_set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让标准示例

class Author(models.Model):
  name = models.CharField(max_length=100)

class Book(models.Model):
  title = models.CharField(max_length=100)
  author = models.ForeignKey(Author)
  #... Many other fields ...

从$ code的管理员更改模板>作者试图访问相关图书

From the admin change template of Author trying to access to the related books

<ul>
  {% for book in original.book_set.all %}
    <li>
      <a href="{% url admin:myapp_manager_change book.id %}">Edit {{ book }}</a>
    </li>
  {% endfor %}
</ul>

我得到


在呈现时捕获NoReverseMatch:未找到参数'(1L,)'和关键字参数'{}''反向'myapp_manager_change'。

Caught NoReverseMatch while rendering: Reverse for 'myapp_manager_change' with arguments '(1L,)' and keyword arguments '{}' not found.

为什么?

我如何访问这个 book_set

推荐答案

查看倒转管理网址

您必须将myapp替换为您的实际应用程序:

You have to replace "myapp" with your actual app:

admin:{{ app_label }}_{{ model_name }}_change object_id

所以如果你的应用程序是命名库,并且你的模型的名字是书,链接将是:

So if your app is named library and your model's name is book the link would be:

<a href="{% url admin:library_book_change book.id %}">Edit {{ book }}</a>

这篇关于Django - 从管理员变更页面访问_set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆