Django Development (v)--displays pictures, loads static files such as Css,js __js

Source: Internet
Author: User

In the website development, must use the picture, the CSS file, the JS file and so on static file, with the Java EE in by the relative path lookup pattern dissimilarity, in Django, the static file loading, with the template similar, You need to set up in the settings.py file to tell the Django project where the static files are stored, which is the method used during the development process, where the static files are handled by the Ngnix server.

The steps given in the Django document

At this point, I will outline the main steps to facilitate beginners ' reference.

Settings in the 1.settings.py file

(1) Adding django.contrib.staticfiles to the Installed_apps

Installed_apps = (
    ' django.contrib.admin ',
    ' Django.contrib.auth ', '
    django.contrib.contenttypes ',
    ' django.contrib.sessions ',
    ' django.contrib.messages ', '
    django.contrib.staticfiles ', #安装此app
    ' Blog ',
)

(2) Set up Static_url and Staticfiles_dirs, which are the folders that represent static files in the root directory. Here, the folder I named is static

Static_url = '/static/'

staticfiles_dirs = (
    os.path.join (Base_dir, "static")
2. Now put the static file in the above steps set in the specified path is OK

3. In the HTML file, omit the root directory, you can access to the corresponding static file.


This approach is only available for development use, which is inefficient and unsafe for actual deployment, and is actually deployed to refer to the actual deployment of static files

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.