Django 模板目录和模板文件夹帮助
因此,我正在拆分一个前端已经构建好的网站,并承担了构建后端的任务,但我对django是新手。我已经成功地将所有内容分开(css、js、静态文件夹中的img、模板文件夹中的html文件等),到目前为止,我已经能够将index.html设置为django的主页。在此index.html文件中,其他html文件(与index.html文件一起位于templates文件夹中)正在index.html文件的标记中加载。目前,所有的图像、css和js都在文件中,但html文件不是。我将这些html文件引用为“name of file.html”,但是,除非我将templates文件夹移动到静态文件夹中(与css、图像、js等一起),并将引用更改为“static/templates/name of file.html”,否则我似乎无法加载这些文件 我的项目设置如下。我还运行django 1.7Django 模板目录和模板文件夹帮助,django,templates,url,Django,Templates,Url,因此,我正在拆分一个前端已经构建好的网站,并承担了构建后端的任务,但我对django是新手。我已经成功地将所有内容分开(css、js、静态文件夹中的img、模板文件夹中的html文件等),到目前为止,我已经能够将index.html设置为django的主页。在此index.html文件中,其他html文件(与index.html文件一起位于templates文件夹中)正在index.html文件的标记中加载。目前,所有的图像、css和js都在文件中,但html文件不是。我将这些html文件引用为
atmos_v4/
atmos_v4/
init__.py
settings.py
urls.py
wsgi.py
db.sqlite3
manage.py
static/
css/
...
img/
...
js/
...
media/
...
templates/
index.html
...
下面是我的url.py和settings.py。我感觉我的模板目录和静态根目录可能设置不正确。如果没有,有人能告诉我我错过了什么吗?谢谢大家!
settings.py:
"""
Django settings for atmos_v4 project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'y=3ey3sv8lm1j358(2bgthtx0bzy_cjaxug@2npx029nfs@5i%'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
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.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'atmos_v4.urls'
WSGI_APPLICATION = 'atmos_v4.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
from os.path import join
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
STATIC_PATH = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = (
STATIC_PATH,
)
url.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.generic import TemplateView
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name="index.html")),
url(r'^admin/', include(admin.site.urls)),
)
index.html参考示例
<div id="wheelartist" style="position:absolute; top:131px; left:536px; z- index:999999;">
<div id="circleartist" class="circleartist">
<a id="homebuttonLink" href="artist_profile.html"><img id="homebutton" title="Home" src="/static/img/icons/user.png" alt=""></a>
<a id="msgsbuttonLink" href="messages.html"><img id="msgsButton" title="Messages" src="/static/img/icons/mail.png" alt=""></a>
<a id="directorybuttonLink" href="directory.html"><img id="directoryButton" title="Directory" src="/static/img/icons/book.png" alt=""> </a>
<a id="cartbuttonLink" href="shopping_cart.html"><img id="cartButton" class="shopingCart" title="Shopping Cart" src="/static/img/icons/shopping.png" alt=""></a>
<a id="contestsbuttonLink" href="contests_list.html"><img id="contestsButton" class="planet" title="Planet" src="/static/img/icons/planet.png" alt=""></a>
<a id="pointsbuttonLink" href="points.html"><img id="pointsButton" class="awards" title="Awards" src="/static/img/icons/awards.png" alt=""></a>
<a id="prefbuttonLink" href="preferences.html"><img id="prefButton" class="tools" title="Tools" src="/static/img/icons/tools.png" alt=""></a>
<a id="searchbuttonLink" href="search.html"><img id="searchButton" class="headphones" title="Search" src="/static/img/icons/music.png" alt=""></a>
<a id="mapbuttonLink" href="artist_careermap.html"><img id="mapButton" title="Career Map" src="/static/img/icons/map.png" alt=""></a>
<a id="profitsbuttonLink" href="artist_profits.html"><img id="profitsButton" title="Profits" src="/static/img/icons/profits.png" alt=""></a>
<a id="statsbuttonLink" href="artist_stats.html"><img id="statsButton" title="Stats" src="/static/img/icons/stats.png" alt=""></a> </div>
尝试改变这种结构
templates/
atmos_v4/
index.html
然后在settings.py中删除此项
“从操作系统路径导入联接”
编辑:在你现在更新你的文件之后,我发现你有一个坏的index.html文件,你需要使用模板和url的内置函数
***在此index.html文件中,其他html文件(与index.html文件一起位于templates文件夹中)正在index.html文件的div标记中加载。您需要为所有这些页面设置URL…您可以编辑问题以更正该句子。但我实际上不知道这意味着什么-你忽略了发布一个例子,说明你是如何做到这一点的,所以这个问题是无法回答的。我的意思是,该网站的设置使得单独的html页面被加载到另一个html文件(index.html)中的div标记中。当包含所述html文件的templates文件夹移动到我的静态文件夹时,我已经能够加载html文件。但是,当模板文件夹按我上面的方式设置时,html文件不会加载。你说的“正在加载html页面”是什么意思?你是说他们被联系到了一起?你什么时候看到错误的?你看到了什么错误?
templates/
atmos_v4/
index.html