Django,Template-loader postmortem - django

Django does not see my registration.templates folder.I do not understand where the problem lies.
Why is he looking foe base.html?
This is how I specified my templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'store/templates/registration')],
My store directory
tree .
.
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│   ├── 0001_initial.py
│   ├── 0002_auto_20180604_0751.py
│   ├── __init__.py
│   └── __pycache__
│   ├── 0001_initial.cpython-36.pyc
│   ├── 0002_auto_20180604_0751.cpython-36.pyc
│   └── __init__.cpython-36.pyc
├── models.py
├── __pycache__
│   ├── admin.cpython-36.pyc
│   ├── __init__.cpython-36.pyc
│   ├── models.cpython-36.pyc
│   ├── urls.cpython-36.pyc
│   └── views.cpython-36.pyc
├── templates
│   ├── registration
│   │   ├── activate.html
│   │   ├── activation_complete.html
│   │   ├── activation_email_subject.txt
│   │   ├── activation_mail.txt
│   │   ├── registration_complete.html
│   │   └── registration_form.html
│   ├── store.html
│   └── template.html
├── tests.py
├── urls.py
└── views.py
urls.py
urlpatterns = [
url(r'^', include('store.urls')),
path('accounts/', include('registration.backends.default.urls')),
path('admin/', admin.site.urls),
]

Related

ImportError at / cannot import name 'register_new_org_view' from 'users.views' (/home/ubuntu/xyz/users/views.py)

I am having trouble with my gunicorn, nginx deployed django application.
Everything runs smoothly on my local development and python manage.py check does not throw any errors. However, as soon as I deploy my application to my server this error is thrown:ImportError at / cannot import name 'register_new_org_view' from 'users.views' (/home/ubuntu/xyz/users/views.py)
I have defined the view and import it correctly... other views from other apps are working perfectly fine... what can be the reason?
Thank you so much for your help!
Update1: Project Tree/Structure:
xyz_Django
│   ├── __init__.py
│   ├── __pycache__
│   ├── asgi.py
│   ├── xyz_django_env
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── landingpage
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── templates
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
├── organization
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── decorators.py
│   ├── forms.py
│   ├── migrations
│   ├── models.py
│   ├── templates
│   ├── tests.py
│   └── views.py
├── overview
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── custom_HTML_calendar.py
│   ├── forms.py
│   ├── migrations
│   ├── models.py
│   ├── overview_helper.py
│   ├── templates
│   ├── testing_file.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── requirements.txt
├── slack_application
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   ├── models.py
│   ├── slack_application_helpers.py
│   ├── slack_notifications.py
│   ├── slack_notifications_daily_afternoon.py
│   ├── slack_notifications_daily_morning.py
│   ├── slack_notifications_weekly_friday.py
│   ├── tests.py
│   └── views.py
├── templates
│   ├── account
│   └── socialaccount
└── users
├── __init__.py
├── __pycache__
├── adapter.py
├── admin.py
├── apps.py
├── forms.py
├── migrations
├── models.py
├── templates
├── tests.py
├── tokens.py
├── user_automation_on_submission.py
├── user_automations_daily_update.py
└── views.py

Issue with static files when deploying Django app to Heroku

