Statics Files Django on Heroku Deployment - django

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/

Related

Having issue in loading files from static folder

I am new to django and I am having issues while following a tutorial on YouTube. Please help me so my static page which I changed into dynamic will load with changes that I made. Everything is working fine except when I try to run it by using {% static %}. I also told the system about static stuff through {% load static %} but it not showing pictures which I try to show through the static folder.
Summary
Pictures are not showing on the web page.
Files are not available from the static folder.
Not sure exactly what your problem is here but, as I understand it, you are trying to use images from your static folder in a django template. This can be achieved by correctly configuring django and using template tags.
In Development
To make this work with the development server, you need to create a folder called static inside each app's folder. For example, your project's file structure could look like this:
project
|_ project
|_ app
|_ static
|_ app
|_ image.png
|_ templates
|_ app
|_ home.html
|_ __init__.py
|_ admin.py
|_ models.py
|_ tests.py
|_ urls.py
|_ views.py
|_ db.sqlite3
|_ manage.py
Then, you simply access your image from the template home.html by adding {% load static %} at the beginning of the file and using {% static "app/image.png" %} wherever you want to use your image. Note that this template tag basically gives you the url to the static file you want to show. So, actually, you should insert the image like this:
<img src="{% static 'app/image.png' %}" />
In Production
In production, we need to collect all the static files from the different apps in one common place, so that the server can easily find them. We do this with the useful django app staticfiles, which we can configure in the settings.py file.
settings.py
Make sure your settings.py includes the following lines
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
INSTALLED_APPS = [
...
'django.contrib.staticfiles',
...
]
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
You can replace 'static/' with whatever you want to be your static url. You can replace 'staticfiles' in STATIC_ROOT with the name of the root folder where you will collect your static files. This folder must be (with this configuration) in your project's base directory - the same folder where you have your database (db.sqlite3). For example:
project
|_ project
|_ app
|_ staticfiles
|_ db.sqlite3
|_ manage.py
When you run python3 manage.py collectstatic, all the static files of each app of your project will be copied into the STATIC_ROOT directory, so they are all available from the same place. Your file structure will look like:
project
|_ project
|_ app
|_ staticfiles
|_ app
|_ image.png
|_ db.sqlite3
|_ manage.py
Finally, you will have to tell your production server where the staticfiles directory is, and you are all set to go (how to do this depends on the hosting service you are using).
Hope this helps!

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 folder structure

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.

Django directory structure?

I would like to implement a simple queueing service specific to a project. Where should the code go into in the Django directory structure?
Currently the structure is:
sound/
__init__.py
models.py
tests.py
views.py
static
[edit] I am asking where to place the queue service code I created within the direcotry structure above. Should I create a new directory?
Common structures
In Django 1.4+
project_root/
project_name/
media/
static/
some_app/css/app.css # overriding an app css file from project level
css/
project.css
static_root/ # in production using the collectstatic command
templates/some_app/foo.html # overriding some_app at project level
/admin/some_app/some_model/change_list.html
# overriding admin changelist for some_app.models.some_model
settings/
__init__.py
base.py # settings common to all instances of the project
dev.py
staging.py
test.py
prod.py
urls.py
some_app/
static/
css/
app.css
templates/some_app/foo.html
urls.py
views.py
models.py
manage.py
In Django 1.3 and prior
project_root/
some_app/
templates/some_app/foo.html
static/
css/
app.css
urls.py
views.py
models.py
media/
static/
some_app/
css/
app.css # overriding an app css file from project level
css/
project.css
static_root/ (in production)
templates/some_app/foo.html # overriding some_app at project level
/admin/some_app/some_model/change_list.html
# overriding admin changelist for some_app.models.some_model
settings/
__init__.py
base.py # settings common to all instances of the project
dev.py
staging.py
test.py
prod.py
urls.py
manage.py
Alternative approach
project_root/
.gitignore
README.md
docs/
venv/
src/
main/
media/
static/
some_app/css/app.css # overriding an app css file from project level
css/
project.css
static_root/ # in production using the collectstatic command
templates/some_app/foo.html # overriding some_app at project level
/admin/some_app/some_model/change_list.html
# overriding admin changelist for some_app.models.some_model
settings/
__init__.py
base.py
dev.py
staging.py
test.py
prod.py
urls.py
some_app/
static/
css/
app.css
templates/some_app/foo.html
urls.py
views.py
models.py
manage.py
wsgi.py
If you need to use the database, you should add the data models to models.py. For your program I recommend writing it in new python files (e.g. queuing.py) that you would import when and where you want to use it.
You could create another django app just for this as well.

No module named urls.py

I've been following the django tutorial over at https://docs.djangoproject.com/en/dev/intro/tutorial03/ but I've hit a brick wall.
yes I have searched google and looked up the 15 or so exact questions and nothing is working.
My directory structure is:
djangotest/
urls.py
settings.py
My settings has this
ROOT_URLCONF = 'urls.py'
The generator generated urls.py, not djangotest.urls as detailed in the tutorial. However, I don't think the naming matters too much. I've tried every variation of this urls file and I am having errors spat at me every time. The main one being the above.
urls.py
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
url(r'^reviews/$', 'djangotest.review.urls')
)
I'd love some help with this like how I can debug?
Are you being careful about the projectname and appname? Are you following the right tutorial for the version of django you downloaded? I'd also recommend using the latest stable release http://www.djangoproject.com/download/1.3.1/tarball/ rather than the latest dev release.
I'd recommend starting over; because without posting all your files (or steps you did), we aren't going to be able to debug where you went wrong.
Also, it seems like just running:
django-admin.py startproject my_test_project
cd my_test_project
django-admin.py startapp my_test_app
that you should have a directory structure like:
my_test_project/
|-- __init__.py
|-- manage.py
|-- my_test_app
| |-- __init__.py
| |-- models.py
| |-- tests.py
| `-- views.py
|-- settings.py
`-- urls.py
settings.py should have ROOT_URLCONF = 'my_test_project.urls', which reads the data in urls.py with no errors if you try
./manage runserver
and point your web browser to http://127.0.0.1:8000
On your update, it seems your specific mistake seems to be that your urls.py references 'djangotest.review.urls' Is there an app (in the installed apps) named review in the project djangotest with a urls.py? Are all the permissions correct?
Well, you need to have a file named "__init__.py" in each directory where you have .py files. These indicate to python that the directory is a python package. Also, verify the ownership and permissions on the files/folder to make sure you can access them as the user running the server.