django startproject using project template gives template TemplateSyntaxError - django

I am working on a project to customize the startproject command to create django projects from project_template, see below:
bootstrap/
├── __init__.py
├── conf
│   ├── __init__.py
│   └── project_template
│   ├── manage.py
│   ├── project_name
│   │   ├── __init__.py
│   │   ├── apps
│   │   │   ├── __init__.py
│   │   │   └── example
│   │   │   ├── __init__.py
│   │   │   ├── admin.py
│   │   │   ├── models.py
│   │   │   ├── tests.py
│   │   │   ├── urls.py
│   │   │   └── views.py
│   │   ├── contrib
│   │   │   └── __init__.py
│   │   ├── settings
│   │   │   ├── __init__.py
│   │   │   ├── base.py
│   │   │   ├── development.py
│   │   │   └── production.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   ├── requirements
│   │   ├── base.txt
│   │   ├── development.txt
│   │   └── production.txt
│   ├── requirements.txt
│   ├── static
│   │   ├── js
│   │   │   └── base.js
│   │   ├── scss
│   │   │   └── base.scss
│   │   └── vendor
│   │   └── README
│   └── templates
│   ├── 404.html
│   ├── 500.html
│   ├── base.html
│   └── example
│   ├── base.html
│   └── index.html
└── management
├── __init__.py
└── commands
├── __init__.py
└── startproject.py
this is the startproject.py
import os
import grabone as go
from django.core.management.commands.startproject import Command as StartprojectCommand
EXTENSIONS = ['py', 'txt', 'html', 'scss', 'css', 'js', 'bowerrc', 'rst']
class Command(StartprojectCommand):
def handle(self, project_name=None, target=None, *args, **options):
options['extensions'] += EXTENSIONS
return StartprojectCommand.handle(self, project_name=project_name, target=target, *args, **options)
def handle_template(self, template, subdir):
if template is None:
return os.path.join(go.__path__[0], 'conf', subdir)
return StartprojectCommand.handle_template(self, template, subdir)
When I run django-admin startproject test1 it creates the project folder etc etc, but it will try to parse the html templates and give me an error:
django.template.base.TemplateSyntaxError: 'staticfiles' is not a valid tag library: Template library staticfiles not found, tried django.templatetags.staticfiles
This is what the template looks like:
{% load staticfiles %}
<html>
...
</html>
This is what the INSTALLED_APPS in setting look like:
DJANGO_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
)
THIRD_PARTY_APPS = (
'django_extensions',
'pipeline',
# Add third party apps here
)
LOCAL_APPS = (
# Add local apps here
)
# See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
Some how the content in the settings is not being executed though...

removed html from
EXTENSIONS = ['py', 'txt', 'html', 'scss', 'css', 'js', 'bowerrc', 'rst']
Worked.

Related

Heroku and Django: Couldn't find that process type (web)

I know this has been asked a lot of times but none of the solutions seem to work. I am trying to run a Django app in Heroku but am running into issues when I try to scale dynos.
➜ main-website git:(master) heroku ps:scale web=1
Scaling dynos... !
▸ Couldn't find that process type (web).
The issue seems to be related to ProcFile. This is what I have configured in my root directory (same as requirements.txt etc).
web: gunicorn main-website.wsgi:application --log-file -
What am I missing or doing wrong so I can correct this?
Project Structure
➜ main-website git:(master) tree -L 3
.
├── app
│   ├── about
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   ├── models.py
│   │   ├── __pycache__
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── app
│   │   ├── asgi.py
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   ├── contact
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   ├── models.py
│   │   ├── __pycache__
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── core
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   ├── models.py
│   │   ├── __pycache__
│   │   ├── tests
│   │   ├── tests.py
│   │   └── views.py
│   ├── db.sqlite3
│   ├── home
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   ├── models.py
│   │   ├── __pycache__
│   │   ├── static
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── manage.py
│   ├── privacy
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   ├── models.py
│   │   ├── __pycache__
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── static
│   │   ├── app
│   │   ├── css
│   │   ├── images
│   │   └── scripts
│   └── templates
│   ├── about.html
│   ├── contact.html
│   ├── footer.html
│   ├── header.html
│   ├── home.html
│   ├── layout.html
│   ├── privacy.html
│   └── slider.html
├── docker-compose.yml
├── Dockerfile
├── LICENSE
├── ProcFile
└── requirements.txt
The file name is Procfile not ProcFile.
Then try this web: gunicorn main-website.wsgi:application --log-file -

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

List apps of a django project

