Reputation: 101
WEBSITEviews.py
from django.shortcuts import render
from django.utils import timezone
from .models import Post
def minjuand(request):
posts = Post.objects.filter(published_date__lte = timezone.now()).order_by('published_date')
return render(request,'blog/minjuand.html',{'posts':posts})
urls.py
from django.urls import path
from . import views
urlpatterns=[
path('', views.minjuand, name="minjunad"),
]
in settings.py
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',
],
},
},
]
I added my application to my settings.py
at the end of INSTALLED_APPS
and checked for typos. Why can't Django find my template?
I am developing with Django 3.1.4 and Python 3.9.0 on Windows 10.
Upvotes: 0
Views: 469
Reputation: 1218
There's no need to define additional template directories in the Django settings. All registered apps (apps that are in your INSTALLED_APPS
list in settings.py
) use a default templates directory my-app-directory/templates/
.
Inside your app directory, create a folder called templates
(all lowercase), place your templates inside this folder. So if you had an app called blog
it would look something like this:
> blog/
> migrations/
> 0001_initial.py
> templates/
> my_template.html
> admin.py
> apps.py
> models.py
> views.py
> mysite/
> settings.py
> urls.py
> wsgi.py
> manage.py
You can then use your my_template.html
template in a view like this:
def my_view(request):
context = {}
return render(request,'my_template.html', context)
Note, we reference the template as if we are inside the templates/
directory
Upvotes: 2
Reputation: 1
You should create the templates directory inside the folder containing folder of project and app. If you have done that, then os.path.join(BASE_DIR, 'templates')
do this change in TEMPLATES
in settings.py
Upvotes: 0