Django admin:在change_form中包含数据库中的一些数据
我正在使用 Django 1.2.4。我有一个具有 JavaScript 媒体的模型的管理类。我想让数据库中的一些数据可供这些脚本使用。我不确定做到这一点的最佳方法是什么。
也许是这样的:(并不是说我认为这是有效的,只是想表达我的观点。)
class FooAdmin(admin.ModelAdmin):
class Media:
js = ("js/baz.js")
raw_js = "var boo = %s; var bar = %s" % (boo, bar)
我考虑在 /templates/admin/foo-app/foo/change_form.html
制作一个模板,但我不确定如何将 boo
和 bar
值放入模板中。 (它们是根据数据库计算的。)
这里正确的方法是什么?
I'm using Django 1.2.4. I have an admin class for a model that has JavaScript media. I would like to make some data from the database available to these scripts. I'm not sure what the best way to do this would be.
Perhaps something like this: (not that I think this is valid, just trying to get my point across.)
class FooAdmin(admin.ModelAdmin):
class Media:
js = ("js/baz.js")
raw_js = "var boo = %s; var bar = %s" % (boo, bar)
I considered making a template at /templates/admin/foo-app/foo/change_form.html
, but I'm not sure how I would put the boo
and bar
values into the template. (They are calculated from the database.)
What is the correct approach here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以重写
ModelAdmin.change_view
以将您想要的任何变量传递给change_form
模板。http://docs.djangoproject。 com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.change_view
您还可以(未经测试)在
ModelAdmin
中定义一个视图,将其映射到带有ModelAdmin.get_urls
的 URL,并将该 URL 传递给管理媒体。这样您就不必覆盖模板。
You can override
ModelAdmin.change_view
to pass in whatever variables you wish to thechange_form
template.http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.change_view
You could also potentially (untested) define a view in your
ModelAdmin
, map it to a URL withModelAdmin.get_urls
, and pass in that url to the admin media.That way you wouldn't have to override a template.