Introduction to Django 3.0 - Building, Authenticating, and Deploying - Part 2

Learn to build Django web applications in minutes and deploy them to production

Brought to you by Vihar Kurama (Guest Author)

Read more

Second and last part of Django 3.0! What are your thoughts guys? Share it in the comments :speech_balloon:

@robertino.calcaterra Doesn’t seem to work properly. While running locally (before Heroku steps), clicking the “Log In” link to bring up the login page produces an error.

File "\codeshare\auth0login\auth0backend.py", line 15, in authorization_url
return 'https://' + self.setting('DOMAIN') + '/authorize'
TypeError: can only concatenate str (not "NoneType") to str

FYI, I’m being taken to http://localhost:8000/login/auth0/ for the login page. Not sure if that’s correct but it doesn’t feel correct. I’ve gone through every step multiple times but cannot find any mistakes in my code. Starting to think there’s a bug in the tutorial but I am not sure where.

Hey there @pdipatrizio!

One of our Guest Authors (Vihar Kurama) wrote that blog article. We already reached out to him so either someone from the team responsible for blog or the article author himself will reach out to you soon regarding your hurdle!

1 Like

Hwy @pdipatrizio, it seems like your setting("Domain") variable is returning a "None" type instead of your application domain. To resolve this issue, make sure you use the right values for Auth0 keys that you setup in .env file. You can find them in the Auth0 Dashboard after creating your application. Let me know if you’re encountering any other errors. Thanks!

3 Likes

Hey Vihar. Thanks for the reply. I had the correct Auth0 domain and keys but it turned out my .env file wasn’t being found. I’m still not 100% sure why but I was able to get it to work by moving the .env into the same folder as settings.py and calling load_dotenv() without setting the path. I also edited the path in .gitignore to reflect the new location. Thanks again!

3 Likes

No worries! We’re here for you!

Small tip for others who may want to know how to generate their own requirements.txt. You can run pip freeze in a terminal to get a list of packages/versions installed in your environment. Or to keep a list updated dynamically, you could use something like:

import pkg_resources
installed_packages = pkg_resources.working_set
installed_packages_list = sorted([(str(i).replace(' ', '==')) for i in installed_packages], key=str.lower)
[print(i) for i in installed_packages_list]

The pip freeze command misses a couple of packages but they may not be necessary to list as requirements. I’m not quite sure :man_shrugging:

Thanks again for the guide!

3 Likes

Thanks for sharing that with the rest of community!

Pretty sure this code snippet is wrong:

# ga-django-python/codeshare/settings.py

AUTHENTICATION_BACKENDS = {
    "auth0login.auth0backend.Auth0",
    "django.contrib.auth.backends.ModelBackend",
}

it should be:

# ga-django-python/codeshare/settings.py

AUTHENTICATION_BACKENDS = [
    "auth0login.auth0backend.Auth0",
    "django.contrib.auth.backends.ModelBackend",
]
1 Like

I’m not sure if there’s something wrong with my eyes but those two are the same.

1 Like

One of them is surrounded by {} and one by . Different datatypes.

1 Like

Ohh gosh! Gotchya thanks for sharing it!

Hey @mark.jones, there’s no error in the code. According to the comment raised both the data-type’s lists or dicts will work in the production environment. Also, all those settings were automatically generated by Django itself based on the version you’re using. Let me know if you’re still facing any problems.

Thanks :slight_smile:

1 Like

Thanks for reaching out Vihar!

1 Like

Hi Vihar,

Thank you for sharing this tutorial. Unfortunately I am confused with the Login_URL thing.
I am not sure which URL I will have to mention there ? I created the app name SLMS_APP1 and you created the app - with the name auth0login

python3 manage.py startapp auth0login

I am now getting an error -

ModuleNotFoundError at /login/auth0
No module named ‘auth0login’
Request Method: GET
Request URL: http://127.0.0.1:8000/login/auth0
Django Version: 3.1
Exception Type: ModuleNotFoundError
Exception Value:
No module named ‘auth0login’
Exception Location: /Volumes/My_Personal/Programming/DJANGO/Smart_Leave_Management_system/lib/python3.8/site-packages/social_core/utils.py, line 56, in import_module
Python Executable: /Volumes/My_Personal/Programming/DJANGO/Smart_Leave_Management_system/bin/python3
Python Version: 3.8.4
Python Path:
[’/Volumes/My_Personal/Programming/DJANGO/Smart_Leave_Management_system’,
‘/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip’,
‘/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8’,
‘/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload’,
‘/Volumes/My_Personal/Programming/DJANGO/Smart_Leave_Management_system/lib/python3.8/site-packages’]
Server time: Sat, 29 Aug 2020 21:46:52 +0000

modules installed on my environment is -

asgiref==3.2.10
certifi==2020.6.20
cffi==1.14.2
chardet==3.0.4
cryptography==3.1
defusedxml==0.7.0rc1
Django==3.1
idna==2.10
oauthlib==3.1.0
pycparser==2.20
PyJWT==1.7.1
python3-openid==3.2.0
pytz==2020.1
requests==2.24.0
requests-oauthlib==1.3.0
six==1.15.0
social-auth-app-django==4.0.0
social-auth-core==3.3.3
sqlparse==0.3.1
urllib3==1.25.10

1 Like

Hello, welcome to the Auth0 Community. Thank you for reading the blog post. I’ve shared your question with the blog post author :slight_smile:

1 Like

Sorry I have resolved this. I didn’t mention the .env file path in settings.py and after mentioning this it worked.

ENV_FILE = find_dotenv()
if ENV_FILE:
load_dotenv(ENV_FILE)

2 Likes

Perfect glad to hear that and thanks for sharing with the rest of community!

Hey @trickyj, glad you’ve resolved the issue. Also make sure to add your app name in settings.py file, in your case it’s SLMS_APP1. I also hope you figured all the URIs and Keys that you need to export in the .env file. Let us know if you have any questions further.

Happy coding :slight_smile: !

1 Like