Detailed Explanation of Django Static File Configuration Process

  • 2021-07-24 11:30:46
  • OfStack

Static file configuration

Overview:

Static files are handled by the Web server, and Django itself does not handle static files. The simple processing logic is as follows (take nginx as an example):

URI Request-- > According to the configuration rules in Web server, take nginx as an example. The main requirement is to configure location in nginx. conf

-- > If it is a static file, it is directly processed by nginx

-- > If not, it is handled by Django, and Django matches according to the rules in urls. py

The above is the processing mode after deployment to Web server. In order to facilitate development, Django provides a processing mechanism for static files in the development environment, as follows:

static configuration:

STATIC mainly refers to files such as css, js and images:


STATIC_URL = '/static/' #  Alias 

STATICFILES_DIRS = [
  os.path.join(BASE_DIR, "static"), #  The actual name, that is, the name of the actual folder 
]
#  Note: django The reference name and the real name are mapped. When referencing, you can only find them according to the reference name, not the real name 

media configuration:


MEDIA_URL = "/media/"

#  User uploaded files for models.py In FileField , ImageField Field) to where to save 
MEDIA_ROOT = os.path.join(BASE_DIR, "app Name ", "media")

#  Cooperate settings.py In MEDIA_URL = "/media/" Configuration, that is, the permissions of this interface are opened to the public 
from django.conf.urls import url
from django.views.static import serve
from . import settings

urlpatterns = [
  url(r'^media/(?P<path>.*)$', serve, {"document_root": settings.MEDIA_ROOT}),
]

ps:

The processing of static files includes STATIC and MEDIA, which are often confused. It is defined as follows in Django:

MEDIA: refers to the files uploaded by users, such as the files uploaded by FileFIeld and ImageField in Model. If you define

MEDIA_ROOT = c:\ temp\ media, then File=models. FileField (upload_to= "abc/") #, the uploaded file is saved to


c:\temp\media\abc
eg : 
class blog(models.Model):
Photo = models.ImageField(upload_to="photo")

The uploaded picture is uploaded to c:\ temp\ media\ photo, and to display this file in the template, set MEDIA_ROOT in settings (must be the absolute path of the local path), which is generally written as follows:

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

MEDIA_URL refers to the address prefix when accessed from a browser, for example:


    MEDIA_ROOT = c:\temp\media\photo
    MEDIA_URL = "/data/"

In the development phase, the processing of media is handled by django:

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

Write this in the template < img src="/media/abc/a.png" >

The biggest difference during deployment is that you have to have the web server handle the media files, so you have to configure it in the web server,

So that the web server can access the media file.

Take nginx as an example, which can be found in nginx. conf as follows:


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

For details, please refer to the information on how to deploy django in nginx.


Related articles: