我是django的新手。我现在正在学习几周。我有一个.txt文件,其中包含我想要呈现的HTML内容。据我所知,我没有联系到css& js文件正常。你能帮忙吗?
项目结构:
mysite/
manage.py
static/
css/
home.css
images/
logo.png
templates/
indexTemplate.txt
indexTemplate.txt
<html>
<head>
<title>Home Page</title>
<link rel="stylesheet" href="{% static "css/home.css" %}"/>
<script src="{% static "js/jquery-1.10.2.js" %}"></script>
</head>
<body>
<div class="Header">
<div class="HeaderLogo">
<a href="index.html"><img src="{% static "images/logo.png" %}" alt="Logo"/></a>
</div>
Nav1 | Nav 2
</div>
</body>
</html>
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
SECRET_KEY = 'u^37+&l60(xhoq8qyzlsyvynynpx%i-amg@@n#_x)tugfc5)61'
DEBUG = True
TEMPLATE_DEBUG = True
TEMPLATE_DIRS = (
'/home/ubuntu/Desktop/mysite/templates',
)
ALLOWED_HOSTS = []
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
WSGI_APPLICATION = 'mysite.wsgi.application'
DATABASES = {
'default': {
'ENGINE':'django.db.backends.mysql',
'NAME': 'myapp',
'USER': 'root',
'PASSWORD': '***',
'HOST': '*********',
'PORT': '8000',
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'`
views.py:
from django.http import HttpResponse
from django.template import Template, Context
from django.template.loader import get_template
from django.shortcuts import render_to_response
def hello(request):
t = get_template('indexTemplate.txt')
html = t.render(Context({'mnu':'Trending'}))
return HttpResponse(html)
答案 0 :(得分:1)
你需要
{% load static %}
在您的模板中,靠近顶部的某个位置,在{% static ...
标记之前。
在你的settings.py中添加
STATICFILES_DIRS = (
'/home/ubuntu/Desktop/mysite/static',
)
确保确实是正确的路径。