简体   繁体   English

django 从模板创建启动页面

[英]django creating a splash page from templates

I am trying to create a default splash page from the templates and I keep getting an error that my template does not exist.我正在尝试从模板创建默认启动页面,但我不断收到我的模板不存在的错误。

>>> import django

>>> django.VERSION
(2, 0, 5, 'final', 0)

Error:错误:

Exception Type:     TemplateDoesNotExist

Session:会议:

root@localhost:/home/robin/www/trader# ls root@localhost:/home/robin/www/trader# ls

admin  buy  db.sqlite3  manage.py  manager  __pycache__  sell  templates  trader  views.py

root@localhost:/home/robin/www/trader# cat trader/urls.py root@localhost:/home/robin/www/trader# cat trader/urls.py

#from django.contrib import admin
from django.urls import path
#from django.conf.urls import patterns, url, include
#from django.views.generic import TemplateView
from . import views

app_name = 'trader'
urlpatterns = [
        path('', views.index, name='index'),
#       path('admin/', admin.site.urls),
        #url(r"^$", trader, {"template": "index.html"}),
#       url(r'^buy/', include('buy.urls')),
]

root@localhost:/home/robin/www/trader# cat trader/views.py root@localhost:/home/robin/www/trader# cat trader/views.py

from django.shortcuts import render
from django.http import Http404
from django.http import HttpResponse
from django.template import loader
def index(request):
        #return HttpResponse('<p>Hello Index from trader/views.py</p>')
        return render(request, 'trader/index.html')

root@localhost:/home/robin/www/trader# ls templates/ root@localhost:/home/robin/www/trader# ls 模板/

buy  manager  sell  trader

root@localhost:/home/robin/www/trader# cat templates/trader/index.html root@localhost:/home/robin/www/trader# cat templates/trader/index.html

<a>trader templates index</a>

root@localhost:/home/robin/www/trader# cat trader/settings.py root@localhost:/home/robin/www/trader# cat trader/settings.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = 'xxx'

DEBUG = True

ALLOWED_HOSTS = ['tradercoinz.com']

INSTALLED_APPS = [
        'trader',
        'buy',
        'sell',
        'manager',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
]

MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        '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 = 'trader.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'trader.wsgi.application'

DATABASES = {
        'default': {
                'ENGINE': 'django.db.backends.mysql',
                'NAME': 'xxx',
                'USER': 'xxx',
                'PASSWORD': 'xxx',
                'HOST': '127.0.0.1',
                'PORT': '',
        }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True
STATIC_URL = '/static/'

Full error:完整错误:

Request Method:     GET
Request URL:    http://coinz.com/
Django Version:     2.0.5
Exception Type:     TemplateDoesNotExist
Exception Value:    

trader/index.html

Exception Location:     /usr/local/lib/python3.6/dist-packages/django/template/loader.py in get_template, line 19
Python Executable:  /usr/bin/python3
Python Version:     3.6.5
Python Path:    

['/home/robin/www/trader',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '/usr/local/lib/python3.6/dist-packages',
 '/usr/local/lib/python3.6/dist-packages/setuptools-39.1.0-py3.6.egg',
 '/usr/lib/python3/dist-packages']

Server time:    Sun, 13 May 2018 11:09:06 +0000
Template-loader postmortem

Django tried loading these templates, in this order:

Using engine django:

    django.template.loaders.app_directories.Loader: /usr/local/lib/python3.6/dist-packages/django/contrib/admin/templates/trader/index.html (Source does not exist)
    django.template.loaders.app_directories.Loader: /usr/local/lib/python3.6/dist-packages/django/contrib/auth/templates/trader/index.html (Source does not exist)

Traceback Switch to copy-and-paste view

    /usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py in inner

                    response = get_response(request)

         ...
    ▶ Local vars
    /usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py in _get_response

                        response = self.process_exception_by_middleware(e, request)

         ...
    ▶ Local vars
    /usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py in _get_response

                        response = wrapped_callback(request, *callback_args, **callback_kwargs)

         ...
    ▶ Local vars
    /home/robin/www/trader/trader/views.py in index

            return render(request, 'trader/index.html')

         ...
    ▶ Local vars
    /usr/local/lib/python3.6/dist-packages/django/shortcuts.py in render

            content = loader.render_to_string(template_name, context, request, using=using)

         ...
    ▶ Local vars
    /usr/local/lib/python3.6/dist-packages/django/template/loader.py in render_to_string

                template = get_template(template_name, using=using)

         ...
    ▶ Local vars
    /usr/local/lib/python3.6/dist-packages/django/template/loader.py in get_template

            raise TemplateDoesNotExist(template_name, chain=chain)

         ...
    ▶ Local vars 

尝试在“SETTINGS.PY”中的“Template DIR”中添加您的模板文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM