【问题标题】:TEMPLATE_DIRS and templates folder help please请帮助 TEMPLATE_DIRS 和模板文件夹
【发布时间】:2015-02-19 08:56:57
【问题描述】:

所以我正在拆分一个前端已经构建好的网站,并承担了构建后端的任务,但我是 django 的新手。我已经设法将所有内容相应地分开(css、js、img 在静态文件夹中,html 文件在模板文件夹中等),并且到目前为止已经能够将 index.html 设置为 django 的主页。在这个 index.html 文件中,其他 html 文件(与 index.html 文件一起位于模板文件夹中)正在 index.html 文件的标签中加载。目前所有的图像、css 和 js 都在文件中,但 html 文件不是。我将这些 html 文件引用为“file.html 的名称”但是,除非我将模板文件夹移动到静态文件夹(与 css、图像、js 等一起)并更改引用,否则我似乎无法加载这些文件到“静态/模板/file.html 的名称”

我的项目设置如下。我也在运行 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>

【问题讨论】:

  • ***在这个 index.html 文件中,其他 html 文件(与 index.html 文件一起位于模板文件夹中)正在 index.html 文件的 div 标记中加载。跨度>
  • 您需要为所有这些页面设置 URL...
  • 您可以编辑您的问题以更正该句子。但我实际上不知道这意味着什么 - 你忽略了发布一个你是如何做到这一点的例子,所以这个问题是无法回答的。
  • 我的意思是该站点已设置为在另一个 html 文件 (index.html) 中的 div 标记内加载单独的 html 页面。当包含所述 html 文件的模板文件夹移动到我的静态文件夹中时,我已经能够加载 html 文件。但是,当我在上面设置模板文件夹时,不会加载 html 文件。
  • “正在加载 html 页面”是什么意思?你的意思是他们有联系吗?你什么时候看到错误?你看到了什么错误?

标签: django templates url


【解决方案1】:
templates/
         index.html

尝试改成这个结构

templates/
         atmos_v4/
                  index.html

并在 settings.py 中删除它 "从 os.path 导入加入"

编辑:在你更新你的文件后,我发现你有错误的 index.html 文件,你需要使用模板和 url 的内置函数

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多