<i id='400VR'><tr id='400VR'><dt id='400VR'><q id='400VR'><span id='400VR'><b id='400VR'><form id='400VR'><ins id='400VR'></ins><ul id='400VR'></ul><sub id='400VR'></sub></form><legend id='400VR'></legend><bdo id='400VR'><pre id='400VR'><center id='400VR'></center></pre></bdo></b><th id='400VR'></th></span></q></dt></tr></i><div id='400VR'><tfoot id='400VR'></tfoot><dl id='400VR'><fieldset id='400VR'></fieldset></dl></div>
  • <legend id='400VR'><style id='400VR'><dir id='400VR'><q id='400VR'></q></dir></style></legend>

      • <bdo id='400VR'></bdo><ul id='400VR'></ul>

      <small id='400VR'></small><noframes id='400VR'>

      1. <tfoot id='400VR'></tfoot>

        Django 管理弹出功能

        时间:2023-10-01

        <i id='F6nX1'><tr id='F6nX1'><dt id='F6nX1'><q id='F6nX1'><span id='F6nX1'><b id='F6nX1'><form id='F6nX1'><ins id='F6nX1'></ins><ul id='F6nX1'></ul><sub id='F6nX1'></sub></form><legend id='F6nX1'></legend><bdo id='F6nX1'><pre id='F6nX1'><center id='F6nX1'></center></pre></bdo></b><th id='F6nX1'></th></span></q></dt></tr></i><div id='F6nX1'><tfoot id='F6nX1'></tfoot><dl id='F6nX1'><fieldset id='F6nX1'></fieldset></dl></div>
        <legend id='F6nX1'><style id='F6nX1'><dir id='F6nX1'><q id='F6nX1'></q></dir></style></legend>

              <tbody id='F6nX1'></tbody>
              <tfoot id='F6nX1'></tfoot>
              • <bdo id='F6nX1'></bdo><ul id='F6nX1'></ul>

              • <small id='F6nX1'></small><noframes id='F6nX1'>

                  本文介绍了Django 管理弹出功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这个话题相当普遍(最明确的细节在这里:http://www.hoboes.com/Mimsy/hacks/replicating-djangos-admin/),但我仍然遇到问题.我正在尝试使用管理站点中使用的加号"按钮功能,可以在其中向链接条目添加额外的外键.在管理站点中,将显示一个弹出窗口,允许用户提交一个新字段,然后在原始表单中填充该新值.

                  This topic is fairly common (most explicitly detailed here: http://www.hoboes.com/Mimsy/hacks/replicating-djangos-admin/), but I'm still having trouble with it. I'm trying to use the "plus" button functionality used in the admin site where one can add an additional foreign key to a linked entry. In the admin site, a popup displays allowing the user to submit a new field and then that new value is populated on the original form.

                  我认为我的问题集中在包含这一行:

                  I think my issue centers around the inclusion of this line:

                  在 base.html 模板和 popadd.html 模板中.单击加号按钮不会打开新窗口.popadd 模板只是在同一个选项卡中加载.并且提交新条目不会让用户回到原来的表单.

                  within the base.html template and the popadd.html template. Clicking the plus button does not bring up a new window. The popadd template simply loads in the same tab. And submitting a new entry does not take the user back to the original form.

                  管理站点正常运行.我在 settings.py 文件中包含 ADMIN_MEDIA_PREFIX = '/media/admin/' .它与 RelatedObjectLookups.js 所在的位置有关吗?它目前位于我的项目文件夹之外的管理目录中.我必须创建一个符号链接吗?

                  The admin site is functional. I am including ADMIN_MEDIA_PREFIX = '/media/admin/' in the settings.py file. Does it have something to do with where the RelatedObjectLookups.js lives? It's currently in an admin directory outside of my project folder. Do I have to create a symlink?

                  对不起,菜鸟问题.希望有任何建议(尽可能详细).

                  Sorry for the noob questions. Would appreciate any suggestions (as detailed as possible).

                  推荐答案

                  按照下面概述的步骤,您可以重新创建 Django 管理员的相关对象弹出功能,而无需创建任何自定义小部件、视图和 url.这些步骤假设您正在尝试让这个弹出窗口在您自己的自定义管理站点中工作,该站点是 Django 管理员的子类.

                  Following the steps outlined below will allow you to recreate Django admin's related object pop-up functionality without having to create any custom widgets, views, and urls. These steps assume you are trying to get this pop-up working in your own custom admin site which subclasses Django's admin.

                  让我们假设以下两个模型 BookAuthor,具有从 Book 到 Author 的 FK.还假设我们希望能够在创建/编辑书籍时使用相关对象弹出窗口来添加作者:

                  Lets assume the following two models Book and Author, with an FK from Book to Author. Lets also assume that we'll want the ability to use the Related Object Pop-Up to add an Author when creating/editing a Book:

                  [app_name]/models.py:

                  from django.db import models
                  
                  class Author(models.Model):
                      name = models.CharField(max_length=200)
                  
                  class Book(models.Model):
                      author = models.ForeignKey(Author)
                      title = models.CharField(max_length=200)
                  

                  让我们创建我们的自定义管理站点:

                  Lets create our custom admin site:

                  [app_name]/sites.py:

                  from django.contrib.admin.sites import AdminSite
                  
                  my_admin_site = AdminSite(name='my_custom_admin')
                  

                  我们的自定义管理站点将注册两个 ModelAdmin 以允许用户添加/编辑/删除 Book 和 Author 模型:

                  Our custom admin site will register two ModelAdmins to allow users to add/edit/delete both the Book and Author models:

                  [app_name]/admin.py:

                  from django.contrib.admin.options import ModelAdmin
                  
                  from [app_name].forms import BookForm # We'll create this form below
                  from [app_name].models import Author, Book
                  from [app_name].sites import my_admin_site
                  
                  class BookModelAdmin(ModelAdmin):
                      form = BookForm()
                  
                  # Register both models to our custom admin site
                  my_admin_site.register(Author, ModelAdmin)
                  my_admin_site.register(Book, BookModelAdmin)
                  

                  现在,我们将设置上面 BookModelAdmin 中使用的 BookForm.这就是魔法发生的地方.有关 RelatedFieldWidgetWrapper api 的更多信息,点击这里:

                  Now, we'll setup the BookForm which is used in the BookModelAdmin above. This is where the magic happens. For more info on the RelatedFieldWidgetWrapper api, click here:

                  [app_name]/forms.py:

                  from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
                  from django import forms
                  
                  from [app_name].models import Book
                  from [app_name].sites import my_admin_site
                  
                  class BookForm(forms.ModelForm):
                      author = Book._meta.get_field('author').formfield(
                          widget=RelatedFieldWidgetWrapper(
                              Book._meta.get_field('author').formfield().widget,
                              Book._meta.get_field('author').rel,
                              my_admin_site,
                              can_add_related=True
                          )
                      )
                  
                      class Meta:
                          model = Book
                  

                  注意事项:

                  1. 您需要确保模板中包含以下两个 javascript 文件:admin/js/core.jsadmin/js/admin/RelatedObjectLookups.js.

                  陷阱:

                  1. is_popup 需要在您的模板中正确设置和传递.具体来说,在您覆盖的任何自定义 change_form.html 模板中,您必须记住在表单标签中的某处添加此行:{% if is_popup %}<input type="hidden" name="_popup" value="1"/>{% endif %},这样BaseModelAdmin.response_add() 返回正确的响应.
                  1. is_popup needs to be set and passed correctly in your templates. Specifically, in any custom change_form.html templates you override, you must remember to add this line somewhere within your form tags: {% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %}, so that the logic in BaseModelAdmin.response_add() returns the correct response.

                  幕后:本质上,我们正在重用 Django admin 中已经包含的表单处理逻辑、小部件包装器和 javascript.

                  Under The Hood: Essentially, we're re-using the form processing logic, widget wrapper and javascript that is already included with Django admin.

                  1. 使用 RelatedFieldWidgetWrapper 在我们的表单中包装关联到相关对象字段的小部件(并特别在构造函数中传递 can_add_related=True)告诉小部件附加必要的带有相应 javascript onclick 事件的+"链接.
                  2. Django 管理员的 javascript 处理启动弹出窗口所需的所有逻辑.
                  3. 我们的 change_form.html 模板中的 {% if is_popup %}...{% endif %} 逻辑和 BaseModelAdmin 中的逻辑.response_add() 处理新相关对象的保存并返回适当的 javascript 响应,通知弹出窗口它需要关闭.
                  1. Using RelatedFieldWidgetWrapper to wrap the widget associated to the related object field in our form (and specifically passing can_add_related=True in the constructor) tells the widget to append the necessary '+' link with the appropriate javascript onclick event attached to it.
                  2. Django admin's javascript handles all of the logic necessary to launch the pop-up.
                  3. The {% if is_popup %}...{% endif %} logic in our change_form.html template(s) and the logic in BaseModelAdmin.response_add() handles the saving of the new related object and returns the appropriate javascript response that informs the pop-up that it needs to be closed.

                  相关回购:这个公共 repo 应该为上面讨论的 Django 项目提供示例代码:https://github.com/cooncesean/Books

                  Related Repo: This public repo should provide sample code for the Django project discussed above: https://github.com/cooncesean/Books

                  这篇关于Django 管理弹出功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Google Chrome 中不带滚动条的 window.open 下一篇:使用不同的用户代理加载 iframe 内容

                  相关文章

                    <legend id='i03ab'><style id='i03ab'><dir id='i03ab'><q id='i03ab'></q></dir></style></legend>

                    <tfoot id='i03ab'></tfoot>

                    1. <small id='i03ab'></small><noframes id='i03ab'>

                      • <bdo id='i03ab'></bdo><ul id='i03ab'></ul>
                      <i id='i03ab'><tr id='i03ab'><dt id='i03ab'><q id='i03ab'><span id='i03ab'><b id='i03ab'><form id='i03ab'><ins id='i03ab'></ins><ul id='i03ab'></ul><sub id='i03ab'></sub></form><legend id='i03ab'></legend><bdo id='i03ab'><pre id='i03ab'><center id='i03ab'></center></pre></bdo></b><th id='i03ab'></th></span></q></dt></tr></i><div id='i03ab'><tfoot id='i03ab'></tfoot><dl id='i03ab'><fieldset id='i03ab'></fieldset></dl></div>