My project folder looks this:
├── Procfile
├── core
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-39.pyc
│   │   └── views.cpython-39.pyc
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── inspirationSources.txt
├── manage.py
├── package-lock.json
├── package.json
├── react-frontend
│   ├── README.md
│   ├── build
│   │   ├── asset-manifest.json
│   │   ├── favicon.ico
│   │   ├── index.html
│   │   ├── logo192.png
│   │   ├── logo512.png
│   │   ├── manifest.json
│   │   ├── robots.txt
│   │   └── static
│   ├── package-lock.json
│   ├── package.json
│   ├── public
│   │   ├── favicon.ico
│   │   ├── index.html
│   │   ├── logo192.png
│   │   ├── logo512.png
│   │   ├── manifest.json
│   │   └── robots.txt
│   └── src
│   ├── App.css
│   ├── App.js
│   ├── App.test.js
│   ├── assets
│   ├── components
│   ├── hooks
│   ├── index.css
│   └── index.js
├── requirements.txt
├── spotify
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-39.pyc
│   │   ├── admin.cpython-39.pyc
│   │   ├── apps.cpython-39.pyc
│   │   ├── cluster.cpython-39.pyc
│   │   ├── credentials.cpython-39.pyc
│   │   ├── models.cpython-39.pyc
│   │   ├── urls.cpython-39.pyc
│   │   ├── util.cpython-39.pyc
│   │   └── views.cpython-39.pyc
│   ├── admin.py
│   ├── apps.py
│   ├── cluster.py
│   ├── credentials.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   ├── models.py
│   ├── templates
│   ├── tests.py
│   ├── urls.py
│   ├── util.py
│   └── views.py
├── spotifycluster
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-39.pyc
│   │   ├── settings.cpython-39.pyc
│   │   ├── urls.cpython-39.pyc
│   │   └── wsgi.cpython-39.pyc
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── tutorialSources.txt
When I deploy with git push heroku main it seems to be fine but when I open the app in browser using the complementary url, I get the following errors on screen (debug mode is on):
TemplateDoesNotExist at /
build/index.html
Request Method: GET
Request URL: https://nameless-taiga-02413.herokuapp.com/
Django Version: 3.1.7
Exception Type: TemplateDoesNotExist
Exception Value:
build/index.html
Exception Location: /app/.heroku/python/lib/python3.9/site-packages/django/template/loader.py, line 19, in get_template
Python Executable: /app/.heroku/python/bin/python
Python Version: 3.9.4
Python Path:
['/app/.heroku/python/bin',
'/app',
'/app/.heroku/python/lib/python39.zip',
'/app/.heroku/python/lib/python3.9',
'/app/.heroku/python/lib/python3.9/lib-dynload',
'/app/.heroku/python/lib/python3.9/site-packages']
Server time: Fri, 30 Apr 2021 10:08:26 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: /app/react-frontend/build/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python3.9/site-packages/django/contrib/admin/templates/build/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/templates/build/index.html (Source does not exist)
I have the following settings in my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Also I have whitenoise in my middleware list
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
...]
The templates section in settings.py looks as follows
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'react-frontend')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
I expected that Heroku would be able to find the static folder based on the STATIC_DIRS variable but that doesn't seem to be the case. Any clue what's going on here?
So actually it was a really stupid mistake. react-frontend was a broken submodule and I needed to fix that by making sure .git/config didn't have submodules and removing react-frontend from the cache (No submodule mapping found in .gitmodules for path and missing .gitmodules file). Commit and pushed again and it worked...
if your DEBUG is set to False Django din't handle STATIC FILES. Heroku provide some configuration to serve STATIC FILES
STEP-1 : install whitenoise
$ pip install whitenoise
STEP-2 : check in settings.py whitenoise middleware by default it available in MIDDLEWARE if it's not that add it.
MIDDLEWARE = (
'whitenoise.middleware.WhiteNoiseMiddleware',
...)
STEP-3: add this in your settings.py
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Why cannot i access the about page when i can access the home page in the same directory?

I can access the home.html (http://127.0.0.1:8000/) without any issues but about page (http://127.0.0.1:8000/about/) gives me the below error:
Using the URLconf defined in django_project.urls, Django tried these URL patterns, in this order:
admin/
[name='blog-home']
about [name='blog-about']
The current path, about/, didn't match any of these.
django project folder urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
]
app folder urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='blog-home'),
path('about', views.about, name='blog-about'),
]
app folder views.py:
from django.shortcuts import render
def home(request):
return render(request, 'blog/home.html')
def about(request):
return render(request, 'blog/about.html')
directory structure:
.
├── blog
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │   └── __init__.cpython-37.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-37.pyc
│   │   ├── apps.cpython-37.pyc
│   │   ├── __init__.cpython-37.pyc
│   │   ├── models.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── views.cpython-37.pyc
│   ├── static
│   │   └── blog
│   │   ├── css
│   │   │   ├── main.css
│   │   │   └── test.css
│   │   ├── images
│   │   │   ├── favicon.png
│   │   │   └── freedom.jpg
│   │   └── js
│   ├── templates
│   │   └── blog
│   │   ├── about.html
│   │   ├── home.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── db.sqlite3
├── django_project
│   ├── asgi.py
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── settings.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── wsgi.cpython-37.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py
12 directories, 36 files
u have to add a trailing slash to ur path urls in urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='blog-home'),
path('about/', views.about, name='blog-about'),
]

Python3 Django issues regarding paths and custom decorators

Rather new to Django first time using it. Python is not my strong point in a web context - I'm having issues with my custom decorator I made that will decode the jwt from all requests not really sure what it's looking for.
The error message can be found at the very bottom. Below are what my files look like I'm simply trying to use the verify_token() as a decorator to include my custom token checker when receiving an HTTP request ( please don't recommend simple-jwt or django session token ) ultimately not what we want to do as we're working with legacy tables from other DBS and don't want any abstraction)
decorators.py
from django.core.exceptions import PermissionDenied
import jwt
def verify_token(function):
def wrap(request, *args, **kwargs):
if request:
print('========='.format(request))
jwt.decode(request, 'secret', algorithms=['HS256'])
else:
raise PermissionDenied
wrap.__doc__ = function.__doc__
wrap.__name__ = function.__name__
return wrap
views.py
from .User_Predictions.PredictionService import PredicitonController
from django.http import JsonResponse
from rest_framework.views import APIView
from .decorators import verify_token
class UserPredictions(APIView):
#verify_token
def generate_full_report(request):
_predction = PredicitonController()
results = _predction.compile_complete_predition()
return JsonResponse(results, safe=False)
urls.py
from django.urls import path
from .views import UserPredictions
urlpatterns = [
path('compilePredictions/', UserPredictions.generate_full_report, name='generate_full_report')
]
.
├── Analytics
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── settings.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── wsgi.cpython-37.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── authentication
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── biometrics
│   ├── admin.py
│   ├── apps.py
│   ├── bodyfatPrediciton.py
│   ├── Config.py
│   ├── __init__.py
│   ├── models.py
│   ├── templates
│   │   ├── bodyfat.html
│   │   ├── github.html
│   │   └── upload_bodyfat.html
│   ├── tests.py
│   ├── TorsoDimensions.py
│   ├── urls.py
│   └── views.py
├── db.sqlite3
├── dump.rdb
├── extraction
│   ├── admin.py
│   ├── apps.py
│   ├── ExtractionQueryService.py
│   ├── ExtractionServices.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │   └── __init__.cpython-37.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-37.pyc
│   │   ├── ExtractionQueryService.cpython-37.pyc
│   │   ├── ExtractionServices.cpython-37.pyc
│   │   ├── __init__.cpython-37.pyc
│   │   ├── models.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── views.cpython-37.pyc
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── loginServices
│   ├── admin.py
│   ├── apps.py
│   ├── Authentication.py
│   ├── decorators.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │   └── __init__.cpython-37.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-37.pyc
│   │   ├── Authentication.cpython-37.pyc
│   │   ├── __init__.cpython-37.pyc
│   │   ├── models.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── views.cpython-37.pyc
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
├── models.py
├── nutrition
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_auto_20191007_0323.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   │   ├── 0001_initial.cpython-37.pyc
│   │   ├── 0002_auto_20191007_0323.cpython-37.pyc
│   │   └── __init__.cpython-37.pyc
│   ├── models.py
│   ├── PredictFood.py
│   ├── __pycache__
│   │   ├── admin.cpython-37.pyc
│   │   ├── __init__.cpython-37.pyc
│   │   ├── models.cpython-37.pyc
│   │   ├── PredictFood.cpython-37.pyc
│   │   ├── serializers.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── views.cpython-37.pyc
│   ├── serializers.py
│   ├── TempImages
│   ├── templates
│   │   └── food.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── output
**├── predictions**
│   ├── admin.py
│   ├── apps.py
*│   ├── decorators.py*
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │   └── __init__.cpython-37.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-37.pyc
│   │   ├── decorators.cpython-37.pyc
│   │   ├── __init__.cpython-37.pyc
│   │   ├── models.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── views.cpython-37.pyc
│   ├── tests.py
*│   ├── urls.py*
│   ├── User_Predictions
*│   └── views.py*
├── README.txt
└── TempImages
ERROR MESSAGES
/Development/Backends_HealthApp/mainBackend/ModelBackend/Analytics/predictions/urls.py", line 6, in <module>
path('compilePredictions/', UserPredictions.generate_full_report, name='generate_full_report')
File "/home/travjav/.local/lib/python3.7/site-packages/django/urls/conf.py", line 73, in _path
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
Not really sure what's going on here - I can use other non-custom decorators with no issues
it was the way the decorator was created.
I changed the function name, but you can see the difference.
from django.core.exceptions import PermissionDenied
import jwt
from functools import wraps
from django.http import HttpResponseRedirect, HttpResponse
def protected_endpoint(function):
#wraps(function)
def wrap(request, *args, **kwargs):
auth = request.headers.get('Authorization').split()[1]
if auth is None:
return PermissionDenied
elif auth is not None:
try:
session_token = jwt.decode(auth, '', 'utf-8')
except jwt.DecodeError:
return HttpResponse('Invalid Token')
if session_token:
return function(request, *args, **kwargs)
else:
return HttpResponseRedirect('/')
return wrap

Why is my store.html not recognized?

I am Learning Django Udemy course(James Carrigan),I only updated to 2.0.5
tree bookstore
bookstore
├── bookstore
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   ├── settings.cpython-36.pyc
│   │   ├── urls.cpython-36.pyc
│   │   └── wsgi.cpython-36.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
├── manage.py
└── store
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│   ├── __init__.py
│   └── __pycache__
│   └── __init__.cpython-36.pyc
├── models.py
├── __pycache__
│   ├── admin.cpython-36.pyc
│   ├── __init__.cpython-36.pyc
│   ├── models.cpython-36.pyc
│   └── views.cpython-36.pyc
├── templates
│   ├── store.html
│   └── template.html
├── tests.py
└── views.py
These are my views
def index(request):
return render(request,'template.html')
def store(request):
return render(request,'store.html')
Urls
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from store import views
urlpatterns = [
url(r'^', views.index ,name='index' ),
url(r'^store', views.store ,name='store' ),
path('admin/', admin.site.urls),
]
I have changed line in HTML Boilerplate file
<p>Welcome to mystery books and store template is changed!</p>
I have problem when I go runserver
It does not see any difference between
http://127.0.0.1:8000/store and
http://127.0.0.1:8000/index
Why is my other html not recognized?
You don't end your regex pattern, so the first one matches everything.
Make sure you end your regex expression with $
url(r'^$', views.index ,name='index' ),
url(r'^store$', views.store ,name='store' ),