D
D
David Winter2020-12-18 10:16:12
Django
David Winter, 2020-12-18 10:16:12

How to fix Django error VSCode: Class 'Question' has no objects 'member'?

When I write code, this error is very annoying. I write code in 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,)))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-12-18
@bacon

Take it and google your mistake, the solution is described there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question