Django 1.10声称模板不存在,但它显然确实存在

时间:2016-12-15 23:48:45

标签: django django-templates

enter image description here

但是如果我将该位置复制并粘贴到firefox中,模板就在那里......

enter image description here

views.py:

def artists(request):
    artists = Artist.objects.all()
    template = loader.get_template('artists/index.html')
    context = {'artists': artists,}
    return HttpResponse(template.render(context, request))

的index.html:

{% extends "site_base.html" %}

{% load i18n %}

{% block head_title %}pinax-project-account{% endblock %}

{% block body_class %}home{% endblock %}

{% block body_base %}
    <section class="jumbotron">
        <div class="container">
            {% include "_messages.html" %}
            <h1>{% blocktrans %}Welcome to<br>pinax-project-account{% endblocktrans %}</h1>
            <p>
                {% blocktrans %}
                In addition to what is provided by the "zero" project, this project
                provides thorough integration with django-user-accounts, adding
                comprehensive account management functionality. It is a foundation
                suitable for most sites that have user accounts.
                {% endblocktrans %}
            </p>
            {% if not user.is_authenticated %}
            {% url "account_login" as login_url %}
            {% url "account_signup" as signup_url %}
            <p>{% blocktrans %}You can <a href="{{ login_url }}" class="btn btn-default">Log In</a> or <a href="{{ signup_url }}" class="btn btn-primary">Sign Up</a> to try out the site.{% endblocktrans %}</p>
            {% endif %}
        </div>
    </section>
    <section>
        <div class="container">
            <h2 class="text-center">{% blocktrans %}What is Pinax?{% endblocktrans %}</h2>
            <p class="lead">
                {% blocktrans %}
                <b>Pinax</b> is an open-source platform based on Django and
                intended to provide a starting point for websites. It takes
                care of the things that many sites have in common, so you can
                focus on what makes your site different.
                {% endblocktrans %}
            </p>
            <div class="feature-columns">
                <div>
                    <i class="fa fa-cubes fa-3x"></i><br>
                    {% blocktrans %}
                    <b>Starter projects</b> provide project layout,
                    scaffolding, already integrated components and
                    ready-to-go code.
                    {% endblocktrans %}
                </div>
                <div>
                    <i class="fa fa-puzzle-piece fa-3x"></i><br>
                    {% blocktrans %}
                    <b>Reusable apps</b> provide common
                    infrastructure, back-end functionality,
                    and user-facing components.
                    {% endblocktrans %}
                </div>
                <div>
                    <i class="fa fa-tint fa-3x"></i><br>
                    {% blocktrans %}
                    <b>Themes</b> provide default templates and
                    stylesheets for quick prototyping and easy customization.
                    {% endblocktrans %}
                </div>
            </div>
        </div>
    </section>
    <section>
        <div class="container">
            <p class="lead text-center">
                {% blocktrans %}
                See <a href="http://pinaxproject.com/">pinaxproject.com</a>
                for more information.
                {% endblocktrans %}
            </p>
        </div>
    </section>
{% endblock %}

Settings.py:

import os


PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
PACKAGE_ROOT = os.path.abspath(os.path.dirname(__file__))
BASE_DIR = PACKAGE_ROOT

DEBUG = True

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": "dev.db",
    }
}

ALLOWED_HOSTS = [
    "localhost",
]

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = "UTC"

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = "en-us"

SITE_ID = int(os.environ.get("SITE_ID", 1))

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = os.path.join(PACKAGE_ROOT, "site_media", "media")

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = "/site_media/media/"

# Absolute path to the directory static files should be collected to.
# Don"t put anything in this directory yourself; store your static files
# in apps" "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(PACKAGE_ROOT, "site_media", "static")

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = "/site_media/static/"

# Additional locations of static files
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, "static", "dist"),
]

STATICFILES_STORAGE = "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

# Make this unique, and don't share it with anybody.
SECRET_KEY = "#t_)=9g2nssm4!6rw^vq(1_sqbnxi4zky--b8!crlvp(f1r=ol"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [
            os.path.join(PACKAGE_ROOT, "templates"),
            os.path.join(BASE_DIR, 'templates/'),
            '/home/shawn/Workspace/WebSites/artCollective/ac3/ac3/templates/ac3/',
        ],
        "APP_DIRS": True,
        "OPTIONS": {
            "debug": DEBUG,
            "context_processors": [
                "django.contrib.auth.context_processors.auth",
                "django.template.context_processors.debug",
                "django.template.context_processors.i18n",
                "django.template.context_processors.media",
                "django.template.context_processors.static",
                "django.template.context_processors.tz",
                "django.template.context_processors.request",
                "django.contrib.messages.context_processors.messages",
                "account.context_processors.account",
                "pinax_theme_bootstrap.context_processors.theme",
            ],
        },
    },
]

MIDDLEWARE = [
    "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 = "ac3.urls"

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = "ac3.wsgi.application"

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.messages",
    "django.contrib.sessions",
    "django.contrib.sites",
    "django.contrib.staticfiles",

    # theme
    "bootstrapform",
    "pinax_theme_bootstrap",

    # external
    "avatar",
    "account",
    "pinax.eventlog",
    "pinax.webanalytics",

    # project
    "ac3",
    "artists",
]

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "filters": {
        "require_debug_false": {
            "()": "django.utils.log.RequireDebugFalse"
        }
    },
    "handlers": {
        "mail_admins": {
            "level": "ERROR",
            "filters": ["require_debug_false"],
            "class": "django.utils.log.AdminEmailHandler"
        }
    },
    "loggers": {
        "django.request": {
            "handlers": ["mail_admins"],
            "level": "ERROR",
            "propagate": True,
        },
    }
}

FIXTURE_DIRS = [
    os.path.join(PROJECT_ROOT, "fixtures"),
]

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

ACCOUNT_OPEN_SIGNUP = True
ACCOUNT_EMAIL_UNIQUE = True
ACCOUNT_EMAIL_CONFIRMATION_REQUIRED = False
ACCOUNT_LOGIN_REDIRECT_URL = "home"
ACCOUNT_LOGOUT_REDIRECT_URL = "home"
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 2
ACCOUNT_USE_AUTH_AUTHENTICATE = True

AUTHENTICATION_BACKENDS = [
    "account.auth_backends.UsernameAuthenticationBackend",
]
I have

花了好几个小时才应该有一个简单的解决方案。 Django正在寻找模板的正确位置,为什么不能渲染?

2 个答案:

答案 0 :(得分:1)

尝试从

中的settings.py中删除templates之后的斜杠
os.path.join(BASE_DIR, 'templates')

答案 1 :(得分:0)

我遇到了同样的问题,最后解决了。如果您在记事本中创建了html文件;

  • 将记事本文件中的内容复制并粘贴到写字板文件中。

  • 另存为...然后在下拉栏中选择纯文本文件

古德勒克!