Configure Django static resource URLSTATIC_ROOT

Source: Internet
Author: User
This article mainly introduces the configuration method of Django static resource URLSTATIC_ROOT. This article provides the configuration method and two usage methods. For more information, see Reason

After configuring HTML pages, you need to use some static resources, slice, JS files, and CSS styles. However, using these resources in Django is not just a reference, you also need to configure the path STATIC_URL. if this configuration is not good, requests for these static resources will return HTTP 404.

Experience teaching

1. output the STATIC_URL in the settings. py file to the HTML page. check which physical paths point to and whether the physical paths usually run out of the root directory. Here is a DEMO:

The code is as follows:


Def home (request ):
T = get_template ("index.html ")
Html = t. render (Context ({
"Template_dir": settings. TEMPLATE_DIRS [0],
"Title": "Home ",
"Static_dir": settings. STATIC_ROOT }))
Return HttpResponse (html)

In this way, you can see these paths on the Accessed HTML page.

2. configure the STATIC_ROOT variable

The code is as follows:


STATIC_ROOT = OS. path. join (OS. path. dirname (_ file __),'.. ', 'Templates/content '). replace ('\\','/')

The second and third parameters may be adjusted to correct the path. (Multi-debugging)

3. configure the urlpatterns variable in the urls. py file (mainly for highlighting this line ):

The code is as follows:


Urlpatterns = patterns ('',
Url (r' ^ $ ', home ),
Url (r' ^ static /(? P . *) $ ', 'Django. views. static. serv', {'document _ root': settings. STATIC_ROOT }),
)

4. test the preceding configuration:

The code is as follows:




{Title }}- Oger

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.