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