site domain issue in Django project - python

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

Related

Limit Google OAuth access to one domain using 'hd' param (Django / python-social-auth)

I'm building an internal webapp for my company to use and want to use our Google Apps domain to manage access from our company domain usernames (example.com for the rest of this question).
I'm using:
Django==1.9.5
python-social-auth==0.2.19
+ dependencies
From reading other SO questions I've discovered the Goog "hosted domain" (hd) parameter that can be used via the following setting:
SOCIAL_AUTH_GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {'hd': 'example.com'}
The parameter is being successfully appended to the initial request, I can see it in the URL before granting access.
However, it's not working as I'd expect. I've been able to successfully authenticate with two non-company email addresses.
Am I misunderstanding how the "hd=" parameter works or do I need to also limit access via the app somewhere else on the Google Admin dashboard? Or is it just not supported within the OAuth2 flow?
Thanks in advance for any help.
I know this is an older posting, but I found it searching for how to add extra url params to the OAuth2. I couldn't figure out how to set the hd parameter.
For your situation, set the whitelist to the domains you want to accept. They will be able to authenticate by adding their non-specified hosted domain account with Google, but will not get logged into your site. They will receive the AuthForbidden exception with message "Your credentials aren't allowed."
To set up the whitelist of domains accepted in your site, add the following to settings.py:
SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = ['domain.com', 'example.org']
You can also set up a friendlier page so they don't get the default 500 error page:
SOCIAL_AUTH_LOGIN_ERROR_URL = '/authentication_error/'
SOCIAL_AUTH_BACKEND_ERROR_URL = '/authentication_error/'
Update urls.py to have this url point to a view with a nice HTML page rendered to let them know what went wrong.
AUTH_EXTRA_ARGUMENTS = {'hd': 'example.com'}

Issue with Sites system in Django

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

Django project /admin Site matching query does not exist

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.

Authenticating user from SSO + OpenID in django for google apps custom domain

After a lot of reading and researching i found the following library, the most suited to work for my needs: to signup/login a user using a custom google apps domain.
I did the following settings:
GOOGLE_APPS_DOMAIN = 'example.com'
GOOGLE_APPS_CONSUMER_KEY = 'example.com'
GOOGLE_APPS_CONSUMER_SECRET = '*sekret*'
# domain where your application is running
GOOGLE_OPENID_REALM = 'http://*.hudora.biz/'
GOOGLE_OPENID_ENDPOINT = 'https://www.google.com/accounts/o8/ud'
added custom middlewares and other stuff.
But from now, where do i take it further, showing a form and making form posts to urls, how do i ask user information, it is not given in the docs.
Can anybody please help me with it?
How do i give a user the functionality to login/signup?
How do i customize a view?
You are almost there. For the sake of testing it on your localhost following are things you need to do:
You need to register an app at the Google API Console (while registering, put your domain 127.0.0.1:8000 or the exact location where your server is running)
You need to check the scope for your project, eg. calendar or maps etc.
You need to get the secret key.
Once you have got the secret, put them in your settings file, as described above. Change example.com and *.hudora.biz to 127.0.0.1:8000
Import the views from the library views for login and logout and map them to your urls probably login/logout.
This is all that is reqd. Hope it helps!

Allowing users to use custom domains for Django app on Heroku

I have a Django app hosting on Heroku. In the app, the users create pages at http://domain.com/username
I'd like to give users the option to use their own domain name for their page using a CNAME. Ideally I'd like to avoid an A-Record in case I change hosts in the future and my IP changes.
This is completely new territory for me and dont even know where to start, or what to look for. Does anyone have a suggestion on where to start? I've seen mention of Wildcard DNS, but not sure how that ties into my app.
Any suggestions would be really appreciated.
Prelim Answer:
If you control the nameserver for the domain and have access to the RNDC Key, you can use the post-signup view/signal to squirt out a cname to your DNS server that will resove username.yoursite.com to yoursite.com. Make sure apache is set up to recieve a wildcard virtualhost to the correct app, and then use a custom middleware to read request.META['SERVER_NAME'].lsplit('.')[0] to see what the subdomain is. You can then use this information in your views to differentiate user subdomains.

Categories