Schock54
Schock54

Reputation: 21

in Django FormView I cant see the form

I have some issues where I can see the form nor the parameter this is my code Here I pass an ID from the previous selection as parameter

URL file

path('make_payment/<int:jugador_id>', views.Payment_ViewMix.as_view(), name = "make_payment"),

view fiel: here I try to catch the parameter and also get the current time and get the form, when I delete the get_context_data function, the form is shown correctly when I add it again nothing sows and the ID is never shown in the HTTP template

class Payment_ViewMix(FormView):
    form_class =  PagoJugadorForm
    template_name = "includes/make_payment.html"
    context_object_name = 'payment'

           
    def get_context_data(self, **kwargs):
        idjug = self.kwargs
        idjugador = int(idjug['jugador_id'])
        print(idjugador)
        context = {
            'idjugador':idjugador,
            }
        return context

    def form_valid(self, form):
        context = self.get_context_data(context)
        print(context)
        fecha_pago = datetime.datetime.now()
        print(fecha_pago)
        jugador_id = context['idjugador']
        fecha_pago = fecha_pago
        #jugador_id.instance = self.object
        monto = form.cleaned_data['monto']
        notas = form.cleaned_data['notas']
        id_categoriapago =  form.cleaned_data['id_categoriapago']
        id_group_categoria =form.cleaned_data['id_group_categoria']
        JugadorPagos.objects.create(
            idjugador = jugador_id, 
            fecha_pago = fecha_pago,
            monto = monto, 
            notas = notas,
            id_categoriapago = id_categoriapago,
            id_group_categoria = id_group_categoria, 
            )
        return super(Payment_ViewMix, self).form_valid(form)

form file : here in the form I do not show the ID because is the one that I want to get from the parameter given and the current date is the one that I would like to assign to "fecha_pago"

class PagoJugadorForm(forms.ModelForm):
    
    class Meta:
        model = JugadorPagos
        fields = (#'idjugador',
                  'monto',
                  #'fecha_pago',
                  'id_categoriapago',
                  'id_group_categoria',
                  'notas'
                  )
        
        widgets = {
            'notas': forms.TextInput(
                attrs = {
                    'placeholder': 'ingrese texto'})
            }
        

HTML fiel:

Here I just want to print the ID and the Form


{% block content %}


    <h3 class = "text-center mt-5"> Jugador ID: {{payment.idjugador}}</h3>
    <form  method="post">
        {% csrf_token %}
        <table class="table table-striped" header = "Datos del jugador">
            <tbody>
                <tr>
                    <th scope="row">Monto</th>
                        <td>{{form.monto}}</td>
                    </tr>
                    <tr>
                        <th scope="row">Notas</th>
                            <td>{{form.notas}}</td>
                    </tr>
                    <tr>
                        <th scope="row">id_categoriapago</th>
                            <td>{{form.id_categoriapago}}</td>
                    </tr>
                    <tr>
                        <th scope="row">id_group_categoria</th>
                        <td>{{form.id_group_categoria}}</td>
                    </tr>

            </tbody>
        </table>
        <button type="submit" >Realizar Pago </button>
    </form>

when I delete the def get_context_data the form is shown correctly I hope you can help me because I do not have any error.

Best regards

Upvotes: 2

Views: 34

Answers (1)

ruddra
ruddra

Reputation: 51988

You need to override the get_context_data(...) in way so that it will call its super class (the original get_context_data method)

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    idjug = self.kwargs
    idjugador = int(idjug['jugador_id'])
    
    context['idjugador'] = idjugador
        
    return context

Upvotes: 1

Related Questions