Issue with django staticfiles amazon S3 - python

i just having this issue, and I wonder if you can help me, I'm using django 1.7, my project runs great on local, I make a deploy in EC2 of amazon, with nginx, gunicorn, etc, the project runs well in the instance of EC2, but when I config the settings in the production.py, the styles, images upload to the bucket in S3, but doenst load in the project, in the main page.
Im using pillow for the administration of the images and static..
I just dont know why it doesnt work, because the static files are in the bucket. Please help me, here I put the codes of my base.py, production.py.
I used python manage.py collectfiles and , it collects, but it doesnt load the styles when I open the project in the web.
BASE.PY
from unipath import Path
BASE_DIR = Path(__file__).ancestor(3)
SECRET_KEY = '<my secret key>'
DJANGO_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
LOCAL_APPS = (
'apps.eventos',
'apps.users',
)
THIRD_PARTY_APPS = (
'social.apps.django_app.default',
)
INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS + THIRD_PARTY_APPS
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'ueventos.urls'
WSGI_APPLICATION = 'ueventos.wsgi.application'
LANGUAGE_CODE = 'es'
TIME_ZONE = 'UTC'
DATE_FORMAT = "Y-m-d"
USE_I18N = True
USE_L10N = False
USE_TZ = True
AUTH_USER_MODEL = 'users.User'
AUTHENTICATION_BACKENDS = (
'social.backends.facebook.FacebookAppOAuth2',
'social.backends.facebook.FacebookOAuth2',
'social.backends.google.GoogleOpenId',
'social.backends.google.GoogleOAuth2',
'social.backends.google.GoogleOAuth',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
SOCIAL_AUTH_USER_MODEL = 'users.User'
SOCIAL_AUTH_FACEBOOK_KEY = '776670565804594'
SOCIAL_AUTH_FACEBOOK_SECRET = 'fcc2a240ef71ff88c15fd64d685bf4eb'
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '435717486239-b9dpg2pfian4h8dj1vc95jna8tfdeced.apps.googleusercontent.com'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'oE9ZzK_p9zuNxH91gRLS8nH1'
PRODUCTION.PY
from .base import *
DEBUG = False
TEMPLATE_DEBUG = False
ALLOWED_HOSTS = ['*']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '<mydb>',
'USER': '<mydbuser>',
'PASSWORD' : 'mydbuser',
'HOST': 'localhost',
'PORT': '5432'
}
}
INSTALLED_APPS = INSTALLED_APPS + (
'storages',
)
AWS_STORAGE_BUCKET_NAME = 'ueventos'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorages'
AWS_ACCESS_KEY_ID = 'mykey'
AWS_SECRET_ACCESS_KEY = 'mysecretkey'
STATIC_URL = 'https://s3.amazonaws.com/ueventos/'
MEDIA_URL = 'https://s3.amazonaws.com/ueventos/'
MEDIA_ROOT = BASE_DIR.child('media')
I already install boto and django storages.. eveything works fine on the server, the issue is with the static in s3.
in my manage.py and wsgi.py I specify to use de production.py file.
I have a folder named "media", where all the files the people upload, will save there. But that folder doenst apper in s3. I tried uploading some image in my site, but doesnt work anyways.
I'm using 2 apps, one for events and another for user.. both of them storage images..
Thank you so much.

Try something like this:
from storages.backends.s3boto import S3BotoStorage
StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static')
MediaRootS3BotoStorage = lambda: S3BotoStorage(location='media')
DEFAULT_FILE_STORAGE = 'config.settings.production.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'config.settings.production.StaticRootS3BotoStorage'
MEDIA_URL = 'https://s3.amazonaws.com/%s/media/' % AWS_STORAGE_BUCKET_NAME
STATIC_URL = 'https://s3.amazonaws.com/%s/static/' % AWS_STORAGE_BUCKET_NAME
Adjust path accordingly.
Also see:How to set-up a Django project with django-storages and Amazon S3, but with different folders for static files and media files?

Related

How do I server a specific directory with possible sub directories in Django 1.7?

I've got this Django web app that allows users to upload various video files. The directories are all under media, and any subdirectories are created to uniquely idenfitfy the file. I already am serving static files, but all I want to do is serve the entire 'media' directory. Is there any simple way that I can server this?
"""
Django settings for elearn project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
BASE_DIR + '/templates/',
)
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
#DEBUG = False
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'lessons',
'registration'
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'app.urls'
WSGI_APPLICATION = 'app.wsgi.application'
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/elearn/static',
)
STATIC_URL = '/static/'
I purposely left out sensitive credentials.
Also this is how I'm serving the static files currently
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Thanks
It depends on what you want to do with it. For something simple like dating the file, you can set upload_to to %Y/%m/%d. You can also write a function to return a relative URL as a string, like such:
def widget_file_name(instance, filename):
return '/'.join(['widget', instance.user.username, filename])
class Widget(models.Model):
name = models.CharField(max_length=200)
user = models.ForeignKey(User)
file = models.FileField(upload_to=widget_file_name)
UPDATE
If you're just looking for a way to serve that media directory, you could always just specify it as your MEDIA_ROOT in settings.py:
MEDIA_ROOT = '/path/to/media'
Now, keep in mind, in production, you should let your web server (nginx, apache, etc) serve static and media files, so you'll need to set that up in whatever you use as a proxy pass web server. To serve the files in this directory, you will also need to set a MEDIA_URL. MEDIA_URL = 'media' will usually suffice

django nonrel ImportError when running a script

I've configurated a virtual environment for my project, all is working except that when I try to run a script to populate the MongoDB data base. I get a ImportError exception.
I am using django mongodb engine Link and djangotoolbox. I want to run a script to populate my mongo db, however when I run it the following exceptions raised:
This is my project skeleton:
This is my models.py file:
from django.db import models
from djangotoolbox.fields import EmbeddedModelField, ListField
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
pages = ListField(EmbeddedModelField("Page"))
def __unicode__(self):
return self.name
class Page(models.Model):
title = models.CharField(max_length=128)
url = models.URLField()
views = models.IntegerField(default=0)
def __unicode__(self):
return self.titleenter code here
This is my settings.py file:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from pip.exceptions import BadCommand
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'mw7+iswll+f=a!xg9r+*%v=uyv$r_6lqcf)eb27s!4h_!*&9gt'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rango',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'tango_with_django_project.urls'
WSGI_APPLICATION = 'tango_with_django_project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'tango_with_dj',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
STATIC_PATH = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = (
STATIC_PATH,
)
MEDIA_URL = '/media/'
MEDIA_ROOT
This is my populate script populate.py:
import os
def populate():
python_cat = add_cat('Python')
django_cat = add_cat("Django")
frame_cat = add_cat("Other Frameworks")
# Print out what we have added to the user.
for c in Category.objects.all():
print "Category: "+c.name+" was added!"
def add_cat(name):
c = Category.objects.get_or_create(name=name)[0]
return c
# Start execution here!
if __name__ == '__main__':
print "Starting Rango population script..."
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tango_with_django_project.settings')
from rango.models import Category
populate()
I don't know why djangotoolbox cannot be reached at the populate main scope if it is imported at models.py scope. What is the problem here?

Django static files load locally but not on development server

I was able to get my static files (CSS) to load in my local development environment, however when I push the changes to my development server I am unable to load the CSS. My local environment is Mac OS 10.9.2 and my development server is running Ubuntu 12.04.4 x64.
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/'),
)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '#7h&rny3^hz&q6w-8$6k&+msh554$pz*tx#$lj(+dgctvuj2j%'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'allauth.account.context_processors.account',
'allauth.socialaccount.context_processors.socialaccount',
'django.contrib.auth.context_processors.auth',
)
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pcatapp',
'south',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'tastypie',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
#'django.core.context_processors.csrf',
)
ROOT_URLCONF = 'pcat.urls'
WSGI_APPLICATION = 'pcat.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db',
'USER': 'root',
'PASSWORD': 'password',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
SITE_ID = 1
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
An excerpt from the page source:
<link rel="stylesheet" href="/static/myapp/style.css">
My static folder is located at myapp/static/myapp/style.css. All help is appreciated, thanks.
I had the same problem and dj-static provided a simple solution
$ pip install dj-static
In your wsgi.py file:
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())
Good Luck
When using djangos development server, I have found that this problem, and its solution normally lies in the URLS file (when DEBUG=True), something like this will allow you to use django to serve your static files:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns(
'',
... url includes etc.
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
This will mean that any urls serving from {{ STATIC_URL }} will be handled by django, here are the django docs on this https://docs.djangoproject.com/en/dev/howto/static-files/
EDIT (Added solution for running under a webserver not django development server):
If you are running your project behind a webserver, and not using djangos runserver, then its probably best you dont use djangos static file finder, and instead use its static file collector and the webserver to serve static files, to do this, try this in your settings.py:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
then run ./manage.py collectstatic and ensure your webserver redirects requests from /static/ to the full path of your static files directory, e.g in NGINX you might do..
location /static/ {
alias /var/www/myproject/static/;
}
And apache:
Alias /static/ /var/www/myproject/static/
Hope this helps...

Django AUTHENTICATION_BACKENDS import error

What is the proper way to import a custom backend in settings.py? I currently have the following in settings.py:
AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth')
where apployment_site is the app, auth is file name, and CustomAuth is the class name.
In my view, I get: ImportError: a doesn't look like a module path after I run the following code:
from django.contrib.auth import authenticate
from apployment_site import *
authenticate(username="username", password="password")
Here's my full settings.py:
"""Django settings for apployment project.
For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '3(a=jr=tfedkqzv3f=495%0$ygxjt332(=n0&h=e2bzh(i#r*j'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apployment_site'
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth')
ROOT_URLCONF = 'apployment.urls'
WSGI_APPLICATION = 'apployment.wsgi.application'
# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/dev/howto/static-files/
STATIC_URL = '/static/'
make sure it's a tuple:
AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth',)
note the comma at the end
I don't think you need to actually import it into your view. According to the documentation, Django will go through all your authentication backends when you call authenticate().
Thus, just make sure you have all the authentication backends you want in your settings.py.

Static files in django 1.5 - working locally, not working on Heroku

I have a project in Django 1.5 and hosts it on Heroku.
The problem is that I do not see the server static files (CSS, JS) (on the user and administrator) - went through the tutorial available at: https://devcenter.heroku.com/articles/django-assets
but nothing helps.
When I start the application locally using python manage.py runserver - everything works. Below is the code from the file setting.py
import dj_database_url
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'blog_project.urls'
WSGI_APPLICATION = 'blog_project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {'default': dj_database_url.config()}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
I think you need to change your setting for Heroku so that you include
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, 'static'),
)
Its using a different lookup to
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

Categories