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?
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.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.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.
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().
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
).