How to Separate Media, Static Files and Web Pages in django

Keywords: Python Django

In the django project, static files, media files and html code account for a large part, so how can we separate them so that we and the server can manage and use them easily?

static file
Static, as the name implies, is a static file, and django comes with a command to extract all the static files in the project
python3 manage.py collectstatic

I'm used to putting these files that I can import from outside in the root directory of my project

DemoProject
--DemoProject
--DemoApp
--static
--manage.py

Yes, that's it.
Before that, however, we need to set the location STATIC_ROOT where the extracted files will be stored in the project's setting.py

STATIC_ROOT = os.path.join(BASE_DIR, "static")
That way, when we run the above command, we will see one more in our project structurestatic file
Then configureDemoProjectLowerurl,Add to

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.conf import settings


urlpatterns = [
    url(r'^admin/', admin.site.urls),
]


if settings.DEBUG or True:
    urlpatterns += staticfiles_urlpatterns()

This if judgment, of course, supports such access when debug is true, so if the masking judgment needs to be modified in the real production environment
But it's not over yet, so we need to add it in setting.py for easy reference

STATICFILES_DIRS = [
    ('bootstrap',os.path.join(BASE_DIR, 'static/bootstrap').replace('\\','/')),
]

Note that this can be accessed directly in html as follows

<link rel="stylesheet" href="{% static "bootstrap/css/bootstrap.min.css" %}">

Note to add {% load staticfiles%} to the first line

mediafile
There are always large files on the website, such as music videos or pictures, so we'll also use them from django Separate and invoke in the project
First we're at and static Create a new one in the same directory media Folder, in media Create a new one in image As our folder for storing pictures
The structure is as follows:

DemoProject
--DemoProject
--DemoApp
--static
--media
---image
--manage.py

Step 1: Add at the end of setting.py

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

Part 2: In urls.py
At urlpatterns += staticfiles_urlpatterns()
Add below

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

You might not believe it, so ok ay
<img src="/media/image/test.png" alt="">
This gives you access to pictures or videos from our media/image

templates
If we want to separate the web code files in our project
Step 1:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates').replace('\\','/'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Add your web page code path to the IRS of the code above, and if you want to put it under the root path as well as me, you can do the same

os.path.join(BASE_DIR, 'templates').replace('\\','/')

If you have other questions or are up to date with the new django tutorial, please follow my own public number

Posted by xylex on Mon, 11 Nov 2019 00:28:40 -0800