Reputation: 63
I am using django version 11.3. I create a separate HTML template file and import in my views.py file when i run this file it gives me an error.
ModuleNotFoundError at /myapp/
No module named 'django.templates'
My settings.py file code:
TEMPLATES = [
{
'BACKEND': 'django.templates.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.templates.context_processors.debug',
'django.templates.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
views.py file:
from django.http import HttpResponse
from django .template import loader
from .models import album
def myapp(request):
all_albums = album.objects.all()
template = loader.get_template('myapp/index.html')
context = {
'all_albums': all_albums,
}
return HttpResponse(template.render(context, request))
def detail(request, id):
return HttpResponse("<h1>Your requested numbers is: " + str(id) +"<h2>")
html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>This is list item</title>
</head>
<body>
<ul>
{% for a in all_albums %}
<li><a href="/myapp/{{ a.id }}">{{ a.artist }}</a></li>
{% endfor %}
</ul>
</body>
</html>
Directory structure:
myapp > templates > myapp > index.html
Upvotes: 0
Views: 1776
Reputation: 931
You must use django.template
without the s
:
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',
],
},
}]`
Upvotes: 3