Output: str = ', '.join([q.question_text for q in latest_question_list])

вот вьюшка:

from django.shortcuts import render, get_object_or_404
from .models import Question, Choice
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.views import generic
from django.template import loader
from django.http import Http404


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    context = {
        'latest_question_list': latest_question_list,
    }
    return HttpResponse(template.render(context, request))
    output: str = ', '.join([q.question_text for q in latest_question_list])
    return HttpResponse(output)


class Default(dict):
    def __missing__(self, key):
        return key


class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        """Return the last five published questions."""
        return Question.objects.order_by('-pub_date')[:5]


def detail(request, pk):
    try:
        question = get_object_or_404(Question, pk=pk)
    except Question.DoesNotExist:
        raise Http404("Question does not exist")
#    question = get_object_or_404(Question, pk=pk)
# return HttpResponse("You're looking at question %s." % question_id)
    return render(request, 'polls/detail.html', {'question': question})


class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'


def results(request, pk):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % pk)


class ResultsView(generic.DetailView):
    model = Question
    template_name = 'polls/results.html'


def vote(request, question_id):
    # return HttpResponse("You're voting on question %s." % 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):
        # Redisplay the question voting form.
        return render(request, 'polls/detail.html', {
            'question': question,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

вот урлс:

from django.urls import path

from . import views

app_name = 'question' 'polls'
urlpatterns = [
    path('', views.index, name='index'),
    path('<int:pk>/', views.detail, name='detail'),
    path('<int:pk>/results/', views.results, name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),
]
# <int:question_id>/

вот шаблон index:

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

пай чарм говорит что невозможно воспроизвести строчку output: str = ', '.join([q.question_text for q in latest_question_list]) из вьюшки, мне уже говорили что это из-за того что я поставил его после вывода, но я не понимаю как это можно сделать как-то по-другому, буду очень признателен за помощь

Вернуться на верх