Когда пишу код очень сильно мозолит глаза данная ошибка. Пишу код в
Visual Studio Code.
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.views import generic
from django.urls import reverse
from models import Question, Answer, Category, Good
# Create your views here.
class IndexView(generic.ListView):
template_name = 'sleep/index.html'
def get_queryset(self):
return Question.objects.all() <i>// Вот здесь тоже</i>
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context.update({
'question_list': Question.objects.all(), <i>//Здесь эта же ошибка</i>
'category_list': Category.objects.all() <i>// И даже тут)</i>
})
return context
class DetailView(generic.DetailView):
model = Question
template_name = 'sleep/detail.html'
class ResultView(generic.DetailView):
model = Question
template_name = 'sleep/result.html'
class MembersView(generic.DetailView):
model = Category
template_name = 'sleep/members.html'
class GoodView(generic.DetailView):
model = Good
template_name = 'sleep/good.html'
def vote(request, a_id):
question = get_object_or_404(Question, pk=a_id)
try:
selected_answer = question.answer_set.get(pk=request.POST['q'])
except (KeyError, Answer.DoesNotExist): // Здесь говорит что у Answer нет DoesNotExist
return render(request, 'sleep/detail.html', {'question': question, 'error_message': 'Please, pick the answer.'})
else:
selected_answer.vote += 1
selected_answer.save()
return HttpResponseRedirect(reverse('result', args=(a_id,)))