Django:: Serve static files in different paths for each "app" - python

I would like to serve static (assets) files in the following structure (which coincidentally is the same as the local path:
https app/[appname]/static/*
local_path app/[appname]/static/*
currently, my urls.py from app folder is
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'', 'app.views.load'),
)
How am I going to map the static requests to each application static folder?
I know this is usually not a task for python. In production, this would probably be routed by nginx before reaching python.. But for now, I need this implemented inside django.

Related

What is the right way to setup urls.py for django-rest-framework + angular-route + static files?

I understand the concept of what I want to do (there is a rather nice question here: Django, REST and Angular Routes)
Django has a route for DRF, Admin, and needs at least one more, for the angular page.
But I'm not sure on the implementation details, particularly in where to put the frontend/static files, what to put in urls.py, and how to configure it for dev+production.
My url patterns currently look like this:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
Ideally, in production, I want serve the HTML without touching django - straight from nginx or similar (probably using AWS EB, but that is outside the scope of this question).
For Dev - I don't mind, as long as it works!
Skipping djangos templating system for angular would be great, rather than altering angulars operators. I don't intend to use django templates (except in djangos admin, and DRFs browsable views)
For development, I use something like this, at then end of the main urls.py file, to serve static files from various directories:
if settings.DEBUG:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
# this connects STATIC_URL to STATIC_ROOT
urlpatterns += staticfiles_urlpatterns()
# connect other paths to directories
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
urlpatterns += static('/app/',
document_root=join(settings.BASE_DIR, '../ng_app/'))
For production, you set the webserver to handle the requests. First the known routes like /admin/, /api/, /api-auth/ and files in /static/.
And then a catch-all route / that would always serve your index.html with the Angular app for any route. So that HTML5 routes in Angular will always load the app first, and the app then uses its own routes to build the correct page from API data.
The actual app files could be anywhere, even on a different server.

(Django) How to apply css?

I want to apply CSS on my Django project.
CSS path in my index.html is right but it doesn't work well.
My index.html page show up well in my site,
--app_index
----index.html
----bootstrap
-------css
-----------abc.css
-----------abc2.css
So, must I modify the URL.py file too?
You need to configure static files in your Django app. The following links should help guide you in the right direction:
http://www.tangowithdjango.com/book/chapters/templates_static.html
https://docs.djangoproject.com/en/1.7/howto/static-files/
You will set up your index.html for example in a template directory that can access your static files (e.g. CSS, javascript, static images, etc.) that are located in your static directory.
In response to your question concerning modification of urls.py, the answer is yes. As you will see in the links on configuring static files within your project, you have to add some code to your urls.py. An example of this code might look like this:
if settings.DEBUG:
import debug_toolbar
urlpatterns += patterns(
'django.views.static',
(r'media/(?P<path>.*)',
'serve',
{'document_root': settings.MEDIA_ROOT}),
url(r'^__debug__/', include(debug_toolbar.urls))
)
During development Django serves your static files hence the above code, but during production your static files are served via your configured server (e.g. Apache, nginx).

Is it possible to turn on directory indexes when serving static files in development in Django?

I'm currently using the staticfiles_urlpatterns to serve static files in development, as recommended, but I'm getting 404's on the static files for a particular app I'm using and not sure why... turning on directory indexes for all the static files would be cool if its possible. I noticed the 'show_indexes' option for serving some types of static files. Is it possible to write my debug-only url conf in a way that enables this across the board for my static files in development?
Something like this should work (update to use STATIC_ROOT):
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
'show_indexes': True,
}),
)

Django Static Setup

I've looked through countless answers and questions trying to find a single definitive guide or way to do this, but it seems that everyone has a different way. Can someone please just explain to me how to serve static files in templates?
Assuming I've just created a brand new project with Django 1.4, what all do I need to do to be able to render images? Where should I put the media and static folders?
Put your static files into <app>/static or add an absolute path to STATICFILES_DIRS
Configure your web server (you should not serve static files with Django) to serve files in STATIC_ROOT
Point STATIC_URL to the base URL the web server serves
Run ./manage.py collectstatic
Be sure to use RequestContext in your render calls and {{ STATIC_URL }} to prefix paths
Coffee and pat yourself on the back
A little bit more about running a web server in front of Django. Django is practically an application server. It has not been designed to be any good in serving static files. That is why it actively refuses to do that when DEBUG=False. Also, the Django development server should not be used for production. This means that there should be something in front of Django at all times. It may be a WSGI server such as gunicorn or a 'real' web server such as nginx or Apache.
If you are running a reverse proxy (such as nginx or Apache) you can bind /static to a path in the filesystem and the rest of the traffic to pass through to Django. That means your STATIC_URL can be a relative path. Otherwise you will need to use an absolute URL.
The created project should have a static folder. Put all resources (images, ...) in there.
Then, in your HTML template, you can reference STATIC_ROOT and add the resource path (relative to the static folder)
Here is the official documentation:
How to manage static files: https://docs.djangoproject.com/en/1.4/howto/static-files/
Static Files in general: https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/
If you are planning to to deploy in a production type setting then you should read the section here: https://docs.djangoproject.com/en/1.4/howto/static-files/#staticfiles-production
Generally you put our static and media folders outside of the django project app
Define your STATICFILES_DIRS, STATIC, and MEDIA path in the settings.
Create staticfiles folder beside your templates folder, urls.py, settings.py, etc...
You don't have to create media folder because it will automatically created when you upload images.
In your urlconf put this one:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
admin.autodiscover()
urlpatterns = patterns('',
.............
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
In templates:
{% load static %}
<img src="{% static 'images.png' %}">
<img src="{{MEDIA_URL}}images.png">

Appfog Django admin css missing

I'm using http://appfog.com I have DEBUG = True and ADMIN_MEDIA_PREFIX = '/static/admin/'
The css for the admin is missing.
Django only serves static files if DEBUG=True and it is running the development server.
Documentation:
This view is automatically enabled and will serve your static files at STATIC_URL when you use the built-in runserver management command
[...]
To enable this view if you are using some other server for local development, you'll add a couple of lines to your URLconf. The first line goes at the top of the file, and the last line at the bottom.
Example:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns()
Hope this helps
Using dbieber's Django template for appfog this is the approach that worked for me:
Create a directory for static files in your project directory, then set up a URL to have the appfog server serve these files:
cd <project>
mkdir static
In urls.py, make sure the urlpattern is set to include static files (this is already there if you use the template):
urlpatterns += patterns('',
...
url(r'^static/(?P<path>.*)$',
'django.views.static.serve',
{'document_root': settings.STATIC_ROOT),
)
Collect all static files into the static directory
python manage.py collectstatic
Deploy to appfog
af update <appname>

Categories