TEMPLATE_DIRS和模板文件夹请帮忙

时间:2015-02-19 08:56:57

标签: django templates url

所以我拆开了一个已经建成前端的网站,并承担了构建后端的任务,但我是django的新手。我已经设法将相应的东西分开(css,js,img在静态文件夹中,html文件在模板文件夹中等),到目前为止已经能够将index.html设置为django的主页。在此index.html文件中,其他html文件(位于templates文件夹中与index.html文件一起)正在index.html文件的标记中加载。目前所有的图像,css和js都在文件中传出,但html文件却没有。我将这些html文件称为" file.html的名称"但是,我似乎无法加载这些,除非我将模板文件夹移动到静态文件夹(与css,images,js等一起)并将引用更改为" static / templates / name of file.html& #34;

我的项目设置如下。我也在运行django 1.7。

    atmos_v4/
        atmos_v4/
           init__.py
           settings.py
           urls.py
           wsgi.py
        db.sqlite3
        manage.py
        static/
             css/
                 ...
             img/
                 ...
             js/
                 ...
             media/
                 ...
        templates/
             index.html
                 ...

我的urls.py和settings.py如下。我有一种感觉我的TEMPLATE_DIRS和STATIC_ROOT可能设置不正确。如果没有,有人会非常友好地告诉我我失踪了吗?谢谢!

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,
)

urls.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>

1 个答案:

答案 0 :(得分:-3)

templates/
         index.html

尝试更改为此结构

templates/
         atmos_v4/
                  index.html

并在settings.py内删除此内容 &#34; 来自os.path导入加入&#34;

编辑:现在更新文件后我发现你的index.html文件不好,你需要使用模板和网址的内置函数

https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#url