Pass view parameters to Django form creator

Asked

Viewed 326 times

0

I have the following application where it is supposed to be possible to edit the details of a selected room. In the file that handles the view I query the database of the selected room for editing and I want to pass these details to the form builder to be put as placeholders. The solution I currently have is as follows::

view py.

def editar_espaco(request):
    if(request.method=="POST"):
        r=request.POST['espaco_edit']
        for regist in sala.objects.all():
            if regist.designacao_sala == r:
                form = editForm(piso=regist.piso,lotacao=regist.lotacao,laboratorio=regist.laboratorio,auditorio=regist.auditorio,estado=regist.status)
                return render(request,'editar_espaco.html',{'form':form,'nome_sala':r} )
            return render(request,'espaco.html')

Forms.py

from django import forms

TIPOS_SALA=(('1','Laboratório'),('2','Auditório'),('3','Normal'))

class editForm(forms.Form):
    def __init__(self, *args, **kwargs):
        self._piso = kwargs.pop('piso', None)
        self._lotacao = kwargs.pop('lotacao', None)
        self._laboratorio = kwargs.pop('auditorio', None)
        self._estado = kwargs.pop('estado', None)
        super(editForm,self).__init__(*args, **kwargs)

    piso = forms.IntegerField(required=True,label='Piso',widget=forms.NumberInput(attrs={'placeholder':self._piso}))
    lotacao = forms.IntegerField(required=True,label='Lotação',widget=forms.NumberInput(attrs={'placeholder':self._lotacao}))

    if (self._laboratorio==True):
        tipo_sala = forms.ChoiceField(required=True,label='Tipo de Sala:',choices=TIPOS_SALA,widget=forms.ChoiceInput(attrs={'placeholder':choices(1)}))
    elif(self._auditorio==True):
        tipo_sala = forms.ChoiceField(required=True,label='Tipo de Sala:',choices=TIPOS_SALA,widget=forms.ChoiceInput(attrs={'placeholder':choices(2)}))
    else:
        tipo_sala = forms.ChoiceField(required=True,label='Tipo de Sala:',choices=TIPOS_SALA,widget=forms.ChoiceInput(attrs={'placeholder':choices(3)}))

    estado = forms.ChoiceField(required=True,label='Estado',choices(('0','Indisponivel'),('1','Disponivel')),widget=forms.ChoiceInput(attrs={'placeholder':'Disponivel'}))

The method is supposed to __init__ receive and save arguments on all lines I use "self" to get the argument he points out that "self" is not set. I would appreciate any kind of help.Thank you

1 answer

1


The solution is this same, yes it is a lot of text. However in the abstraction scenario allows greater flexibility.

In the case of Modelform you can also use the meta class:

from django import forms
from .models import MyModel

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        widgets = {
            'name': forms.TextInput(attrs={'placeholder': 'Name'}),
            'description': forms.Textarea(
                attrs={'placeholder': 'Enter description here'}),
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.