我怎样才能获得 Django 管理员的“现场查看” 链接到工作?
我使用 Django 应用程序已经有一段时间了,Django 管理界面运行良好,除了“在站点上查看”链接不起作用。 每当我尝试使用它时,都会收到一个 OperationalError
,其中包含消息:no such table: django_site
。 我对这个问题做了一些研究,似乎我必须设置 Django 站点框架 使此链接正常工作,但我完全确定如何做到这一点。 该文档讨论了数据库表等,但没有说明如何实际设置站点。 所以我的问题实际上有两个:
- 如何设置站点框架? 我是否必须自己创建表(并自己输入数据),或者是否必须启用某些功能,以便
./manage.pysyncdb
会自动“检测”我想要设置表? - 当我在本地开发时(即仅在本地主机上运行而不是在我的实际域名上运行),设置站点框架会影响我吗? 我是否必须在
settings.py
中添加一些内容,例如SITE_ID = 2 if DEBUG else 1
,或者manage.py
只是检测到我'我在调试站点上工作并且不对站点框架执行任何操作?
I've been working with a Django app for a while, and the Django admin interface works great, except that the "View on site" link doesn't work. Whenever I try to use it, I get an OperationalError
with the message: no such table: django_site
. I've done some research into this problem, and it seems that I have to set up the Django sites framework for this link to work, but I'm exactly sure how to do that. The documentation talks about database tables, etc., but it doesn't tell how to actually set up a site. So my question is really two-fold:
- How do I get the sites framework set up? Do I have to create the table myself (and enter the data myself), or is there something I have to enable so
./manage.py syncdb
will automagically "detect" that I want the table set up? - Will setting up the sites framework effect me when I'm developing locally (i.e., just running on localhost and not off my actual domain name)? Will I have to add something to
settings.py
likeSITE_ID = 2 if DEBUG else 1
, or willmanage.py
just detect that I'm working on the debug site and not do anything with the sites framework?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在您的模型上定义
get_absolute_url
。 管理员使用该方法来确定如何构造对象 url。 请参阅文档。Define a
get_absolute_url
on your model. The admin uses that method to figure out how to construct the objects url. See the docs.放入
您的 INSTALLED_APPS 和以下内容
就足够了。
安装后,编辑站点实例(例如通过
/admin
界面)以反映您的本地主机名(例如localhost:8000
)。Putting
into your INSTALLED_APPS and a following
may suffice.
When installed, edit the Site instance (e.g. through
/admin
interface) to reflect your local hostname (e.g.localhost:8000
).正如其他人所传达的,除了启用 view_on_site 之外,这还需要几个额外的步骤。 您必须在模型中实现 get_absolute_url() ,并在项目设置中启用站点。
设置 view_on_site 设置
将 view_on_site 设置添加到管理表单:
实现 get_absolute_url()
添加 get_absolute_url() 到您的模型。
在 models.py 中:
启用站点
在 yourapp/settings.py 中添加站点:
然后更新数据库:
完成!
查看 reverse() 了解更复杂的方法在 get_absolute_url() 中生成路径。
As communicated by others, this requires a couple extra steps in addition to enabling view_on_site. You have to implement get_absolute_url() in your model, and enable Sites in your project settings.
Set the view_on_site setting
Add view_on_site setting to admin form:
Implement get_absolute_url()
Add get_absolute_url() to your model.
In models.py:
Enable Sites
Add Sites in yourapp/settings.py:
Then update the database:
Done!
Check out reverse() for a more sophisticated way to generate the path in get_absolute_url().
根据 Django 文档,从 Django 3.1(2020 年 5 月)开始,您必须在模型中定义 get_absolute_url() 方法。
以下是文档中的示例:
来源:https ://docs.djangoproject.com/en/3.1/ref/models/instances/#get-absolute-url
According to the Django documentation, and as of Django 3.1 (May 2020), you have to define a get_absolute_url() method in your model.
Here is an example from the documentation:
Source: https://docs.djangoproject.com/en/3.1/ref/models/instances/#get-absolute-url
在我看来,只有当
get_absolute_url
引用 Django 视图时,站点功能视图才有效。 如果您尝试创建一个链接,该链接会重定向到 Django 控制之外的页面(即使它是由 apache 本身从同一域提供服务),那么它似乎不起作用。在这种情况下,可以通过覆盖管理模板来手动创建按钮,如下所示:
另外,将
view_on_site = False
添加到您的ModelAdmin
类中,否则两个按钮都会出现。It seems to me that the view on site functionality works only if
get_absolute_url
refares to a Django view. It does not seem to work if you are trying to create a link, which redirects to a page out of Django's control (even if it is served from the same domain by apache itself).In this case, it is easy to create the button manually by overriding admin tempale as follows:
Also, add
view_on_site = False
to yourModelAdmin
class, otherwise both of the buttons will appear.当您编辑了 settings.py 中的 SITE_ID 或管理员认为的站点实例时,请不要忘记重新启动您的 Web 服务器以使更改生效。
When you have edited either SITE_ID in settings.py or a Site instance thought the admin, don't forget to restart your web server for the change to take effect.