TemplateTesNotExist at / polls / 1 /

时间:2017-08-21 14:17:20

标签: python django

我收到了一个错误, 在/ polls / 1 /上的TemplateDoesNotExist templates / polls / detail.html。我想展示detail.html。

追溯是

Traceback:

File "/Users/XXX/django/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/Users/XXX/django/django/core/handlers/base.py" in _get_response
  130.                 response = self.process_exception_by_middleware(e, request)

File "/Users/XXX/django/django/core/handlers/base.py" in _get_response
  128.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/XXX/djangostudy/polls/views.py" in detail
  43.         'question': obj,

File "/Users/XXX/django/django/shortcuts.py" in render
  36.     content = loader.render_to_string(template_name, context, request, using=using)

File "/Users/XXX/django/django/template/loader.py" in render_to_string
  61.         template = get_template(template_name, using=using)

File "/Users/XXX/django/django/template/loader.py" in get_template
  19.     raise TemplateDoesNotExist(template_name, chain=chain)

Exception Type: TemplateDoesNotExist at /polls/1/
Exception Value: templates/polls/detail.html

我的forms.py就像

from django import forms

class MyForm(forms.Form):
    text = forms.CharField(max_length=100,required=False,label='テキスト')

class VoteForm(forms.Form):
    choice = forms.ModelChoiceField(
        queryset=None,
        label='選択',
        widget=forms.RadioSelect,
        empty_label=None,
        error_messages={
            'required':"You didn't select a choice.",
            'invalid_choice':"invalid choice.",
        },
    )

    def __init__(self,question,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.fields['choice'].queryset = question.choice_set.all()

单选按钮的设置写在detail.html中,如

<!DOCTYPE html>
<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'poll_vote' question.id %}" method="post">
<!--<form action="" 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 %}
    <!--{{ form }}-->
    <input type="submit" value="Vote" />
</form>
</html>

views.py是

from django.shortcuts import render
from django.utils.html import mark_safe
from .models import Question
from django.http import HttpResponse
from django.shortcuts import Http404
from django.shortcuts import get_object_or_404,redirect
from .models import Choice
from django.views.generic import TemplateView
from django.views.generic import DetailView
from django.views.generic import ListView
from .forms import MyForm
from .forms import VoteForm


# Create your views here.
def index(request):
    return render(request,'polls/index.html',{
        'questions': Question.objects.all(),
    })

def detail(request,pk):
    obj = get_object_or_404(Question,pk=pk)
    if request.method == "POST":
        form = VoteForm(question=obj,data=request.POST)
        if form.is_valid():
            return redirect('polls:results',pk)
    else:
        form = VoteForm(question=obj)
    return render(request,'templates/polls/detail.html',{
        'form':form,
        'question': obj,
    })

def vote(request,pk):
    pass

def results(request,pk):
    obj = get_object_or_404(Question,pk=pk)
    return render(request,'polls/results.html',{
        'question':obj,
    })

def form_test(request):
    if request.method == "POST":
        #request.POST???
        form = MyForm(data=request.POST)
        if form.is_valid():
            pass
    else:
        form = MyForm()
    return render(request,'polls/form.html',{
        'form':form,
    })

我认为加载模板的地方,即detail.html

  return render(request,'templates/polls/detail.html',{
        'form':form,
        'question': obj,
    })

,所以我无法理解为什么这里没有显示detail.html。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

你告诉Django使用templates/polls/detail.html。这意味着Django会寻找polls/templates/templates/polls/detail.html,这可能不是你想要的。

您还没有说出模板的位置,但我在polls/templates/polls/detail.html假设它。在这种情况下,您应该将视图更改为:

return render(request,'polls/detail.html', {
    'form':form,
    'question': obj,
})