Mohammed Rafik
Mohammed Rafik

Reputation: 51

TemplateDoesNotExist at /search/ in Django 2.0.2

when I try to execute the function, I am getting (TemplateDoesNotExist at /search/) my templates folder is located at C:\Users\Rafik\Documents\myproject\env_mysite\Scripts\mysie\books\templates python code is running fine but it says TemplateDoesNotExist

views.py:

def search(request):

error = False

if 'q' in request.GET:

    q = request.GET['q']

    if not q:

        error = True

    elif len(q) > 20:

        error = True

    else:

        books = Book.objects.filter(title__icontains=q)

        return render(request, 'search_results.html', {'books': books, 'query': q})

return render(request, 'search_form.html', {'error': error})

apps/urls.py:

from django.conf.urls import url

from books import views

urlpatterns = [

url(r'^search-form/$', views.search_form),
url(r'^search/$', views.search)

]

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

]

Upvotes: 2

Views: 841

Answers (1)

Beat Lang
Beat Lang

Reputation: 31

I am new to stackoverflow as an author, so please excuse me for not using the appropriate markup here. I run into the same error when working through the Django 2.0 tutorial (more specifically: in Tutorial03, after switching from loader.get_template() to shortcuts.render()). The following solution finally proved to work for me:

  • In the TEMPLATES section of my settings.py, DIRS remains an empty list and APP_DIRS is set to True.
  • In the render() statement, there is no path portion when specifying the template, just e.g. render(request, 'index.html', context)
  • the template file must directly be located in the app/templates directory (not in /app/templates/app)

My enviroment is Django 2.0.2 with CPython 3.6.4 under Windows 10 (and cygwin). Hope this helps.

Upvotes: 3

Related Questions