0

this is current_datetime.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    it is now {{date time}}
</body>
</html>

this is templates in settings.py

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

this is views.py

import datetime

from django.shortcuts import render

def current_datetime(request):
    now=datetime.datetime.now()
    return render(request,'mysite/template/current_datetime.html',{'datetime':now})

this is urls.py

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls,name="admin"),
    path('current_datetime/',views.current_datetime,name="current_datetime"),
]

and this is my directory

enter image description here

but when i run server by "python manage.py run server" and go to this url:

http://127.0.0.1:8000/current_datetime/

i got this error:

TemplateDoesNotExist at /current_datetime/

saeedzali
  • 45
  • 6

2 Answers2

1

I believe the template should be loading fine (assuming that you changed the folder name to templates instead of template, but it's the path that may be incorrect.

def current_datetime(request):
now=datetime.datetime.now()
return render(request, '/current_datetime.html', {'datetime':now})

This is because Django has already found your templates directory, so now you only need to specify the path to the file within the folder itself.

Raheel Junaid
  • 577
  • 3
  • 12
  • it returns same error as before @raheeljunaid – saeedzali Dec 26 '20 at 16:27
  • Have you tried any of the solutions from this thread: [Django TemplateDoesNotExist?](https://stackoverflow.com/questions/1926049/django-templatedoesnotexist) – Raheel Junaid Dec 26 '20 at 16:34
  • yes i tried them and i use pycharm community edition so i create templates folder myself @raheeljunaid – saeedzali Dec 26 '20 at 16:51
  • Thank you for the information @saeedzali. You should use the `django-admin startproject` and `django-admin startapp` commands instead of having to do everything manually. I'd try that method and make sure to put your new app inside of the settings.py `INSTALLED_APPS`. – Raheel Junaid Dec 26 '20 at 16:58
  • i used them before but in community edition of pycharm , it does not create by coomands that you tell @raheeljunaid – saeedzali Dec 26 '20 at 17:20
  • Have you tried running django-admin through a regular command line instead of PyCharm? I can see that your manage.py file is working correctly which means there may be something wrong with your Virtual Environment. – Raheel Junaid Dec 26 '20 at 17:31
  • i do that but it does not create templates directory @raheeljunaid – saeedzali Dec 26 '20 at 18:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226453/discussion-between-raheel-junaid-and-saeedzali). – Raheel Junaid Dec 26 '20 at 18:49
1

You had everything set up correctly except for a few things

  • Your templates folder was named "template" instead of "templates"
  • You didn't create a Django App and were instead working in the root project folder

To fix this problem you have to run

$ django-admin startapp "your_app_name"

And then add the app under INSTALLED_APPS in settings.py. Once that has been created, you can move the templates folder into the new app and then the URL route "/current_datetime.html" should work. Django automatically goes through the template folders in every app, but if you don't have an app then it throws a TemplateDoesNotExist error.

I'm glad I could help!

Raheel Junaid
  • 577
  • 3
  • 12