python - "Can't find template" error in Django -
i started learning django. followed guide on django webpage, still don't feel understood it. decided make similar myself. making similar voting system in guide, bit different. started doing reading documentations , guide text. created generic listview display index.html, show list of votings. '
this views code:
class indexview(generic.listview): template_name = 'vote/index.html' model = type def get_queryset(self): return type.objects.order_by('-pub_date')
here index.html code:
{% load staticfiles %} <link rel="stylesheet" type="text/css" href="{% static 'vote/style.css' %}" /> {% block content %} <h2>votings</h2> <ul> {% voting in object_list %} <li>{{ voting.type_name }}</li> {% empty %} <li>sorry, there not votes.</li> {% endfor %} </ul> {% endblock %}
models code:
from django.db import models django.utils import timezone # create models here. class voted_object(models.model): voted_object_name = models.charfield(max_length=50) voted_object_image = models.imagefield voted_object_rating = models.integerfield(default=0) class type(models.model): pub_date = models.datetimefield type_name = models.charfield(max_length=50)
urls code:
from django.conf.urls import url . import views urlpatterns = [ url(r'^$', views.indexview.as_view(), name='index'), #url(r'^(?p<pk>[0-9]+)/results/$', views.resultsview.as_view(), name='results'), # url(r'^(?p<pk>[0-9]+)/voting/$', views.voteview.as_view(), name='voting'), ]
there settings templates:
templates = [ { 'backend': 'django.template.backends.django.djangotemplates', 'dirs': [base_dir], '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 directory:
and error:
templatedoesnotexist @ /vote/
vote/index.html, vote/type_list.html request method: request url: http://127.0.0.1:8000/vote/ django version: 1.8.2 exception type: templatedoesnotexist exception value: vote/index.html, vote/type_list.html exception location: c:\users\vato\envs\django_test_env\lib\site-packages\django\template\loader.py in select_template, line 76 python executable: c:\users\vato\envs\django_test_env\scripts\python.exe python version: 2.7.10 python path: ['c:\users\vato\pycharmprojects\project3', 'c:\users\vato\pycharmprojects\project3', 'c:\windows\system32\python27.zip', 'c:\users\vato\envs\django_test_env\dlls', 'c:\users\vato\envs\django_test_env\lib', 'c:\users\vato\envs\django_test_env\lib\plat-win', 'c:\users\vato\envs\django_test_env\lib\lib-tk', 'c:\users\vato\envs\django_test_env\scripts', 'c:\python27\lib', 'c:\python27\dlls', 'c:\python27\lib\lib-tk', 'c:\users\vato\envs\django_test_env', 'c:\users\vato\envs\django_test_env\lib\site-packages']
it asks 2 templates 1 index.html have , wrote , second type_list.html
i think error caused missing file of type_list.html, don't why django asks me template. where in code specify need it? , how can fix program votes database , display them on index?
as researched , understand second template looked because of model(type)-to lower case , _list ending reason. made somewhere automatically, don't understand it.
i not sure in code, cause copied documentations, thought should have worked without second(type_list) template. sorry long post. thought shouldn't miss code.
if have suggestions better way of learning django please feel free comment.
you have 'dirs': [base_dir],
templates not in base_dir, in base_dir/templates. should have:
'dirs': [os.path.join(base_dir, 'templates')],
Comments
Post a Comment