在/ django的TemplateDoesNotExist

时间:2018-04-18 21:57:16

标签: django python-3.x

出错了

enter image description here

views.py:

def index(request):
        user = request.user
        return render(request, 'main_app/index.html',{'user': user})

试过这个:

'DIRS': [os.path.join(BASE_DIR, 'templates')],

但没有帮助

CMD:

Internal Server Error: /
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/ubuntu/workspace/main_app/views.py", line 10, in index
    return render(request, 'main_app/index.html',{'user': user})
  File "/usr/local/lib/python3.4/dist-packages/django/shortcuts.py", line 36, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/usr/local/lib/python3.4/dist-packages/django/template/loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
  File "/usr/local/lib/python3.4/dist-packages/django/template/loader.py", line 19, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: main_app/index.html

文件结构: enter image description here

Django 2.0

Settings.py

INSTALLED_APPS = [
# 'main_app.apps.MainAppConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main_app'

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

3 个答案:

答案 0 :(得分:0)

BASE_DIR是根目录 您需要一个名为

的个人应用程序的PROJECT_APP_PATH
class AudioField(serializers.FileField):
    def to_internal_value(self, data):
        if isinstance(data, basestring):
            data = re.sub(r"^data\:.+base64\,(.+)$", r"\1", data)

            # Try to base64 decode the data url.
            try:
                decoded = base64.b64decode(data)
            except TypeError:
                raise serializers.ValidationError(_('Not a valid file'))

            file_name, file_ext, mime_type = self.i_need_the_file_extension_mimetype(decoded)


            data = ContentFile(decoded, name=file_name)

            return super(AudioField, self).to_internal_value(data)

快乐编码

答案 1 :(得分:0)

您应该将模板从main_app/templates移动到嵌套的子文件夹main_app/templates/main_app

例如,在django tutorial

中对此进行了解释

答案 2 :(得分:0)

def索引(请求): 表格= RegisterationForm() 上下文= { ” } 返回render(request,'index.html',上下文)