Django --- media static file configuration & amp; global variable, django --- media static

Source: Internet
Author: User

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 }}

 

  



 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.