Reputation: 459
I have an issue with the Sites system in Django. My problem is when running Site.objects.get_current()
on some pages on initial load of the development server I get an exception saying Site matching query does not exist.
however if I load the main page of the site it loads just fine then I can go back to any other page with the exception and they load fine as well.
Has anyone come across this issue before?
Thanks,
Nick
Upvotes: 0
Views: 69
Reputation: 28702
get_current
looks at the current hostname and detects whether you have a Site
object with a matching domain name.
Perhaps on development you are using localhost
or something similar. You need to create a Site
object with the same domain name, i.e., via something like this which could be added to a migration:
if settings.DEBUG:
development_site_domain = 'localhost' # Or whatever yours is.
Site.objects.create(
name=development_site_domain,
domain=development_site_domain)
You must have a Site
object in the database for any FQDN you access your app via. You also need to include the FQDN in your ALLOWED_HOSTS
setting.
The alternative is to set SITE_ID = 1
(or any pk of an existing SITE_ID
in your database). The drawback here is that your server will respond differently on local vs. on the server if you're using a dynamic SITE_ID
on production.
Upvotes: 1
Reputation: 6424
add SITE_ID = 1
to your settings.py,
and run python manage.py syncdb
to create corresponding tables if not exist, this will work
and then, you could login into your admin site:
click Site
to modify defalut example.com
to yours, this used when you edit an object, It will provide a View on site
button if you defined a get_absolute_url
function in your models.py
Upvotes: 0