I am using django 1.3 and trying to deploy a django project (client sent) on my dev machine (ubuntu 12.04). The problem is regarding the static files. My directory structure is as follows:
project_name
media
static
css
img
js
settings.py
Here is my settings.py:
ROOT = '/home/user/project_name'
MEDIA_ROOT = '%s/media/' % ROOT
MEDIA_URL = '/media/'
STATIC_ROOT = '%s/static/' % ROOT
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
My site is perfectly deployed but the css, js and imgs are missing. Same is the case for the admin interface. When I use the link http://mysite.com/static/js/some.js it gives a 404.
Help would be appreciated and up-voting an answer is custom.
I think you need to run ./manage.py collectstatic :)
You don't mention configuring your web server to actually serve the static files. You need to point it at the directory that collectstatic put them into.
did you run python manage.py collectstatic ? see here
be careful that in production you should place the static file in a static server. There should be something in the guidelines.
And a little offtopic..
It will be better, to use:
MEDIA_ROOT = os.path.join(os.path.dirname(file),'media').replace('\','/')
STATIC_ROOT = os.path.join(os.path.dirname(file),'static').replace('\','/')
and in main urls.py at development, django webserver only:
urlpatterns = patterns('',
(r'^media/(?P.*)','django.views.static.serve',{'document_root': os.path.join(os.path.dirname(file),'media').replace('\','/') }),
(r'^static/(?P.*)','django.views.static.serve',{'document_root': os.path.join(os.path.dirname(file),'static').replace('\','/')}),
In this way, u dont need collectstatic, ull need it at production server, where u will use nginx or something other to server your static
Related
I know that this isn't the first time the question has been asked, so I'm apologizing up front. I've been working on this for a few days and still have no clue how to proceed.
I followed this tutorial to the tee: https://aws.amazon.com/getting-started/hands-on/deploy-python-application/
The website is up and running, but of course the static files won't load.
Here's where I'm at. In settings.py, I've set my STATIC_ROOT and STATIC_URL to the following:
STATIC_ROOT = os.path.join(BASE_DIR, 'mysite', 'static')
STATIC_URL = '/static/'
I ran collectstatic and gathered all my static files into the mysite app directory. It looks like this:
-mysite
- mysite (app)
- static
- base
- base.css
- settings.py
- urls.py
- wsgi.py
Unfortunately, the static files still fail to load on the website.
Here are my questions:
Suppose I wanted to view the base.css text file on the web. Would I go to www.mysite.com/static/base/base.css? If no, what would the URL be? If yes, why is it not appearing given the current set up?
Per the AWS tutorial, I ran edited the httpd-app.conf file to include the following
Alias /mysite/static /opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/contrib/admin/static
What was the purpose of the edit? How does it impact how the static files are served on the site?
Any help you guys can offer on loading these static files will be a lifesaver.
Your urls.py file needs to be configured to serve your STATIC_URL
urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
More on serving static files with Django
I'm struggling getting staticfiles (CSS) to work on my Django site deployed to Openshift. I can workaround this by putting style info in the HTML templates, but the Django admin site stylesheets still will not load. Everything works locally, including with debug off.
Openshift stores staticfilesunder repo/ -> wsgi/ -> static/, where the main project directory is also under wsgi/.
Here are the relevant parts of settings.py:
ON_OPENSHIFT = True if 'OPENSHIFT_REPO_DIR' in os.environ else False
PROJECT_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir)
if ON_OPENSHIFT:
STATIC_ROOT = os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi', 'static')
else:
STATIC_ROOT = ''
STATIC_URL = '/static/'
if ON_OPENSHIFT:
STATICFILES_DIRS = (os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi', 'static'))
else:
STATICFILES_DIRS = [os.path.join(PROJECT_DIR, 'static')]
I have a script that runs the following when deploying:
cd $OPENSHIFT_REPO_DIR/wsgi/squadron
python manage.py collectstatic --noinput
I'm using Python 3.3 and Django 1.6, and referencing the following guides: 1 and 2 from the docs.
What am I doing wrong? I don't get an error message, just no stylesheets, either my own, or on Django admin. When I use SCP, I can verify my stylesheet is in the correct Openshift directory.
This is the best source to read when you are trying to get OpenShift working with Django.
http://appsembler.com/blog/django-deployment-using-openshift/
Some of this will be changing (for the better) with upcoming releases but this should help for now.
I'm trying to use django-cumulus for serving files off Rackspace CloudFiles. I'm currently only trying it on my local dev server, using Django 1.4.2.
I can use cumulus's syncstatic management command to upload all my static assets successfully, but I can't seem to display them on my site with the same settings.
If my relevant settings are:
STATIC_URL = '/static/'
CUMULUS = {
'USERNAME': 'myusername',
'API_KEY': 'myapikey',
'CONTAINER': 'mycontainername',
'STATIC_CONTAINER': 'mycontainername',
}
DEFAULT_FILE_STORAGE = 'cumulus.storage.CloudFilesStorage'
STATICFILES_STORAGE = 'cumulus.storage.CloudFilesStaticStorage'
then when I run syncstatic all my apps' static files are uploaded into /mycontainername/static/, as I'd expect. But when I load a page in admin it ignores STATIC_URL and tries to serve assets from URLs like http://uniquekey....r82.cf2.rackcdn.com/path/to/file.css rather than http://uniquekey....r82.cf2.rackcdn.com/static/path/to/file.css.
Also, I can't see how to have my public (non-admin) pages use the static files on CloudFiles, rather than serving them from a local /static/ directory.
Have I missed some crucial setting, or am I doing something else wrong?
I had the same problem. What i did was to
git clone https://github.com/richleland/django-cumulus.git
edit context_processors.py
from django.conf import settings
from cumulus.storage import CloudFilesStorage
def cdn_url(request):
"""
A context processor to expose the full cdn url in templates.
"""
cloudfiles_storage = CloudFilesStorage()
static_url = '/'
container_url = cloudfiles_storage._get_container_url()
cdn_url = container_url + static_url
print {'CDN_URL': cdn_url}
return {'CDN_URL': cdn_url}
Once you are done, install it with sudo python setup.py install
Do note that context_processors.py from django cumulus is actually quite slow
I'm 2hours stuck in a issue about STATIC_URL and STATIC_ROOT when I try to make run the webapp on my server at webfactional.
when I load the webpage all the requests works well, except by the fact that any link with {{ STATIC_URL}} is working or loading.
So a common error that appears on firebug is:
GET http://mydomain/static/extras/h5bp/js/libs/modernizr-2.5.3.min.js 500 (Internal Server Error)
My setup is:
urls.py
I did nothing, and there's nothing about static files.
settings.py
DEBUG = False
STATIC_ROOT = '/home/mydomain/webapps/static_app/'
STATIC_URL = 'http://mydomain/static/'
STATICFILES_DIRS = ()
views.py
view example
#csrf_exempt
def IndexView(request):
try:
request.user.is_authenticated()
except AttributeError:
return render_to_response('index.html',
{'request': request,},
context_instance=RequestContext(request))
return render_to_response('index.html',
{'request': request, 'profile' : request.user},
context_instance=RequestContext(request))
index.html
a part of code not found
<script src="{{ STATIC_URL }}extras/h5bp/js/libs/modernizr-2.5.3.min.js"></script>
well, I follow all the points of:
https://docs.djangoproject.com/en/1.4/howto/static-files/
and this another one:
http://docs.webfaction.com/software/django/getting-started.html
I'm using the correct installed apps, middlewares, template_contexts.
If I'm missing something please help me to figure out.
Thanks in advance!
--edit
I have to say, if I just change the DEBUG = True will works fine.
because on urls.py I have this piece of code:
if settings.DEBUG:
# static files (images, css, javascript, etc.)
urlpatterns += patterns('',
(r'^media/(?P<path>.*)/$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
2 things must happen on a production environent that is not needed in the development environment.
You must run manage.py collectstatic -- this collects all static files into your STATIC_ROOT directory.
You must serve your STATIC_ROOT directory at the STATIC_URL url. How exactly depends on your production setup. This is not even django related; all that matters is that STATIC_ROOT contents are available at STATIC_URL.
Let's say you're using Apache, you'd alias a URL to a directory.
Alias /static/ /path/to/my/static_root/
If you're using nginx, it would be something like
location = /static/ {
alias /path/to/my/static_root/;
}
I just realized you're using webfaction, in which case you set up a static application which literally just serves files at the targeted directories at the URL you define. I'm trying to remember my webfaction login to see the exact procedure, but it shouldn't be difficult to figure out.
Update:
Logged into webfaction; you can create an application that is a symlink. Easy!
Create a symbolic link app that serves /YOUR_STATIC_URL/ and point it to /YOUR_STATIC_ROOT/. Done!
How can I view my static css files? I've set my STATIC_ROOT, and am using python manage.py runserver.
In my development environment, according the docs, I only need to place my static files (in this case, /static/css/typography.css) in my STATIC_ROOT, and python manage.py runserver will automatic create the views necessary to access it if I have DEBUG = True.
STATIC_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), "static")
I've also tried manually adding the views in URLConf, which won't display the css file either:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns()
In my template, the {{ STATIC_URL }} gets to the correct address (/static/css/typography.css), but it will not serve the file when I try to access it:
<link href="{{ STATIC_URL }}css/typography.css" rel="stylesheet" type="text/css">
Notes: The other Django related static files questions on StackOverflow are over two years old. Django version 1.3b1 differentiates STATIC (static files, such as css and images) and MEDIA (user-uploaded file).
Besides. all the tries mentioned above, you must also make sure that your template is receiving the RequestContext when called from the view.
http://lincolnloop.com/blog/2008/may/10/getting-requestcontext-your-templates/
This link gives various ways of doing the same and you could choose any one. :) Also, the TEMPLATE_CONTEXT_PROCESSORS must be added to settings.py for this to take effect.
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"myapp.processor.foos",
)
Note: STATIC_ROOT is the place where all your static files are stored by Django after collecting them from STATICFILES_DIRS.
Runserver will pick them up from the path mentioned in STATIC_ROOT, so STATIC_URL should point to the same location as STATIC_ROOT.
Answered here: Django staticfiles app help
I was putting my files in STATIC_ROOT, so adding this works:
STATICFILES_DIRS = (STATIC_ROOT,)
In development:
#settings.py have this by default
STATIC_URL = '/static/'
This means when you want to use static files, django will look for them in folder 'static' in your app directory. So you need to create folder 'static' in every your app and put inside your static files.
In production:
1. Make place to hold static files from all app's
STATIC_ROOT = "/var/www/example.com/static/"
2. Run python manage.py collectstatic to collect static files from all app's in STATIC_ROOT folder.
3. In settings.py change DEBUG=False and ALLOWED_HOSTS = ['yourhostadress'].
4. Point your webserver to STATIC_ROOT folder (eg. for Apache2.4):
Alias /static/ /path/to/mysite.com/static/
<Directory /path/to/mysite.com/static>
Require all granted
</Directory>
After this setup, your django project should be able to run (using mod_wsgi) and use static files on Apache web server.