1

I want to display to simple search Forms in a view.

forms.py:

from django import forms
from django.utils.translation import gettext_lazy as _

class Vehicle_Search_by_VIN(forms.Form):
    vin = models.CharField(max_length=17)
    first_registration_date = models.DateField()

class Vehicle_Search_by_Plate(forms.Form):
    plate = models.CharField(max_length=7)
    last_four_diggits_of_vin = models.DateField(max_length=4)

views.py:

from django.shortcuts import render
from django.views import View
from .forms import *


class VehicleSearch(View):
    template = 'vehicle_search_template.html'
    cxt = {
        'Search_by_VIN': Vehicle_Search_by_VIN(),
        'Search_by_Plate': Vehicle_Search_by_Plate()
    }
    def get(self, request):
        return render(request, self.template, self.cxt)

my template-file:

<form class="by_vin" method="POST" action="">
        {% csrf_token %}
        {{ Search_by_VIN.as_p }}
    
        <button name='action' value='login' type="submit">Suchen</button>
</form>
    
    <form class="by_plate" method="POST" action="">
        {% csrf_token %}
        {{ Search_by_Plate.as_p }}
    
        <button name='action' value='signup' type="submit">Suchen</button>
    </form>

But as a result only the submit buttons are displayed in the view. Does anybody know why my forms aren't being rendered?

aejsi5
  • 147
  • 8
  • Does this answer your question? [Django: Can class-based views accept two forms at a time?](https://stackoverflow.com/questions/15497693/django-can-class-based-views-accept-two-forms-at-a-time) – cizario Jul 04 '20 at 17:41

2 Answers2

0

in views.py, i think, you are missing *args and **kwargs parameters in get() function

class VehicleSearch(View):

    template = 'vehicle_search_template.html'

    cxt = {
        'Search_by_VIN': Vehicle_Search_by_VIN(),
        'Search_by_Plate': Vehicle_Search_by_Plate()
    }

    def get(self, request, *args, **kwargs):  # HERE
        return render(request, self.template, self.cxt)

Update

By default, class-based views does only support a single form per view but you can counter this limitation with few options depending on your logic. refer to this thread Django: Can class-based views accept two forms at a time?

cizario
  • 3,995
  • 3
  • 13
  • 27
0

try to give full path in your template variable for e.g. if your app name is my_app then template = 'my app/vehicle_search_template.html'

Neeraj
  • 783
  • 5
  • 8