Reputation: 2839
I just started learning Django. I followed the guide on the Django webpage, but still don't feel that understood it. So I decided to make something similar by myself. Basically I am making similar voting system as in guide, but a bit different. So I started doing it by reading some documentations and guide text. I created a generic listview to display index.html, which will show the list of votings. '
this is my 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 is my index.html code:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'Vote/style.css' %}" />
{% block content %}
<h2>Votings</h2>
<ul>
{% for voting in object_list %}
<li>{{ voting.Type_name }}</li>
{% empty %}
<li>Sorry, there are not votes.</li>
{% endfor %}
</ul>
{% endblock %}
Models code:
from django.db import models
from django.utils import timezone
# Create your 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
from . 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 is also settings for 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 is my directory:
and finally this is the error:
TemplateDoesNotExist at /Vote/
Vote/index.html, Vote/type_list.html Request Method: GET 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 for 2 templates one index.html which I have and I wrote and second for type_list.html
I think the error is caused by missing file of type_list.html, but I don't get why django asks me for that template. Where in the code do I specify the need for it? and How can I fix it so that the program will get votes from database and display them on index?
as I researched and as I understand second template is looked because of model(Type)-to lower case and _list ending for some reason. it is made somewhere automatically, but I don't understand it.
I am not sure in my code, cause much is copied from documentations, but as I thought it should have worked without the second(type_list) template. Sorry for the long post. Thought shouldn't miss any code.
If you have any suggestions for the better way of learning django please feel free to comment.
Upvotes: 1
Views: 577
Reputation: 599956
You have 'DIRS': [BASE_DIR],
but your templates are not in BASE_DIR, they are in BASE_DIR/templates. You should have:
'DIRS': [os.path.join(BASE_DIR, 'templates')],
Upvotes: 2