Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

315
Visualizações
How to display Form query results in a ListView

I've created a form where users will enter a date range. Once submitted it then needs to return a list from my database of people all those whose birthdays are within that date range. I'm fairly new to Django and I've tried but do not understand how to pass my "start_date & end_date" as kwargs from my form to ListView.

views.py:

class BirthdaysRangeFormView(FormView):
    template_name = 'snippets/date_selection_form.html'
    form_class = datefilterform

    def get_form_kwargs(self):
        kwargs = super(BirthdaysRangeFormView, self).get_form_kwargs()
        kwargs ['request'] = self.request 
        return kwargs

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['title'] = "Date Selection"
        context['header'] = "Please select date range"
        return context

    def form_valid(self, form):
        self.start_date = (form.cleaned_data['start_date'], People.next_birthday)
        self.end_date = (form.cleaned_data['end_date'], People.next_birthday)
        return self.redirect_to_results()

    def redirect_to_results(self):
        self.success_url = "dates:birthdays"
        return redirect(
            self.get_success_url(),
        )


class BirthdaysListView(BirthdaysRangeFormView, ListView):
    template_name = 'dates/birthdays.html',
    context_object_name = "person"
    person = True
    # queryset = People.objects.order_by('next_birthday')


    def get_queryset(self):
        test = ''

forms.py:

class datefilterform(forms.Form):
    model = People



    start_date = forms.DateField(
        input_formats=['%d/%m/%Y'],
        widget=DateTimePicker(
            options={"format": "DD/MM/YYYY", "pickTime": False})
    )

    end_date = forms.DateField(
        input_formats=['%d/%m/%Y'],
        widget=DateTimePicker(
            options={"format": "DD/MM/YYYY", "pickTime": False})
    )

    People.next_birthday = start_date
    People.next_birthday = end_date

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request')
        super(datefilterform, self).__init__(*args, **kwargs)

        self.helper = FormHelper()
        self.helper.form_id = 'main_form'
        self.helper.form_class ='form-horizontal'
        self.helper.label_class = 'col-lg-3'
        self.helper.field_class = 'col-lg-8'

        self.helper.layout = Layout(

            'start_date',
            'end_date',
            FormActions(
                Submit('submit', 'Submit'),
                Button('cancel', 'Cancel', css_class='cancel')
            )
        )

    def clean(self):

        cleaned_data = super().clean()
        start_date = cleaned_data.get("start_date")
        end_date = cleaned_data.get("end_date")

        if not check_end_date(start_date, end_date):
            raise forms.ValidationError(
                "Incorrect Dates entered, End date must be after start date"
            )

I've done simple ListViews before but I don't understand how to use "request" in class based views.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can simply access self.request in your class based views.

Rather you following format, so that you can handle both POST and GET request in the same view.

class MyView(FormView):
   template_name = 'some_template.html'
   success_url = '...'
   form_class = MyForm

   def get(self, request, *args, **kwargs):
      pass

   def post(self, request, *args, **kwargs):
      pass


   def form_invalid(self, form, **kwargs):
      pass


   def form_valid(self, form, **kwargs):
      pass
   .....
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda