Wednesday, February 10, 2016

Send an object parameter from views.py to forms.py

Hello,

I have a problem because I don't know how could I send one specific parameter. I've sent other parameters sometimes in others apps but this time is more difficult.
Below it's my code. I want to send 'p', which is an object. The problem is that I can't send it in else because I've defined p outside (if). So when I tried to send it, p is not defined. I can't define it outside because I need id_medioedit, which is an id obtained from template in medioedit_id form.

Anyone who knows how to solve this?

Thank you in advance


Views.py 

if 'medioedit_id' in request.POST:
        formularioeditarmedios
= EditMediosForm(request.POST)
       
       
try:
            id_medioedit
= request.POST['medioedit_id']
            p
= Medios.objects.get(pk=id_medioedit)
            mensaje
= {"status":"True","medioedit_id":p.id}


           
if formularioeditarmedios.is_valid():
               
...

else:
        formularioeditarmedios
= EditMediosForm(p=p)


Forms.py

class EditMediosForm(forms.Form):
                         
#initial='somevalue'
  titulo
= forms.CharField(max_length=140, widget=forms.TextInput(attrs={'style': 'width:140px'}))

def __init__(self,p,*args,**kwargs):
    p
= kwargs.pop("p", None)
   
super(EditMediosForm, self).__init__(*args,**kwargs)
   
self.fields['titulo'].initial = p.titulo


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e4c77318-156a-40da-9761-9c03f382af77%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment