django-정적 파일 경로 및 템플릿 경로 구성
3490 단어 django
예를 들어 나의 프로젝트는 xiaoshuo---소설에 들어가는 관리자이다.py xiaoshuo가 입장 중:
아래에 static와templates 폴더를 만듭니다
settings를 엽니다.py :
import os
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join( os.path.dirname(__file__),'static').replace('\\','/'),
) 뒤에 경로를 추가합니다,django1.4 static에서 정적 파일을 자동으로 찾을 것입니다. urls를 설정할 필요가 없습니다.활용단어참조
예를 들면 다음과 같습니다.
http://localhost:8000/static/css/home.css
2: templates 경로를 구성하려면:
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join( os.path.dirname(__file__),'tempates').replace('\\','/'),
) 하면 돼...
대응하는 템플릿의 응용 참고http://djangobook.py3k.cn/2.0/chapter04/
from django.shortcuts import render_to_response
def detail(request):
return render_to_response('detail.html') views를 만듭니다.py 파일은 html 페이지를 브라우저로 직접 되돌려줍니다
urls에 있습니다.py에 추가:
('^detail/$', detail),
브라우저 입력: http://localhost:8000/detail/
common 베이스.html 내용
<link rel="stylesheet" href="css/style.css" type="text/css">
<link rel="stylesheet" href="css/reset.css" type="text/css">
<link rel="stylesheet" href="css/home.css" type="text/css">
<script type="text/javascript" src="js/jquery-1.7.1.js"></script>
<script type="text/javascript" src="js/jquery.wookmark.js"></script> 상급 디렉터리 아래detail.html 내용:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
{% include "common/base.html" %}
</head> jsp에서 처리하는 inlcude와 비슷합니다. 상대 경로 django는 상대적으로 접근하는 url 경로입니다.
................
위의 베이스.html은 이렇게 하면 css와 js에 접근할 수 있습니다.
<link rel="stylesheet" href="../static/css/style.css" type="text/css">
<link rel="stylesheet" href="../static/reset.css" type="text/css">
<link rel="stylesheet" href="../static/css/home.css" type="text/css">
<script type="text/javascript" src="../static/js/jquery-1.7.1.js"></script>
<script type="text/javascript" src="../static/js/jquery.wookmark.js"></script>