仅在VPS上执行TemplateDoesNotExist Django错误-在本地运行良好

时间:2018-08-01 04:52:01

标签: python django

我知道还有很多其他类似问题,但是我相信我的案子是独一无二的-我花了数小时,从遇到的每个网站和youtube教程中应用了修复程序,但仍然出现此错误。我想知道为什么:

这是错误:

TemplateDoesNotExist at /skills/
skills\skills.html

这是我的设置。py

"""
Django settings for pranavblog project.

Generated by 'django-admin startproject' using Django 2.0.7.

For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '...'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['host1',host2','127.0.0.1']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'posts',
    'sitepages',
    'hackathons',
    'projects',
    'pagedown',
    'skills.apps.SkillsConfig',
    'django.forms'
]
FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'
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 = 'pranavblog.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(os.path.dirname(__file__),'../templates'),],
        '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 = 'pranavblog.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

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',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True
SITE_ID = 1

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_URL = '/pics/'
MEDIA_ROOT = BASE_DIR

这是我的观点。py

from django.shortcuts import render,get_object_or_404
from .models import Skill
# Create your views here.

def skills(request):
    Skills = Skill.objects.all()
    return render(request,'skills\skills.html', {"Skills":Skills})

def skill_details(request, skill_id):
    skills = get_object_or_404(Skill, pk=skill_id)
    return render(request,'skills/skill_details.html', {'skill':skills})

最后,这是我的技能页面网址

path('skills/',skills.views.skills,name="skills"),
path('skills/<int:skill_id>/', skills.views.skill_details,name="skill_details")

奇怪的是,此错误仅在部署到外部VPS期间发生。当我在本地运行代码并导航到localhost时,一切正常。当我将代码部署到网站并导航到“技能”选项卡的网站URL时,它将引发TemplateDoesNotExist错误。

有人知道为什么会这样吗?我使用FileZilla进行文件传输,使用gunicorn和nginx进行网站。每次进行更改时,我都会重新启动ngninx和gunicorn-不是因为这个原因。

2 个答案:

答案 0 :(得分:1)

答案:

我不小心使用了两个反斜杠'\',而我应该只使用正斜杠'/'。下次必须小心修复。

感谢大家的帮助。

答案 1 :(得分:0)

您应该在settings.py文件中定义如下模板的目录

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(os.path.dirname(BASE_DIR), 'templates'),],
        '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',
            ],
        },
    },
]