Django folder structure - django

What would be a good structure (best practise) for the folders in a Django application (1.7)?
I'm not sure where to put the static data and file Uploads.
In all my projects it turns out different, but currently I have something like this (I left out a few obvious folders/files):
project/
bin/
include/
...
src/
manage.py
main/
- settings.py
- urls.py
signup/
static/
static_/
+ css
+ img
+ js
static/
templates/
- index.html
- base.html
- ...
uploads/
And also, I'd prefer to see url's like for example site.com/css/file.css instead of site.com/static/css/file.css , but somehow thats more complicated then it seems. How can that be accomplished?

I use the the following in setting.py (using Django v1.6.8 at the moment)
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
#TEMPLATE_DIRS = (os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "static", "templates"),)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),)
This gives me a folder layout
project/
manage.py
project_app/
- settings.py
- urls.py
someother_app/
- admin.py
- models.py
- views.py
static/
css/
javascript/
templates
admin/
someother_app/
- base.html
- index.html
media/
I'm not sure what you mean when you say site.com/css/file.css. Something like <link rel="stylesheet" href="{{ STATIC_URL }}css/jquery.asmselect.css"> in the <head> of base.html uses the Django Framework to present your .css files. Why not use what is there? Saves time and effort.
Tommy.

Here is a recommendation for the Django project layout from the book Two Scoops of Django:
repo_root/
.gitignore
Makefile
docs/
README.rst
requirements.txt
django_project_root/
manage.py
media/
django_app_root/
static/
templates/
config/
__init__.py
settings/
urls.py
wsgi.py
and is considered a three level project layout where:
Top Level (repo_root): high-level files required for deployment
Second Level (django_project_root): actual django project
Third Level (config): the django project configuration files.
Regarding file Uploads I would upload them from the browser directly to Amazons S3 file storage (or similar service). Otherwise you're hogging bandwidth and CPU time. Or if you must then in the media folder ^ and for security reasons please validate the file types uploaded.

Related

How to let PIL's Image.open find files from static?

I want to access a file in static from a module called "generate.py" inside my app. However I don't really have any idea how to do it.
I believe I've properly "installed" the app because i can access the .html file in the App
Things I've tried in generate.py
1 -
from django.conf.urls.static import static
Image.Open(static('resources/App/template/photothatiwanttoopen.jpg'))
Error I get from code above: 'list' object has no attribute 'read'
2 -
from django.conf import settings
Image.Open(settings.STATIC_URL+'resources/App/template/photothatiwanttoopen.jpg')
Error I get from code above: [Errno 2] No such file or directory: '/static/resources/App/template/photothatiwanttoopen.jpg'
Here's my folder structure view
- Project
- App
- templates
- App
- app.html
- apps.py
- generate.py <<< Script
- models.py
- urls.py
- views.py
- Project
- settings.py and stuffs
- static
- resources
- App
- template
- photothatiwanttoopen.jpg
- manage.py and stuffs
Here's last few lines of my settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Let me know if you need any more information.
STATIC_URL has no concept of the actual location on the filesystem.
That's where STATIC_ROOT comes into play.
Your optimal choice is likely going to be:
Image.Open(os.path.join(STATIC_ROOT, 'resources/App/template/photothatiwanttoopen.jpg'))
That is assuming of course you've already collected the staticfiles (manage.py collectstatic) and you have imported STATIC_ROOT from django settings.

Static does not loads static files in Django

This is my directory structure
app_web
_init_.py
settings.py
urls.py
wsgi.py
upload_app
migrations/
static/
js/
alert.js
templates/
upload.html
_init_.py
admin.py
apps.py
models.py
tests.py
views.py
db.sqlite3
manage.py
In settings.py , my
STATIC_URL = '/static/'
and in my upload.html
{% load staticfiles %}
<script type="text/javascript" src="{% static "js/alert.js" %}"></script>
It does not works and throws 404 error everytime. I even tried load static but it still cannot load anything from static folder and throws 404 error.
I am using Windows 10 machine and Django==1.9
The idea was to create a static directory outside the upload_app folder. The reason is that in the settings.py the BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) . That means one directory above is the base directory, so it will start searching static/ from that directory. But it will never find it as I placed inside upload_app/static .
Also, I need to work on putting templates outside the app upload_app as it's not generally best practice to keep templates inside the app.
looking for other suggestions in structure

Django template namespace

I read that for every app of my INSTALLED_APPS in settings.py, Django will look for HTML templates inside the templates subdirectory. But I'm a little confused.
For instance, when creating my HTML templates in the initial project below:
myproject/
manage.py
myapp/
__init__.py
settings.py
urls.py
wsgi.py
templates/
Should I simply create a template subdirectory within the myapp directory and put all my HTML templates inside?
Almost correct, the templates subdirectory within myapp also needs myapp subdirectory which will contain all of your app html files. Also do not mix core project files such as settings.py and wsgi.py within app, better separate them out, hence the better structure would be:
myproject/
manage.py
mysite/
__init__.py
settings.py
wsgi.py
myapp/
__init__.py
urls.py
views.py
templates/myapp/

Where should i put django application folder

I have followed several django tutorials on the web.
There is 2 folders: One for django project and another for application. We can have multiple applications for the same project but i my case, i have just one.
Sometimes, the application folder is located at the same level than project folder and sometimes, the application folder is located inside project folder.
I do not know what is the best solution... It works in both case. It is just a state of the art question
Thanks
Mostly it is a matter of choice and the organization of your Project. Even thought, i will post you here a recomented layout and good reasons to choose this
myproject/
manage.py
myproject/
__init__.py
urls.py
wsgi.py
settings/
__init__.py
base.py
dev.py
prod.py
blog/
__init__.py
models.py
managers.py
views.py
urls.py
templates/
blog/
base.html
list.html
detail.html
static/
…
tests/
__init__.py
test_models.py
test_managers.py
test_views.py
users/
__init__.py
models.py
views.py
urls.py
templates/
users/
base.html
list.html
detail.html
static/
…
tests/
__init__.py
test_models.py
test_views.py
static/
css/
…
js/
…
templates/
base.html
index.html
requirements/
base.txt
dev.txt
test.txt
prod.txt
Allows you to pick up, repackage, and reuse individual Django applications for use in other projects
Environment specific settings. This allows to easily see which settings are shared and what is overridden on a per environment basis.
Environment specific PIP requirements
Environment specific PIP requirements
Small more specific test files which are easier to read and understand.

Statics Files Django on Heroku Deployment

I have my app runnning in Heroku, everything works really good with my models and forms, but there is a problem, I can't see any of my styles neither for my templates not for Django Grappelli, how can I solve this problem?
Thank you.
Check the path that your images/styles are trying to reference. Ensure that your STATIC_URL is a relative path. Also ensure that your STATIC_ROOT and STATIC_URL are not the same.
Ex:
settings/base.py
from unipath import Path
# Project directory root assuming: yourapp.settings.base
PROJECT_DIR = Path(__file__).ancestor(3)
# Static files
STATIC_ROOT = PROJECT_DIR.child("static")
# URL prefix for static files.
STATIC_URL = '/static/'
This layout follows the directory structure similar to:
project_name/
|-- app 1
|-- models.py
|-- views.py
...
|-- project_name
|-- settings
|-- base.py
|-- local.py
|-- dev.py
...
Also by default Heroku should collectstatic when you upload your project however if you have modified this setting ensure to call:
python manage.py collectstatic
you can then check to see if static files are present in the specified folder (in the example above it would be in /static/