如何让Django管理员的“站点视图”链接工作?

时间:2022-06-02 07:22:16

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:

我使用Django应用程序已经有一段时间了,Django管理界面非常好用,除了“站点视图”链接不起作用。每当我尝试使用它时,我都会得到一个OperationalError消息:没有这样的表:django_site。我已经对这个问题做了一些研究,似乎我必须为这个链接建立Django站点框架,但是我非常确定该怎么做。文档讨论了数据库表等等,但它不知道如何实际建立一个站点。我的问题有两个方面

  1. 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?
  2. 如何设置站点框架?我需要自己创建表(并输入数据),还是需要启用某些东西。py syncdb会自动“检测”我想要设置的表吗?
  3. 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 like SITE_ID = 2 if DEBUG else 1, or will manage.py just detect that I'm working on the debug site and not do anything with the sites framework?
  4. 当我在本地开发时,建立网站框架会对我产生影响。,只在本地主机上运行,而不退出我的实际域名)?我需要在设置中添加一些东西吗?像SITE_ID = 2如果调试else 1,或者将要管理。py只是检测到我正在调试站点而没有对站点框架做任何事情?

4 个解决方案

#1


9  

Putting

'django.contrib.sites',

into your INSTALLED_APPS and a following

进入你的INSTALLED_APPS和下面。

$ ./manage.py syncdb

may suffice.

可能就足够了。

When installed, edit the Site instance (e.g. through /admin interface) to reflect your local hostname (e.g. localhost:8000).

安装后,编辑站点实例(例如通过/admin界面)以反映本地主机名(例如localhost:8000)。

#2


11  

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.

在模型上定义get_absolute_url。管理员使用该方法来确定如何构造对象url。看文档。

#3


1  

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.

正如其他人所传达的,除了启用view_on_site之外,这还需要一些额外的步骤。您必须在模型中实现get_absolute_url(),并在项目设置中启用站点。

Set the view_on_site setting

Add view_on_site setting to admin form:

将view_on_site设置添加到管理表单:

class MymodelAdmin(admin.ModelAdmin):
    ...
    view_on_site = True
...
admin.site.register(Mymodel, MymodelAdmin)

Implement get_absolute_url()

Add get_absolute_url() to your model. In models.py:

将get_absolute_url()添加到模型中。在models.py:

Mymodel(models.Model):
    ...
    def get_absolute_url(self):
        return "/mystuff/%i" % self.id

Enable Sites

Add Sites in yourapp/settings.py:

添加网站yourapp / settings.py:

INSTALLED_APPS = (
    ...
    'django.contrib.sites',
    ...
)

Then update the database:

然后更新数据库:

$ python manage.py migrate

Done!

Check out reverse() for a more sophisticated way to generate the path in get_absolute_url().

查看reverse(),以获得在get_absolute_url()中生成路径的更复杂的方法。

#4


0  

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.

当您在设置中编辑了SITE_ID时。py或站点实例认为管理员,不要忘记重新启动您的web服务器以使更改生效。

#1


9  

Putting

'django.contrib.sites',

into your INSTALLED_APPS and a following

进入你的INSTALLED_APPS和下面。

$ ./manage.py syncdb

may suffice.

可能就足够了。

When installed, edit the Site instance (e.g. through /admin interface) to reflect your local hostname (e.g. localhost:8000).

安装后,编辑站点实例(例如通过/admin界面)以反映本地主机名(例如localhost:8000)。

#2


11  

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.

在模型上定义get_absolute_url。管理员使用该方法来确定如何构造对象url。看文档。

#3


1  

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.

正如其他人所传达的,除了启用view_on_site之外,这还需要一些额外的步骤。您必须在模型中实现get_absolute_url(),并在项目设置中启用站点。

Set the view_on_site setting

Add view_on_site setting to admin form:

将view_on_site设置添加到管理表单:

class MymodelAdmin(admin.ModelAdmin):
    ...
    view_on_site = True
...
admin.site.register(Mymodel, MymodelAdmin)

Implement get_absolute_url()

Add get_absolute_url() to your model. In models.py:

将get_absolute_url()添加到模型中。在models.py:

Mymodel(models.Model):
    ...
    def get_absolute_url(self):
        return "/mystuff/%i" % self.id

Enable Sites

Add Sites in yourapp/settings.py:

添加网站yourapp / settings.py:

INSTALLED_APPS = (
    ...
    'django.contrib.sites',
    ...
)

Then update the database:

然后更新数据库:

$ python manage.py migrate

Done!

Check out reverse() for a more sophisticated way to generate the path in get_absolute_url().

查看reverse(),以获得在get_absolute_url()中生成路径的更复杂的方法。

#4


0  

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.

当您在设置中编辑了SITE_ID时。py或站点实例认为管理员,不要忘记重新启动您的web服务器以使更改生效。