2016-10-25 13 views
0

私は最初の単純なアプリケーションを作成しようとしましたが、私のところには問題があります。私はリンクhttp://127.0.0.1:8000/polls/を通過するとき、私は次のエラーメッセージがあります。クラスが指定されたクエリに一致しません

TemplateDoesNotExist at /polls/ 
/polls/index.html 
Request Method: GET 
Request URL: http://127.0.0.1:8000/polls/ 
Django Version: 1.10.2 
Exception Type: TemplateDoesNotExist 
Exception Value:  
/polls/index.html 
Exception Location: /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.2-py3.5.egg/django/template/loader.py in get_template, line 25 
Python Executable: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 
Python Version: 3.5.2 
Python Path:  
['/Users/alenasanina/djangoenv/bin/mysite', 
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.2-py3.5.egg', 
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip', 
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5', 
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', 
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload', 
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages'] 
Server time: Tue, 25 Oct 2016 21:48:49 +0300 
Template-loader postmortem 

Django tried loading these templates, in this order: 

Using engine django: 
This engine did not provide a list of tried templates. 
Traceback Switch to copy-and-paste view 

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.2-py3.5.egg/django/core/handlers/exception.py in inner 
      response = get_response(request) ... 
▶ Local vars 
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.2-py3.5.egg/django/core/handlers/base.py in _get_response 
       response = self.process_exception_by_middleware(e, request) ... 
▶ Local vars 
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.2-py3.5.egg/django/core/handlers/base.py in _get_response 
       response = wrapped_callback(request, *callback_args, **callback_kwargs) ... 
▶ Local vars 
/Users/alenasanina/djangoenv/bin/mysite/polls/views.py in index 
    return render(request, '/polls/index.html', context) ... 
▶ Local vars 
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.2-py3.5.egg/django/shortcuts.py in render 
    content = loader.render_to_string(template_name, context, request, using=using) ... 
▶ Local vars 
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.2-py3.5.egg/django/template/loader.py in render_to_string 
     template = get_template(template_name, using=using) ... 
▶ Local vars 
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.10.2-py3.5.egg/django/template/loader.py in get_template 
    raise TemplateDoesNotExist(template_name, chain=chain) ... 
▶ Local vars 

を次は私がリンクhttp://127.0.0.1:8000/polls/1を通過し、ここでは別のエラーです:

Page not found (404) 
Request Method: GET 
Request URL: http://127.0.0.1:8000/polls/1/ 
Raised by: polls.views.detail 
No Question matches the given query. 
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.`enter code here` 

私のmodels.py

import datetime 
from django.db import models 
from django.utils import timezone 

# Create your models here. 
class Question(models.Model): 
    question_text = models.CharField(max_length=200) 
    pub_date = models.DateTimeField('date_published') 
    def __str__(self): 
     return self.question_text 
    def was_published_recently(self): 
     return self.pub_date >= timezone.now() - datetime.timedelta(days=1) 

class Choice(models.Model): 
    question = models.ForeignKey(Question, on_delete=models.CASCADE) 
    choice_text = models.CharField(max_length=200) 
    votes = models.IntegerField(default=0) 
    def __str__(self): 
     return self.choice_text 

urls.py:

from django.conf.urls import url 

from . import views 

app_name='polls' 

urlpatterns = [ 
    url(r'^$', views.index, name='index'), 
    url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'), 
    url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'), 
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'), 
] 

views.py:

from django.shortcuts import get_object_or_404, render 
from django.http import HttpResponseRedirect, HttpResponse 
from django.core.urlresolvers import reverse 
from .models import Choice, Question 


# Create your views here. 
def index(request): 
    latest_question_list = Question.objects.order_by('-pub_date')[:5] 
    context = {'latest_question_list': latest_question_list} 
    return render(request, '/polls/index.html', context) 
def detail(request, question_id): 
    question = get_object_or_404(Question, pk=question_id) 
    return render(request, 'polls/detail.html', {'question': question}) 
def results(request, question_id): 
    question = get_object_or_404(Question, pk=question_id) 
    return render(request, 'polls/results.html', {'question': question}) 
def vote(reqeust, question_id): 
    question = get_object_or_404(Question, pk=question_id) 
    try: 
     selected_choice = question.choice_set.get(pk=request.POST['choice']) 
    except (KeyError, Choice.DoesNotExist): 
     return render (request, 'polls/detail.html', { 
      'question': question, 
      'error_message': "You didn't select a choice.", 
     }) 
    else: 
     selected_choice.votes +=1 
     selected_choice.save() 
     return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) 

settings.py:

INSTALLED_APPS = [ 
    'polls.apps.PollsConfig', 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
] 

MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
] 

ROOT_URLCONF = 'mysite.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [], 
     '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', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'mysite.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 


# Password validation 
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators 

AUTH_PASSWORD_VALIDATORS = [ 
    { 
     'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 
    }, 
] 


# Internationalization 
# https://docs.djangoproject.com/en/1.10/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'Europe/Kiev' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.10/howto/static-files/ 

STATIC_URL = '/static/' 

results.html:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
<h1>{{ question.question_text }}</h1> 

<ul> 
{% for choice in question.choice_set.all %} 
    <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li> 
{% endfor %} 
</ul> 

<a href="{% url 'polls:detail' question.id %}">Vote again?</a> 
</body> 
</html> 

detail.html:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
<h1>{{ question.question_text }}</h1> 

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} 

<form action="{% url 'polls:vote' question.id %}" method="post"> 
{% csrf_token %} 
{% for choice in question.choice_set.all %} 
    <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" /> 
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br /> 
{% endfor %} 
<input type="submit" value="Vote" /> 
</form> 
</body> 
</html> 

のindex.html。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
{% if latest_question_list %} 
    <ul> 
    {% for question in latest_question_list %} 
     <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li> 
    {% endfor %} 
    </ul> 
{% else %} 
    <p>No polls are available.</p> 
{% endif %} 
</body> 
</html> 

みんな、助けてください。私はそれが解決するのは非常に簡単かもしれないが、私はPythonで新しくなったことを知っている。

バージョン: ジャンゴ1.10.2 のPython 3.5

+0

これはデータの問題のように見えますが、アプリケーションのバグではありません。あなたのデータベースをチェックし、id 1の投票があるかどうかを確認してください。 – karthikr

+0

ええ、それはidの問題でした!それは1の代わりに2でした! – CopsLikeDonuts

答えて

0

、テンプレートファイル'/polls/index.html'へのパスは、おそらく間違っています。あなたは最初の/を削除しなければならないかもしれません。

関連する問題