Django --- media static file configuration & global variables, django --- media static
Media static file configuration
Static files are mostly used to store data used to render front-end pages, and media is used to store client uploads or other files.
Add path to setting. py
MEDIA_ROOT = (OS. path. join (BASE_DIR, 'blog01/media') MEDIA_URL = '/media/' # Alias, which can be written at will
Add Route Allocation to urls. py:
from django.conf.urls import url,includefrom django.contrib import adminfrom django.views.static import servefrom Blog import settingsurlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
In this way, all files uploaded to django can be called at the front end:
{Filename. url} if the file filename is uploaded to Django, you can directly find the file using the URL attribute.
Global variables:
- Save global variables to setting. py, as shown in figure
FUNCTION= [
(1,'aaaa'),
(2,'bbbb'),
(3,'cccc'),
]
- Write the call function in views. py as follows:
def func(request): return {'func':FUNCTION}
- Any template of Django can use the template language to get func
{Func }}