djangochange_form.html

发布于 2024-09-28 10:26:54 字数 621 浏览 7 评论 0原文

我想将参数{{x}}传递给我的自定义文件change_form.html,该文件位于/home/django/project/app/template/admin/change_form。 html.我找到了这段代码,但它不起作用:

class MyModelAdmin(admin.ModelAdmin):
    # A template for a very customized change view:
    change_form_template = 'admin/change_form.html'

    def get_osm_info(self):
        z = Klass()
        x = z.final_down()
        return x

    def change_view(self, request, object_id, extra_context=None):
        my_context = { 'x': get_osm_info(),}
        return super(MyModelAdmin, self).change_view(request, object_id,extra_context=my_context)

I'd like to pass an argument {{x}}to my custom file change_form.html, which is located in /home/django/project/app/template/admin/change_form.html. I found this code but it doesn't work:

class MyModelAdmin(admin.ModelAdmin):
    # A template for a very customized change view:
    change_form_template = 'admin/change_form.html'

    def get_osm_info(self):
        z = Klass()
        x = z.final_down()
        return x

    def change_view(self, request, object_id, extra_context=None):
        my_context = { 'x': get_osm_info(),}
        return super(MyModelAdmin, self).change_view(request, object_id,extra_context=my_context)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的憧憬 2024-10-05 10:26:54

我想我实际上可以回答这个问题(对于通过谷歌找到这个问题的其他人)。

Django 1.4 实际上改变了change_view 的定义方式,并且您可能在互联网上找到的一些文档或片段尚未更新。

https://docs.djangoproject。 com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.change_view

换句话说,这应该有效:

class MyModelAdmin(admin.ModelAdmin):
    # A template for a very customized change view:
    change_form_template = 'admin/change_form.html'

    def get_osm_info(self):
        z = Klass()
        x = z.final_down()
        return x

    def change_view(self, request, object_id, form_url='', extra_context=None):
        context = {}
        context.update(extra_context or {})
        context.update({ 'x': get_osm_info(),})
        return super(MyModelAdmin, self).change_view(request, object_id, form_url, context)

I think I can actually answer this question (for anyone else that find this question through google).

Django 1.4 actually changed the way change_view is defined and some documentation or snippets you might find on the internet have not yet been updated.

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.change_view

In other words, this should work:

class MyModelAdmin(admin.ModelAdmin):
    # A template for a very customized change view:
    change_form_template = 'admin/change_form.html'

    def get_osm_info(self):
        z = Klass()
        x = z.final_down()
        return x

    def change_view(self, request, object_id, form_url='', extra_context=None):
        context = {}
        context.update(extra_context or {})
        context.update({ 'x': get_osm_info(),})
        return super(MyModelAdmin, self).change_view(request, object_id, form_url, context)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文