I
I
ISVir2014-11-01 20:52:58
Django
ISVir, 2014-11-01 20:52:58

How to direct a specific URL to a specific directory?

How can I make /js/ request Django to look in one static directory, and /img/ to another.
There is a twitter bootstrap template using angular, it uses its own URLs to get content. How can I teach Django to return data on request?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
yttrium, 2014-11-01
@yttrium

in production, it is better to configure it on nginx.
It's strange that the frontend is generally difficult to parameterize
implemented

A
Anatoly Scherbakov, 2014-11-02
@Altaisoft

Django has a special class-based view for serving static files at design time. Here is an example for Django 1.6.

from django.conf.urls import patterns, include, url
from django.conf import settings

urlpatterns = patterns('',
    # Здесь ваши URL
)

# А это работает только с Django development server, чтобы не было соблазна
# использовать в production
if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^js/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': 'папка-где-лежат-файлы-js',
        }),
        url(r'^css/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': 'папка-где-лежат-файлы-css',
        }),
   )

As already noted, this cannot be used in production, it will work very, very slowly. The exact way you configure it depends on how you generally deploy Django. If this is done under nginx, it will be something like this:
Location /css/ {
    alias /var/www/css/;
}

Google for alias and root directives .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question