I'm completely new for django and I'd want to list the apps of a django project, for example:
FeinCMS
I know that startapp creates the directory structure for an app. I wonder if there is either a function or a file to get the app list.
For example taking FeinCMS as an example, the repo contains:
feincms
├── AUTHORS
├── CHANGELOG.rst
├── CONTRIBUTING.rst
├── docs
│   ├── admin.rst
│   ├── advanced
│   ├── conf.py
│   ├── contenttypes.rst
│   ├── contributing.rst
│   ├── deprecation.rst
│   ├── extensions.rst
│   ├── faq.rst
│   ├── images
│   ├── index.rst
│   ├── installation.rst
│   ├── integration.rst
│   ├── Makefile
│   ├── medialibrary.rst
│   ├── migrations.rst
│   ├── page.rst
│   ├── releases
│   ├── settings.rst
│   ├── templatetags.rst
│   └── versioning.rst
├── feincms
│   ├── admin
│   ├── apps.py
│   ├── content
│   ├── contents.py
│   ├── context_processors.py
│   ├── contrib
│   ├── default_settings.py
│   ├── extensions
│   ├── __init__.py
│   ├── _internal.py
│   ├── locale
│   ├── management
│   ├── models.py
│   ├── module
│   ├── shortcuts.py
│   ├── signals.py
│   ├── static
│   ├── templates
│   ├── templatetags
│   ├── translations.py
│   ├── urls.py
│   ├── utils
│   └── views
├── LICENSE
├── MANIFEST.in
├── README.rst
├── setup.cfg
├── setup.py
└── tests
├── cov.sh
├── manage.py
├── requirements.txt
├── testapp
└── tox.ini
How can I use django to list the apps in the repo?
settings.INSTALLED_APPS is the list of apps. You can start a django shell (manage.py shell) to query the settings:
from django.conf import settings
print(settings.INSTALLED_APPS)
>>> ['user', 'django.contrib.auth', 'django.contrib.sites', ...]
Query django.apps ...
from django.apps import apps
for app in apps.get_app_configs():
print(app, app.name, app.label)
Results in ...
<ContentTypesConfig: contenttypes> django.contrib.contenttypes contenttypes
<AdminConfig: admin> django.contrib.admin admin

auto importing python classes in flask shell not working as expected

So I have a flask app structured in this manner
calvin % tree -L 3 .
.
├── README
├── alembic
│   ├── README
│   ├── env.py
│   ├── env.pyc
│   ├── script.py.mako
│   └── versions
├── alembic.ini
├── api.sublime-project
├── app
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── agencies
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── models.py
│   │   └── models.pyc
│   ├── agents
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── models.py
│   │   ├── models.pyc
│   │   ├── views.py
│   │   └── views.pyc
│   ├── api.py
│   ├── api.pyc
│   ├── auth
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── constants.py
│   │   ├── constants.pyc
│   │   ├── decorators.py
│   │   ├── decorators.pyc
│   │   ├── models.py
│   │   ├── models.pyc
│   │   ├── views.py
│   │   └── views.pyc
│   ├── districts
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── models.py
│   │   ├── models.pyc
│   │   ├── views.py
│   │   └── views.pyc
│   ├── helpers.py
│   ├── helpers.pyc
│   ├── middleware.py
│   ├── middleware.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── properties
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── models.py
│   │   └── models.pyc
│   ├── templates
│   │   └── index.html
│   ├── users
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── models.py
│   │   ├── models.pyc
│   │   ├── views.py
│   │   └── views.pyc
│   └── viewings
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── views.py
│   └── views.pyc
├── config.py
├── config.pyc
├── manage.py
├── requirements.txt
├── run.py
└── shell.py
I am setting up my shell so that it auto-imports all the classes located in the models.py files when I execute ./manage.py shell
And this is the script in manage.py that is intended to achieve that (reference flask-script docs)
def _make_context():
from app import models
return dict(app=app, db=db, models=models) # TODO: this is not working appropriately
manager.add_command("shell", Shell(make_context=_make_context))
In my app/models.py, I have import statements from every module, "agencies", "auth" etc etc.
However, when I enter my shell environment, I have to access my classes as models.Users instead of directly Users, which is not what I am expecting. How do I auto-import everything so that I can access the classes directly?
Below is a simplified solution that assumes you're using SQLAlchemy (db.Model). If not, you should only need to change the issubclass if-statement to match your appropriate check of what to import.
from app import db, create_app
import app.models as models
from flask.ext.script import Manager, Shell
app = create_app()
manager = Manager(app)
def make_shell_context():
return_dict = {}
# grab everything we can possibly import from the models module
module_importables = dir(models)
for importable in module_importables:
# if it isn't a class, it'll throw a TypeError exception that importable is not a class
try:
# we only want our SQLAlchemy models
if issubclass(getattr(models,importable),db.Model):
return_dict[importable] = getattr(models,importable)
except TypeError as inst:
pass
return_dict['app'] = app
return_dict['db'] = db
return return_dict
manager.add_command("shell", Shell(make_context=make_shell_context))
if __name__ == '__main__':
manager.run()
Instead of
from app import models
You can do:
from app.models import *
This will import all classes,variables from models. Note: It is usually not recommended to import * but in this case, it could work for you. Ideally, you should do something like:
from app.models import Users, ...and so on

