0

I have the project like this:

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

When I run the project, I am always getting this error: TemplateDoesNotExist at/ I have tried everything, but I can't fix it. My settings.py file is this:

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

I have tried many ways, but I am always getting errors. The error is raised on the signup function. The function is this:

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')

UPDATE ERROR

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']
Manoj Kamble
  • 620
  • 1
  • 4
  • 21
iuatsj
  • 11
  • 6

2 Answers2

0

Try to replace:

'DIRS': ['templates'],

By:

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

in TEMPLATES section.

Update

Try:

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))
Corralien
  • 109,409
  • 8
  • 28
  • 52
0

Since django is using pathlib I'd go with:

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

in your settings.py templates section.

Gameplay
  • 1,142
  • 1
  • 4
  • 16
  • If you look carefully the `settings.py`, the `BASE_DIR` is not a `Path` instance but a string. – Corralien Feb 06 '23 at 09:19
  • I didn't say it is, I just said `pathlib` is being used in `settings.py` by default. If you initialize an empty project that's what's gonna be at the top of the settings file (Django 4.1.5): `# Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent`. It's their (django creators') recommendation to build path's like that, not my idea. – Gameplay Feb 06 '23 at 09:22
  • "by default" for recent version of Django, I'm agree with you. However if OP uses your code, I think it will raise an exception: `TypeError: unsupported operand type(s) for /: 'str' and 'str'` – Corralien Feb 06 '23 at 09:27
  • Don't think so. I have django app with above code and it works just fine. (python 3.11, django 4.1.5) – Gameplay Feb 06 '23 at 09:28
  • You have this line in settings.py??? `BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))` – Corralien Feb 06 '23 at 09:31
  • It doens't work. I got an error: `TypeError: unsupported operand type(s) for /: 'str' and 'str'` @Gameplay – iuatsj Feb 06 '23 at 09:31
  • @iuatsj. You have to use the old way with `os.path` module (check my answer). However, if you start a new project, it will be better to upgrade your Django's version (if your python version is compatible) – Corralien Feb 06 '23 at 09:33
  • It does work, maybe you have older version of django and please take a look at this: https://stackoverflow.com/a/63845208/15923186 And @Corralien I posted my `BASE_DIR` above. It's: `BASE_DIR = Path(__file__).resolve().parent.parent` (and I didn't change it or modify it in any way, that's what django auto generated) – Gameplay Feb 06 '23 at 09:33
  • @Corralien It still doesn't work with `BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))` I still got the same error `TemplateDoesNotExist at/ ` – iuatsj Feb 06 '23 at 09:40
  • I have the 4.1.6 Django version – iuatsj Feb 06 '23 at 09:42