{"title":"FAQ: 管理","version":"2.0","locale":"zh-hans","docname":"faq/admin","url":"/zh-hans/2.0/faq/admin/","canonical":"https://djangodocs.dev/zh-hans/2.0/faq/admin/","summary":"登陆 cookie 没有被正确设置，这是因为由 Django 发出的 cookie 中的域名和浏览器页面的域名不匹配。","html":"<h1>FAQ: 管理<a class=\"heading-anchor\" href=\"#faq-the-admin\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<section id=\"i-can-t-log-in-when-i-enter-a-valid-username-and-password-it-just-brings-up-the-login-page-again-with-no-error-messages\">\n<h2>无法登录。当我输入有效的用户名和密码后，页面再跳转到登录页面，没有显示任何错误信息。<a class=\"heading-anchor\" href=\"#i-can-t-log-in-when-i-enter-a-valid-username-and-password-it-just-brings-up-the-login-page-again-with-no-error-messages\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>登陆 cookie 没有被正确设置，这是因为由 Django 发出的 cookie 中的域名和浏览器页面的域名不匹配。可以通过修改设置中的 &quot;SESSION_COOKIE_DOMAIN&quot; 选项来匹配你的域名。例如，如果你需要在浏览器中访问 &quot;<a class=\"reference external\" href=\"https://www.example.com/admin/\">https://www.example.com/admin/</a>&quot;，设置 <code class=\"docutils literal notranslate\">SESSION_COOKIE_DOMAIN = 'www.example.com'</code>。</p>\n</section>\n<section id=\"i-can-t-log-in-when-i-enter-a-valid-username-and-password-it-brings-up-the-login-page-again-with-a-please-enter-a-correct-username-and-password-error\">\n<h2>我无法登陆。当我输入有效的用户名和密码后，它又跳转到了登录界面，并且显示一个“请输入正确的用户名和密码”的错误。<a class=\"heading-anchor\" href=\"#i-can-t-log-in-when-i-enter-a-valid-username-and-password-it-brings-up-the-login-page-again-with-a-please-enter-a-correct-username-and-password-error\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>如果你确定你的用户名和密码都是对的，请检查你的用户账户中的“django.contrib.auth.models.User.is_active”和“django.contrib.auth.models.User.is_staff”被设为True。管理员页面只允许这两项同时被设为True的用户访问。</p>\n</section>\n<section id=\"how-do-i-automatically-set-a-field-s-value-to-the-user-who-last-edited-the-object-in-the-admin\">\n<h2>在管理面板中，如何将字段的值自动设置为最后编辑它的用户？<a class=\"heading-anchor\" href=\"#how-do-i-automatically-set-a-field-s-value-to-the-user-who-last-edited-the-object-in-the-admin\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><code class=\"xref py py-class docutils literal notranslate\">save_model`钩子（hook）来更新一个反映用户编辑了的对象。\n参见：ref:`the documentation on ModelAdmin methods 1</code> 中的示例。</p>\n</section>\n<section id=\"how-do-i-limit-admin-access-so-that-objects-can-only-be-edited-by-the-users-who-created-them\">\n<h2>我们如何设定管理权限，以便对象只能由创建它们的用户进行编辑？<a class=\"heading-anchor\" href=\"#how-do-i-limit-admin-access-so-that-objects-can-only-be-edited-by-the-users-who-created-them\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><a class=\"reference internal\" href=\"/zh-hans/2.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin\" title=\"django.contrib.admin.ModelAdmin\"><code class=\"xref py py-class docutils literal notranslate\">ModelAdmin</code></a> 类也提供了自定义沟子(Hook)来控制管理面板中的对象是否可见以及可修改。使用相同的技巧从 request 中提取出当前用户对象，然后自定义:meth:~django.contrib.admin.ModelAdmin.get_queryset 和 <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.has_change_permission\" title=\"django.contrib.admin.ModelAdmin.has_change_permission\"><code class=\"xref py py-meth docutils literal notranslate\">has_change_permission()</code></a> 来控制管理面板中的对象是否可见以及可修改。</p>\n</section>\n<section id=\"my-admin-site-css-and-images-showed-up-fine-using-the-development-server-but-they-re-not-displaying-when-using-mod-wsgi\">\n<h2>管理界面的 CSS 和图片在开发模式下正常加载，但是在使用 mod_wsgi 进行部署的情况下却无法进行加载？<a class=\"heading-anchor\" href=\"#my-admin-site-css-and-images-showed-up-fine-using-the-development-server-but-they-re-not-displaying-when-using-mod-wsgi\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>请仔细阅读“如何通过 mod_wsgi 部署 Django”下的&lt;serving-the-admin-files&gt;“部署 admin 模块的静态文件”</p>\n</section>\n<section id=\"my-list-filter-contains-a-manytomanyfield-but-the-filter-doesn-t-display\">\n<h2>我的“list_filter”包含一个 ManyToManyField (多对多字段)，但过滤器不显示<a class=\"heading-anchor\" href=\"#my-list-filter-contains-a-manytomanyfield-but-the-filter-doesn-t-display\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>如果相关联对象少于 2 个，Django 则不会显示``ManyToManyField(多对多字段)``的过滤器</p>\n<p>例如，如果您的：attr：~django.contrib.admin.ModelAdmin.list_filter`包含：doc：`sites 1，并且你的数据库中只有一个站点，则不会显示“站点”过滤器。在这种情况下，按网站过滤将毫无意义。</p>\n</section>\n<section id=\"some-objects-aren-t-appearing-in-the-admin\">\n<h2>有些对象没有在管理中出现。<a class=\"heading-anchor\" href=\"#some-objects-aren-t-appearing-in-the-admin\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>不一致的行计数可能由缺少外键值或由将外键字段错误地设置为：attr：null = False &lt;django.db.models.Field.null&gt;`造成。如果你有一个指向不存在对象的 class：`~django.db.models.ForeignKey 的记录，并且包含的​​外键是：attr：~django.contrib.admin.ModelAdmin.list_display，将不会显示在管理更改列表中，因为 Django 模型正在声明未作用在数据库级别的完整性约束。</p>\n</section>\n<section id=\"how-can-i-customize-the-functionality-of-the-admin-interface\">\n<h2>如何个性化管理员后台接口的功能？<a class=\"heading-anchor\" href=\"#how-can-i-customize-the-functionality-of-the-admin-interface\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>你有几个选择。 如果您想要在 Django 自动生成的添加/更改表单上加载，您可以通过模型的类 Admin：ref：<a href=\"#id1\"><span class=\"problematic\" id=\"id2\">`</span></a>js parameter &lt;modeladmin-asset-definitions&gt;`将任意 JavaScript 模块附加到页面上。 该参数是一个 URL 列表，作为字符串，指向将通过“&lt;script&gt;”标签包含在管理表单中的 JavaScript 模块。</p>\n<p>如果您想要比简单调整后自动生成的表单更灵活，请随时为 Admin 模块编写自定义视图。 Admin 模版是由Django 官方提供支持的模块，你可以编写自定义的视图关联到认证，检查权限，并完成任何他们需要做的业务逻辑。</p>\n<p>如果您想自定义管理界面的外观，请阅读下一个问题</p>\n</section>\n<section id=\"the-dynamically-generated-admin-site-is-ugly-how-can-i-change-it\">\n<h2>动态生成的管理页面太丑了。我该怎么换掉它？<a class=\"heading-anchor\" href=\"#the-dynamically-generated-admin-site-is-ugly-how-can-i-change-it\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>如果你不喜欢它，你可以通过修改 CSS 样式文件以及关联的图片来定制管理员界面。这个网站使用 HTML 语法和大量的 CSS 钩子，所以任何你想要的改变都应该可以通过修改 CSS 样式表来实现。</p>\n</section>\n<section id=\"what-browsers-are-supported-for-using-the-admin\">\n<h2>Admin 适配了哪些浏览器？<a class=\"heading-anchor\" href=\"#what-browsers-are-supported-for-using-the-admin\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>除了 IE6，admin 为 YUI's A-grade 浏览器提供了一个全功能的体验。</p>\n<p>在支持的浏览器之间可能会有细微的风格差异，例如，某些浏览器可能不支持圆角。在渲染页面的时候会认为这些是可接受的差异。</p>\n</section>","rootId":"faq-the-admin","toc":[{"title":"无法登录。当我输入有效的用户名和密码后，页面再跳转到登录页面，没有显示任何错误信息。","anchor":"i-can-t-log-in-when-i-enter-a-valid-username-and-password-it-just-brings-up-the-login-page-again-with-no-error-messages","children":[]},{"title":"我无法登陆。当我输入有效的用户名和密码后，它又跳转到了登录界面，并且显示一个“请输入正确的用户名和密码”的错误。","anchor":"i-can-t-log-in-when-i-enter-a-valid-username-and-password-it-brings-up-the-login-page-again-with-a-please-enter-a-correct-username-and-password-error","children":[]},{"title":"在管理面板中，如何将字段的值自动设置为最后编辑它的用户？","anchor":"how-do-i-automatically-set-a-field-s-value-to-the-user-who-last-edited-the-object-in-the-admin","children":[]},{"title":"我们如何设定管理权限，以便对象只能由创建它们的用户进行编辑？","anchor":"how-do-i-limit-admin-access-so-that-objects-can-only-be-edited-by-the-users-who-created-them","children":[]},{"title":"管理界面的 CSS 和图片在开发模式下正常加载，但是在使用 mod_wsgi 进行部署的情况下却无法进行加载？","anchor":"my-admin-site-css-and-images-showed-up-fine-using-the-development-server-but-they-re-not-displaying-when-using-mod-wsgi","children":[]},{"title":"我的“list_filter”包含一个 ManyToManyField (多对多字段)，但过滤器不显示","anchor":"my-list-filter-contains-a-manytomanyfield-but-the-filter-doesn-t-display","children":[]},{"title":"有些对象没有在管理中出现。","anchor":"some-objects-aren-t-appearing-in-the-admin","children":[]},{"title":"如何个性化管理员后台接口的功能？","anchor":"how-can-i-customize-the-functionality-of-the-admin-interface","children":[]},{"title":"动态生成的管理页面太丑了。我该怎么换掉它？","anchor":"the-dynamically-generated-admin-site-is-ugly-how-can-i-change-it","children":[]},{"title":"Admin 适配了哪些浏览器？","anchor":"what-browsers-are-supported-for-using-the-admin","children":[]}],"breadcrumbs":[{"docname":"faq/index","title":"Django FAQ","url":"/zh-hans/2.0/faq/"}],"prev":{"docname":"faq/models","title":"FAQ：数据库和模型","url":"/zh-hans/2.0/faq/models/"},"next":{"docname":"faq/contributing","title":"FAQ: 贡献代码","url":"/zh-hans/2.0/faq/contributing/"},"formats":{"html":"/zh-hans/2.0/faq/admin/","markdown":"/zh-hans/2.0/faq/admin.md","json":"/zh-hans/2.0/faq/admin.json"},"source":"https://github.com/django/django/blob/stable/2.0.x/docs/faq/admin.txt","official":"https://docs.djangoproject.com/zh-hans/2.0/faq/admin/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}