django, compressor and foundation scss

Hi I am trying to integrate foundation scss into django using django-compressor's precompiler, the project looks like this:
├── manage.py
├── requirements.txt
├── static
│   ├── config.rb
│   ├── humans.txt
│   ├── index.html
│   ├── javascripts
│   │   ├── foundation
│   │   │   ├── foundation.alerts.js
│   │   │   ├── foundation.clearing.js
│   │   │   ├── foundation.cookie.js
│   │   │   ├── foundation.dropdown.js
│   │   │   ├── foundation.forms.js
│   │   │   ├── foundation.joyride.js
│   │   │   ├── foundation.js
│   │   │   ├── foundation.magellan.js
│   │   │   ├── foundation.orbit.js
│   │   │   ├── foundation.placeholder.js
│   │   │   ├── foundation.reveal.js
│   │   │   ├── foundation.section.js
│   │   │   ├── foundation.tooltips.js
│   │   │   └── foundation.topbar.js
│   │   └── vendor
│   │   ├── custom.modernizr.js
│   │   ├── jquery.js
│   │   └── zepto.js
│   ├── MIT-LICENSE.txt
│   ├── robots.txt
│   ├── sass
│   │   ├── app.scss
│   │   ├── normalize.scss
│   │   └── _settings.scss
│   └── stylesheets
│   ├── app.css
│   └── normalize.css
├── templates
│   ├── 404.html
│   ├── 500.html
│   ├── admin
│   │   └── base_site.html
│   └── base.html
└── weddings
├── __init__.py
├── __init__.pyc
├── local_settings.py
├── local_settings.pyc
├── settings.py
├── settings.pyc
├── urls.py
├── urls.pyc
└── wsgi.py
and the precompiler looks like this in settings.py
COMPRESS_PRECOMPILERS = (
('text/x-scss', 'sass --scss --compass {infile} {outfile}'),
)
And when I run it with nginx + uwsgi, I get the following error:
Syntax error: File to import not found or unreadable: foundation/foundation-global.
Load paths:
/etc/uwsgi/vassals
/etc/uwsgi/vassals/sass
/srv/www/weddings/gems/compass-0.12.2/frameworks/blueprint/stylesheets
/srv/www/weddings/gems/compass-0.12.2/frameworks/compass/stylesheets
Compass::SpriteImporter
/srv/www/weddings/gems/bourbon-3.1.1/app/assets/stylesheets
/srv/www/weddings/gems/bourbon-3.1.1/app/assets/stylesheets
on line 2 of /srv/www/weddings/weddings/static/sass/_settings.scss
from line 2 of /srv/www/weddings/weddings/static/sass/app.scss
Use --trace for backtrace.
I suspect it's not reading the config.rb or the settings in config.rb is wrong:
http_path = "/"
css_dir = "stylesheets"
sass_dir = "sass"
images_dir = "images"
javascripts_dir = "javascripts"
As you have a config.rb file, you have a Compass project.
Compass projects are supposed to be compiled with the compass command line tool, not sass command line tool.
As you have already discovered, compilation should be launched from insie the project folder. But it's a bad idea to hardcode the path into settings.py as it makes your project unportable.
Instead of a hardcoded path, you should use os.path.dirname(os.path.realpath(__file__)) to discover current script's path. To change the folder relative to settings.py, use os.path.join() like this (adjust as necessary, you can use ..):
os.path.join(os.path.dirname(os.path.realpath(__file__)), "static")
Also, you may already have PROJECT_DIR var in your settings.py. Use it to make this line cleaner.
A little improvement to #James Lin
COMPRESS_PRECOMPILERS = (
# ('text/x-scss', 'django_libsass.SassCompiler'),
# ('text/x-scss', 'sass --scss {infile} {outfile}'),
('text/x-scss', 'sass --scss --compass {infile} {outfile}'),
)
My current work around is to run the sass command in the folder where ocnfig.rb is located
COMPRESS_PRECOMPILERS = (
('text/x-scss', 'cd /srv/www/project/name/static && sass --scss --compass {infile} {outfile}'),
)