Wednesday, February 10, 2016

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

I've moved to CBV sometime ago  and  I can not write  your code on fly:

 Let imagine:
1) User request page using "GET"
2) User set some values and send it using  "POST"
3) New page is shown using  new "GET"

id_medioedit is set ant stage 2 and answer at 3 is ok. But you dont know how to set  it at stage 1 . In this case  you need default value  for first get  
Add  some code for "GET" such as
if request.method == "GET":
    p = Medious.object.get(id=1)

Many thanks,

Serge


+380 636150445
skype: skhohlov

On Wed, Feb 10, 2016 at 1:17 PM, elcaiaimar <sepas91@gmail.com> wrote:
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.

--
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/CADTRxJN4z4HnZ%2BKzpOGp8y7qtmMYeBGYFDsA4jOz7UbZJjX6og%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment