2015-06-25 32 views
1

我剛開始學習Django。我遵循Django網頁上的指南,但仍然不覺得它理解它。所以我決定自己做一些類似的事情。基本上我正在製作類似於指南的投票系統,但有點不同。所以我開始通過閱讀一些文件和指導文本來做到這一點。我創建了一個通用的listview來顯示index.html,它將顯示投票列表。 「Django中的「無法找到模板」錯誤

這是我的看法代碼:

class IndexView(generic.ListView): 
    template_name = 'Vote/index.html' 
    model = Type 

    def get_queryset(self): 
     return Type.objects.order_by('-pub_date') 

這裏是我的index.html代碼:

{% 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 %} 

型號代碼:

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) 

的url代碼:

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

還爲模板設置:

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

這是我的目錄: enter image description here

最後是這樣的錯誤:在/投票

TemplateDoesNotExist/

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

它要求2個模板中的一個index.html,我已經寫過,其次是type_list。 HTML

我認爲錯誤是由缺少type_list.html的文件引起的,但我不知道爲什麼Django的問我該模板。 在代碼中,我需要指定它嗎?我該如何修復它,以便程序將從數據庫中獲得投票並將它們顯示在索引上?

爲我的研究和我的理解第二個模板看着因爲Model(型號)-to小寫,由於某種原因_List結束。它是自動進行的,但我不明白。

我不知道在我的代碼,造成多大的從單證複製,但我認爲應該沒有第二個(TYPE_LIST)模板工作過。對不起,很長的職位。思想不應該錯過任何代碼。

如果你有學習的Django,請隨意評論的更好的辦法任何建議。

+0

您是否在已安裝的應用程序列表中添加了應用程序? – nu11p01n73R

+0

是的,我肯定有。 – Vato

回答

2

你有'DIRS': [BASE_DIR],但你的模板不BASE_DIR,他們在BASE_DIR /模板。您應該有:

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

它工作!非常感謝!你有學習Django的技巧嗎? – Vato