Django Tutorial: please help rewrite form on classes?

12 views
Skip to first unread message

Alexey

unread,
Mar 18, 2021, 2:10:45 PM3/18/21
to django...@googlegroups.com

I can't rewrite the mysite/polls form into classes.help me. I get
different errors every time.


<!-- detail.html -->
<form action="" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Vote">
</form>


# forms.py:
from django import forms
from .models import Choice

class VoteForm(forms.ModelForm):
    choices = [(ch.pk, ch.choice_text) for ch in Choice.objects.filter(pk=pk)]
    choices = tuple(choices)
    choice_text = forms.ChoiceField(widget=forms.RadioSelect(choices=choices))
    class Meta:
        model = Choice
        fields = ['choice_text']


# views.py#
class DetailView(generic.FormView, generic.DetaildView):
    model = Question
    template_name = 'polls/detail.html'
    form_class = VoteForm
    def get_queryset(self):
        """
        Excludes any questions that aren't published yet.
        """
        return Question.objects.filter(pub_date__lte=timezone.now())

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        #context["pk"] = self.pk
        #context["form"] = VoteForm()
        return context

    #def get(self, request, *args, **kwargs):
        #form = VoteForm(pk=kwargs['pk'])
        #return super().get(self, request, *args, **kwargs)


How do you bind it all together? How get question_id (pk) in form, how
mix with detaild view, how correct check validation this form?

I'm confused. Help please. Is it possible?

Reply all
Reply to author
Forward
0 new messages