Django-03- static configuration file

A, Django static configuration file principle

Static configuration file is to allow users to request django server to find Static file returns.

We must first understand a few concepts:

  • Media files: user-uploaded files
  • Static files: css, js, image, etc.
  • Development environment: using django built-in server static files
  • Production environment: using apache2 / nginx server static files map

Static files handed over to the Web server processes, Django itself is not static files. Simple process logic is as follows (in nginx example):

Request URI -----> arrangement rule according to which the first Web server, so as to nginx an example, the location of the main claim arranged nginx. #Conf Lane

  | ----------> If a static file, by directly processing nginx

  | ----------> If it is not referred to the treatment Django, Django match rules according to which urls.py

These are deployed to deal with the way the Web server, in order to facilitate the development, Django provides a mechanism for processing development environment for static files, this method is:

1, which was added in INSTALLED_APPS 'django.contrib.staticfiles',

2, which was added in urls.py 

 if settings.DEBUG:  
           urlpatterns += patterns('', url(r'^media/(?P<path>.*)$', 
           'django.views.static.serve', {'document_root': settings.MEDIA_ROOT }),   
            url(r'^static/(?P<path>.*)$',
          'django.views.static.serve',{'document_root':settings.STATIC_ROOT}), )  

3, so you can use the static file directly in the development phase.

Two, MEDIA_ROOT and MEDIA_URL

Handling static files and also includes STATIC MEDIA categories, which are often confusing, in Django which is defined as: 

MEDIA: refers users to upload files, such as the Model FileFIeld inside, ImageField uploaded files. If you define # MEDIA_ROOT = c: \ temp \ media, then File = models.FileField (upload_to = "abc /") to upload the file will be saved to c: \ temp \ media \ abc

 class blog(models.Model):  
         Title=models.charField(max_length=64)  
         Photo=models.ImageField(upload_to="photo") 
# Upload pictures to upload to c: \ temp \ media \ photo, and in the template to display the file, then write

MEDIA_ROOT in settings which set the local path must be an absolute path, usually is written like this:

BASE_DIR= os.path.abspath(os.path.dirname(__file__))  
MEDIA_ROOT=os.path.join(BASE_DIR,'media/').replace('\\','/') 

MEDIA_URL is the address prefix of the access from the browser, for example:

MEDIA_ROOT=c:\temp\media\photo  
MEDIA_URL="/data/"
        # During the development phase, processed by the media processing django:

        # Visit http: //localhost/data/abc/a.png is to visit the c: \ temp \ media \ photo \ abc \ a.png

        # Write <img src = "{{MEDIA_URL}} abc / a.png"> template which

In the deployment phase the biggest difference is that you have to let the web server to handle media files, so you must configure the web server in order to allow a web server to access media files # nginx for example, you can be in nginx.conf inside:

location ~/media/{
         root/temp/
         break;
}

Specific information on how to deploy can refer to in django nginx.

Three, STATIC_ROOT and STATIC_URL

STATIC mainly refers to as css, js, images of such files, which you can configure the settings STATIC_ROOT and STATIC_URL, configuration and MEDIA_ROOT is the same, but be careful:

STATIC files are generally stored in the following location:

1. STATIC_ROOT: in which settings provided, generally used to put some common js, css, images and the like.

2, app of static folder, where each app file folders may be to create a static folder, and then when you run collectstatic, Django will traverse INSTALL_APPS all app inside the static folder, copy all the files inside to STATIC_ROOT. So, if you want to build reusable app, then you want the static files app needed on the static folder. That is a project references a lot of app, then css, images and other static files needed for this project are scattered in various app of static documents, more typical is the admin application. When you want to publish, we need to collect the scattered static files to a place that is STATIC_ROOT.

3, STATIC files can also be configured STATICFILES_DIRS, specify additional static file storage location.

STATIC_URL MEDIA_URL similar meaning.

Note 1: In order to change your backend will not affect the introduction of front-end, front-end to avoid extensive modification

STATIC_URL = '/ static /' # reference name
STATICFILES_DIRS = (
     os.path.join (BASE_DIR, "statics") # real name, that is the actual name of the folder
)

#django a reference to the name and the actual name of the map, when referring to, only in accordance with a reference to the name, can not find the actual name
#<script src="/statics/jquery-3.1.1.js"></script>
# ------ error ----- not be directly used, must STATIC_URL = '/ static /':
#<script src="/static/jquery-3.1.1.js"></script>

Note 2 (statics folder written in a different app, call the static file):

 STATIC_URL = '/static/'
STATICFILES_DIRS=(
     ('hello',os.path.join(BASE_DIR,"app01","statics")) ,
)

#<script src="/static/hello/jquery-1.8.2.min.js"></script>

Note 3: Use the template language

STATIC_URL = '/static/'
{% load staticfiles %}
<script src={% static "jquery-1.8.2.min.js" %}></script>

 

Guess you like

Origin www.cnblogs.com/lsf123456/p/11306948.html