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
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.
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
Related
I started django server with following command:
python manage.py runserver 192.168.31.79:8030
when I use following code,
from django.contrib.sites.models import Site
current_site = Site.objects.get_current()
print (current_site.domain)
the result will be 192.168.31.79:8000, why not port is 8030? Anyone could explain it for me? how could I retrieve 192.168.31.79:8030 via coding. many thanks.
When resolving the Site with get_current Django will first check if a SITE_ID is set in the settings file. If there is a SITE_ID it will return what is stored in the domain field (obtaining the cached version in SITE_CACHE or adding it to the cache and returning it).
If SITE_ID is not present Django will resolve the host with request.get_host() and first query to see if the domain exists within the cache then check if it is in a Site object.
If this does not produce a result it will remove the port and check again in the same manner.
I would try looking at each of your Site objects to see if the port has made it into the domain. If not look into what is being stored in SITE_CACHE.
Documentation at https://docs.djangoproject.com/en/dev/ref/contrib/sites/#django.contrib.sites.shortcuts.get_current_site
I'm trying to activate social logins in my Django web application, which comes from open source software in this GitHub repository (so I didn't write it); and am running into this well-known issue:
DoesNotExist: SocialApp matching query does not exist.
The base settings file is located here. I do not modify that file at all. Instead, I import (inherit) it at the top of my deploy.py settings file, and make overrides and customization there.
Specifically related to this issue, here are the relevant overrides and additions that I made in deploy.py to enable Google and Twitter social authentication, both of which result in the same error:
INSTALLED_APPS.remove('allauth.socialaccount.providers.persona') # Remove
INSTALLED_APPS.append('allauth.socialaccount.providers.google') # Add
INSTALLED_APPS.append('allauth.socialaccount.providers.twitter') # Add
_GOOGLE = {
'SCOPE': ['email', 'https://www.googleapis.com/auth/userinfo.profile'],
'AUTH_PARAMS': {'access_type': 'online'},
'PROVIDER_KEY': get_env("GOOGLE_PROVIDER_KEY"), # Stored in secrets.env
'PROVIDER_SECRET_KEY': get_env("GOOGLE_PROVIDER_SECRET_KEY"), # Stored in secrets.env
}
SOCIALACCOUNT_PROVIDERS['google'] = _GOOGLE # This isn't enabled in biostar.settings.base
_TWITTER = {
'SCOPE': ['email'],
'AUTH_PARAMS': {'access_type': 'online'},
'PROVIDER_KEY': get_env("TWITTER_PROVIDER_KEY"), # Stored in secrets.env
'PROVIDER_SECRET_KEY': get_env("TWITTER_PROVIDER_SECRET_KEY"), # Stored in secrets.env
}
SOCIALACCOUNT_PROVIDERS['twitter'] = _TWITTER
I show two provider examples here -- Twitter and Google -- to show the pattern of what I am doing, and to show that the issue isn't provider-specific; though let's try to focus on just Twitter to keep things simple.
Now according to this document -- which comes from a close fork of the above project -- in addition to what I implemented above (programmatically), it is necessary to also set up the same Social Accounts and the Keys/Secret-keys for them in the Social Apps section of the Django Admin panel. (Note that that fork was worked on by more-or-less the same team; so this requirement likely applies to the original upstream implementation, as well). The relevant part of that document instructs the following:
After restoring, you need to re-enter social login info.
Unfortunately, even though this information is required to be in the config
environment that initializes biostar, it also requires it
(redundantly) to be in the database as well. So go to the Django Admin panel
and click on Social Apps and then go through each app and fill in the
appropriate values.
So I went ahead and did that, too.
No matter what I try (different providers, different keys, django panel additions, no django panel additions, etc), I get the aforementioned exception, as though I'm missing a step.
Incidentally, I tried both key-pairs for Twitter (because I always forget which pair to use; so tried both Consumer Key (API Key) + Consumer Secret (API Secret) and Access Token + Access Token Secret, though it's really the former from what I read online). Just mentioning this for completeness.
What am I missing? Any ideas?
Thank you in advance! :)
EDIT-1: Here is a full paste of the exception: https://pastebin.com/0UBAfAtu
I was getting the same error again and again and solve with this ..
The reason we get this error SITE_ID = 1
When "SocialApp matching query does not exist" occurs,
this ID needs to be replaced.
SITE_ID = 1
hope for anyone else facing this issue will get help for this.
Following below steps will resolve this issue:
Go to your database table named django_site.
Look at the id number of the site mentioned as a chosen site in the Social Applications.
Now configure SITE_ID to id number in setting.py of the Django project.
I had a similar problem and as people have said, you need to know the SITE_ID!
You have to make sure that on the Django admin dashboard, you have added your social application under SOCIAL ACCOUNTS.
When you're creating the social application, make sure that have listed the right websites for "chosen sites". If you're on local development, paste your computer IP in there. There will be a new SITE_ID associated with each "Chosen site" that you add to your application. Make sure you're using the right one that's associated to your IP. Personally, after adding my IP, I then just played and incremented my SITE_ID variable until it worked.
Posting this in case someone in a similar situation in the future lands on this page.
I'm currently developing 2 different sites at the same time: one of them is a heavily customized django-admin interface, and the other a "stand-alone" website that will share it's database with the previous one .
Even though they are related, I'd like my users not to loosely identify between the two sites : they are both able to be independant of the other.
However, a problem arises when someone is logged in the "admin" site : when they go to the other website, they are automatically logged. Won't happen the other way unless I allow it though, as the admin site requires special permissions in the User model.
I already created an UserProfile that can differentiate an user of one of the sites or of the both.
So, knowing all this, how can I make sure that the customers of the admin site don't get to be authenticated when in the other web site (without, of course, logging them out from the first one) ?
Thanks !
EDIT : To format it better, here is what I got , summed up :
One admin application / site Both running
One related application / site on same server,
sharing settings and urls.py
If some is logged in admin, I want to require them to create a new session to log on [related site] : this, without logging them off the admin site.
What changes should I do to this configuration to achieve this ?
Put different SESSION_COOKIE_DOMAIN and SESSION_COOKIE_NAME for each appication. Hope this solve your issue.
SESSION_COOKIE_DOMAIN = 'site1.com' #site2.com for other
SESSION_COOKIE_NAME = 'sid1' #sid2 for other
I am running a site live on heroku. My issuse occurs when I visit my site's admin located at www.mysite.com/admin. When I visit this page, I get
DoesNotExist at /admin/
Site matching query does not exist.
Thing is, my project /admin works locally as well as on my staging server. It just so happens when I visit it live, I get that error.
After looking around SOF, I tried doing running python manage.py shell with the following commands:
from django.contrib.sites.models import Site
Site.objects.create(pk=1, domain='www.xxx.com', name='xxx.com')
and I changed SITE_ID = 1 to SITE_ID = www.mysite.com and that just ended up breaking my staging server, so I changed it back and tried again without changing the SITE_ID.
What could be the issue?
It seems the SITE_ID setting in your settings file is pointing to an ID that doesn't exist in your database.
You either need to create a new site in the sites model (via admin) and change the ID in the settings file to match that one, or re-insert the item via SQL and give it the same ID as your settings file.
This problem will always seem to occur continually, so if you don't want to serve your model with multiple sites, then I suggest you remove the SITE_ID from the setting file. Note this worked for me!
I just had to remove the 'django.contrib.sessions' from INSTALLED_APPS in settings.py as it was causing problems and I was using this project only for one site.
However, if you are using your project for multiple different sites and need the Django sites framework, please check SITE_ID in your database and if it matches the one defined in the settings.
I have a server with two Django/FeinCMS backends. One is on a subdomain like sub.domain.com and the other on domain.com.
For each backend I created a page (one called "home", one called "frontpage") and told them to overwrite the url with "/".
Now I have a very strange problem:
Ca every 5min the frontpage of the subdomain works and the frontpage of the domain not. This behaviour changes after 5min so that the frontpage of the domain works and the frontpage of the subdomain doesn't.
I don't know where I should start to look for a mistake. Maybe someone of you had the same error and knows a solution.
Thanks for your help
Ps: By not working I mean that the user sees another page from the site when he goes to www.domain.com or sub.domain.com
PPs: in the subdomain settings.py I have:
ALLOWED_HOSTS = (
# main host
'sub.domain.com',
)
And in the domain settgins.py I have:
ALLOWED_HOSTS = (
# main host
'.domain.com',
# alias hosts
'.other-domain.com',
'.other-domain2.com',
)
Might this be the problem?
It was a cache collision, because I used the same backend twice and just altered one of them.