0

I am making a polls application in django and I get the following error:

TemplateDoesNotExist at /polls/

This is what my index function looks like this:

def index(request):
    latest_questions = Question.objects.order_by('-pub_date')[0:5]
    context = {'latest_questions': latest_questions}

    return render(request, "polls/template.html", context)

template.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% if latest_ques %}
    <ul>
        {% for question in latest_ques%}
           <li><a href = '/polls/{{question_id}}/'><b>
{{question.ques_text}}</b></a></li>
        {% endfor %}
    </ul>
{% endif %}
</body>
</html>

Inside of my polls file I have a template file and inside of that I have a template folder inside of that I have another polls folder and inside of that I have template.html.

I have tried using render_to_response instead of render, I also tried to add the path to DIRS in settings.py and I tried taking request out of the function. Thank you so much.

cezar
  • 11,616
  • 6
  • 48
  • 84
LionCoder
  • 3
  • 1

2 Answers2

0

In your settings file you need to add template path

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

Add your app to INSTALLED_APPS in settings.py And in your TEMPLATES settings set 'APPS_DIRS' = True

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

'polls',
]
...

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