Django CSS style, picture path problem solution

Source: Internet
Author: User

When you do a project with Django, if you are debugging locally, we will turn on debug = True in settings.py
We will also do the following:

1. Set static_root = Os.path.join (Os.path.dirname (__file__), ' STATIC ')

2. Set static_url = '/static/'

I think the above two steps are generally mentioned, but even if you do such a configuration, when you browse in the browser of your CSS or JS file, still 404 error, indicating that the path error, can not find.

For example, my project is as follows:

You must be wrong if you enter Http://localhost:8000/static/js/myjs.js. After many tests, the following methods can be used to solve
Configure Staticfiles_dirs
For example, my project configuration is as follows:

Staticfiles_dirs = (
(' CSS ', os.path.join (static_root, ' CSS '). replace (' \ \ ', '/'),
(' JS ', Os.path.join (static_root, ' JS '). replace (' \ \ ', '/'),
(' Images ', Os.path.join (static_root, ' images '). replace (' \ \ ', '/'),
(' Upload ', Os.path.join (static_root, ' upload '). replace (' \ \ ', '/'),
)


Note that the css,js,images,upload in front must be consistent with your directory structure name.
Configure you to start Django again, then visit: Http://localhost:8000/static/js/myjs.js will see the results, instructions to find the path, the same css,image pictures can be found.

Use in an early template

<script type= "Text/javascript" src= "/static/js/myjs.js" ></script>


This is the way to go. CSS is the same as the picture.

Note that in case of appeal, it can only take effect when opening debug=true, it is very convenient when we develop. If set Debug=false will not take effect. In this case, the configuration of the production environment is used. Use the Web application server to handle these static pictures, CSS, and JS files.

Processing of production environment
1. Set Debug=false
2. Can not configure Staticfiles_dirs
3. Configure Web application server, such as I use Nginx:

Location ^~/static {
Root/opt/www/mynet;
}


In this way, you will naturally find the static path without having to deal with Django. If you want to configure your production environment, you can refer to another article I wrote: Configure Django on a production environment

Note: I see some articles configured in urls.py, but I don't think that's a good way to do it. Like what:

Media_root=os.path.join (Os.path.dirname (__file__), ' media '). replace (' \ \ ', '/') #如果是 *nix system, no replace function required
Media_url= '/media/' #末尾一定要加上/
Admin_media_prefix = '/media/admin/' #填啥都可以, just can't be like Media_url


And then

(R ' ^media/(?) p.*) $ ', ' Django.views.static.serve ', {' Document_root ': Settings. Media_root})

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.