Django: Adding social authentication to site - python

I'm a beginner in Django and I'm studying Django from the book "Django 2 by Example", written by Antonio Mele. I'm trying to add social authentication to my site(book page no. 251) {project name-bookmark, app name-account}. Up to this point, I'm installed the Django package
"social-auth-app-django",
synced python-social-auth models with database using migration and added "social_django" to the INSTALLED_APPS settings of my project. Then I've added
path('social-auth/', include('social_django.urls', namespace='social')),
now it is telling me to edit my hosts file located at "C:\Windows\System32\Drivers\etc\hosts" and to add "127.0.0.1 mysite.com" line which I've added. After this running server "http://example.com:8000/account/login/" should give error, here example.com is actually mysite.com,
DisallowedHost at /account/login:
Invalid HTTP_HOST header:'mysite.com:8000'.You may need to add 'mysite.com' to ALLOWED_HOSTS.
But I don't know why it isn't showing instead of that it gives me "This site can’t be reached. mysite.com refused to connect."
But it works perfectly fine at 127.0.0.1:8000.
Please help me I'm stuck. I've searched on google as well as watched videos on youtube but still, this error can't be solved(generated) by me.

Edit the hosts file in your etc folder.
Don't add the # on the line that contains your new line adding mysite.com

Related

Django 400 Bad request after pulling update from Github

I had a working page running Django with channels via Nginx, Daphne, Redis, and Gunicorn. After pulling an update from Github my index page now shows "Bad Request (400)" with nothing helpful in the console except for missing favicon. My settings.py has DEBUG = False and ALLOWED_HOSTS = ['AWS_IP']. Can anyone help me figure out what might be causing this?

social_django, redirect_uri is incorrect, vk backend

After migration from python-social-auth to split social according to this file I get 401 error (DEBUG=False) when somebody want to sign in using VK backend.
401 Client Error: Unauthorized for url: https://oauth.vk.com/access_token
Exception Location: /usr/local/lib/python3.5/dist-packages/requests/models.py in raise_for_status, line 844
What I have tried to far:
I have tried to reset VK app keys and change them in settings.
Also I have checked the redirect URI's in VK app and they are the same as in the request.
I think the problem is not with VK app setup but most probably in missing settings inside of split social Django app. Any ideas are welcome!
The issue was solved after
I had two redirect URI's in my VK app settings. (one with "www", another without) I have removed one.
I added again "'social.apps.django_app.default' into the settings.py

Web2py: auth user image field Error 2

I try to add a few extra fields in web2py's auth user register form. The part auth.setting.extra_fields['auth_user'] is what I added to models/db.py
auth = Auth(db)
crud, service, plugins = Crud(db), Service(), PluginManager()
auth.settings.extra_fields['auth_user'] = [
Field('address'),
Field('city'),
Field('zip'),
Field('image','upload')
]
## create all tables needed by auth if not custom tables
auth.define_tables(username=False, signature=False)
All fields except the image field seem work fine.
But when I try to upload an image to 'Apply Changes',
I got IOERROR Error2 No such file or directory: "....jpg"
The web server runs locally and the image is in my computer.
Am I missing any thing?
Thanks!
I try to create a new app and add this :
auth.settings.extra_fields['auth_user'] = [
Field('address'),
Field('city'),
Field('zip'),
Field('image','upload')
]
in app/models/db.py like you did as far as I can see.
Then I create a new user with appadmin and insert a image in my image field of type upload. It worked out of the box with web2py 2.4.7
Maybe provide the version of web2py you use could help to find the issue you face.
Your problem maybe be coming from permissions or web server configuration, but without detailed informations about you setup, I can't help.
Note: You could have more help on the mailing-list here :
https://groups.google.com/forum/#!forum/web2py
The community of web2py is very freindly and helping.
Cheers
Richard

Serving different urlconfs in a single Django project

My question is about how to serve multiple urls.py (like urls1.py, urls2.py and so on) files in a single Django project.
I am using Win7 x64, django 1.4.1, python 2.7.3 and as a server django dev-server tool.
I have decided to use a method which i found by google from
http://effbot.org/zone/django-multihost.htm
I have created a multihost.py file and put in to the django middleware folder:
C:\python27\Lib\site-packages\django\middleware\multihost.py
With the following code:
from django.conf import settings
from django.utils.cache import patch_vary_headers
class MultiHostMiddleware:
def process_request(self, request):
try:
host = request.META["HTTP_HOST"]
if host[-3:] == ":80":
host = host[:-3] # ignore default port number, if present
request.urlconf = settings.HOST_MIDDLEWARE_URLCONF_MAP[host]
except KeyError:
pass # use default urlconf (settings.ROOT_URLCONF)
def process_response(self, request, response):
if getattr(request, "urlconf", None):
patch_vary_headers(response, ('Host',))
return response
Also in my project setting.py file i have added a mapping dictionary like the link above shows:
# File: settings.py
HOST_MIDDLEWARE_URLCONF_MAP = {
"mysite1.com": "urls1",
#"mysite2.com": "urls2"
}
I did not yet implemented the error handling like described by the link above.
My hosts file includes the follwoing:
127.0.0.1 mysite1.com
Project structure is the following:
effbot django project folder:
+ effbot
--- settings.py
--- view.py
--- wsgi.py
--- urls.py
--- urls1.py
+ objex
--- models.py
--- views.py
+ static
--- css
--- images
There is no templates folder, because i dont serve html items from files, they are coming from databsse (i doubt that my problem is in this).
Now the problem is: when i go for the adress
mysite1.com
in my browser with django dev-server launched i get code 301 from the server. And browser shows "cannot display page" message.
Could you please explain me how to use mentioned method? I'm new to django and haven't done any real projects yet. Just have read the docs and launched a couple of sites at home to learn how it works.
I expect that urlconfs will be called in dependance from incoming
request.META["HTTP_HOST"]
The target is to serve different urlconfs for mysite1.com and mysite2.com
in a single django project.
I think this should to work some how.
Thank you for any feedback.
EDIT:
After some research attempts i found that i plugged my multyhost.py incorrectly in settings.
Fixed now. But the same result still.
Also i found out that my django dev-server tool is not reflecting anyhow that it handles any requests from the browser (IE9) except when i do "http://127.0.0.1".
May be i have to try some production server for my task, like nginx?
Your ROOT_URLCONF should be effbot.urls without .pyas you can see in the example in the documentation.
Also, HOST_MIDDLEWARE_URLCONF_MAP should reflect the ROOT_URLCONF so add `effbot. like this:
HOST_MIDDLEWARE_URLCONF_MAP = {
"mysite1.com": "effbot.urls1",
#"mysite2.com": "effbot.urls2"
}
One more thing, please try with another browser (Chrome, Firefox), sometimes I had problems accessing dev server with IE.

django-social-auth twitterBackend , always redirected to LOGIN_URL_ERROR

I started using django-social-auth which looks great, i want to setup a tiny twitter app and just need the twitter connect. I followed the guidelines and examples.
The twitter connect looks like it's working, i am redirected to twitter authentication but when returning back to my callbackUrl i get
always redirected to the LOGIN_URL_ERROR, i have no error messages. I tried adding django logging still no error is showed.
The messages module used in the example views gives me:
"Sorry but some error made you impossible to login. Please try again"
I am currently testing on localhost so maybe it's the problem, i have setup the twitter app with the url: local.dev:8000 and callbackUrl: local.dev:8000/filtered
with a mapping on /etc/hosts
settings.py
social_settings.py
views.py
urls.py
python2.7, django 1.3, django-social-auth github repo

Categories

Resources