python 模板不存在于Django

icnyk63a  于 2023-02-07  发布在  Python
关注(0)|答案(2)|浏览(165)

我有这样的项目:

├── manage.py
├── myProjet
│   ├── __init__.py
│   ├── settings.py
│   ├── templates
│   ├── urls.py
│   ├── wsgi.py
│   └── wsgi.pyc
├── app1
├── templates

当我运行这个项目时,我总是得到这个错误:TemplateDoesNotExist at/我已尝试了所有方法,但无法修复该问题。我的settings.py文件如下所示:

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

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app1',
]

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

我试过很多方法,但总是出错。错误是在注册函数上引发的。函数是这样的:

def SignupPage(request):
    if request.method=='POST':
        uname=request.POST.get('username')
        email=request.POST.get('email')
        pass1=request.POST.get('password1')
        pass2=request.POST.get('password2')

        if pass1!=pass2:
            return HttpResponse("Your password and confrom password are not Same!!")
        else:

            my_user=User.objects.create_user(uname,email,pass1)
            my_user.save()
            return redirect('login')
        


    return render (request,'signup.html')

更新错误

TemplateDoesNotExist at /
signup.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 4.1.6
Exception Type: TemplateDoesNotExist
Exception Value:    
signup.html
Exception Location: /home/myPC/myProject/my_env/lib/python3.8/site-packages/django/template/loader.py, line 19, in get_template
Raised during:  app1.views.SignupPage
Python Executable:  /home//myProject/my_env/bin/python
Python Version: 3.8.10
Python Path:    
['/home/myPC/myProject/pmTools_V1',
 '/usr/lib/python38.zip',
 '/usr/lib/python3.8',
 '/usr/lib/python3.8/lib-dynload',
 '/home/myPC/myProject/my_env/lib/python3.8/site-packages']
dpiehjr4

dpiehjr41#

因为django使用的是pathlib,我会选择:

"DIRS": [
            BASE_DIR / "templates"
        ],

在您的settings.py模板部分。

tkclm6bt

tkclm6bt2#

尝试替换:

'DIRS': ['templates'],

签署人:

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

TEMPLATES截面中。

    • 更新**

试试看:

from django.http import HttpResponse
from django.template import loader

def SignupPage(request):
    template = loader.get_template('signup.html')

    # Your code here

    return HttpResponse(template.render(context, request))

相关问题