Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Jan. 16, 2011 14:37:39

Benchouia A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django forms


Hello there,

I am new to django developement model. If someone know
1- how to access to the ModelForm fields instance in the view?
2- I am trying to developpe a website that contains many forms, is
there a way to make the same template (new and update) for a given
model.Models class

thanks,

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 18, 2011 09:31:19

D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django forms


First read and understand:http://docs.djangoproject.com/en/dev/topics/forms/modelforms/Then:
1.http://stackoverflow.com/questions/1645444/django-modelform-accessing-a-fields-value-in-the-view-template2.http://neverfear.org/blog/view/123/Auto_Generate_Forms_With_Django_s_ModelFormOn Jan 16, 8:43 am, Benchouia Ahmed Abdelhakim <abhakim1...@gmail.com>
wrote:
> Hello there,
>
> I am new to django developement model.  If someone know
> 1- how to access to the ModelForm fields instance in the view?
> 2- I am trying to developpe a website that contains many forms,  is
> there a way to make the same template (new and update) for a given
> model.Models class
>
> thanks,

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 18, 2011 15:17:37

H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django forms


Thanks man, but I am still suffering from another thing,

look at this code:

@csrf_exempt
@login_required
def update_book(request):
dontvalidate = request.POST
book_id = int(request.POST)
book = get_object_or_404(Book, id = book_id)
if dontvalidate == 'yes':
bookform = BookForm(instance = book)
else:
booknoteform = BookForm(request.POST, request.Files)
if bookform.is_valid():
bookform.save()
return render_to_response(
'books/update_book.html',
{
'bookform':bookform,
'dontvalidate': dontvalidate,
'is_authenticated' : request.user.is_authenticated(),
},
context_instance = RequestContext(request)
)

Once the update code is printed on the screen, all old fields are
there except the one for the FileField.
I coundn't fill out the file data filed with old file saved when the
form was created.
The Book model is like

class Book(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length = 100)
author = models.CharField(max_length = 100)
publisher = models.CharField(max_length = 100)
publication_date = models.DateField()
front_page = models.FileField(upload_to='books')

def __unicode__(self):
return self.title

On Jan 18, 10:31 am, Derek <gamesb...@gmail.com> wrote:
> First read and
> understand:http://docs.djangoproject.com/en/dev/topics/forms/modelforms/>
> Then:
> 1.http://stackoverflow.com/questions/1645444/django-modelform-accessing...
> 2.http://neverfear.org/blog/view/123/Auto_Generate_Forms_With_Django_s_...
>
> On Jan 16, 8:43 am, Benchouia Ahmed Abdelhakim <abhakim1...@gmail.com>
> wrote:
>
> > Hello there,
>
> > I am new to django developement model.  If someone know
> > 1- how to access to the ModelForm fields instance in the view?
> > 2- I am trying to developpe a website that contains many forms,  is
> > there a way to make the same template (new and update) for a given
> > model.Models class
>
> > thanks,

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 18, 2011 15:22:37

H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django forms


Thanks man, the only problem I still have is the following

class Book(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length = 100)
author = models.CharField(max_length = 100)
publisher = models.CharField(max_length = 100)
publication_date = models.DateField()
front_page = models.FileField(upload_to='books')

def __unicode__(self):
return self.title

@csrf_exempt
@login_required
def update_book(request):
dontvalidate = request.POST
book_id = int(request.POST)
book = get_object_or_404(Book, id = book_id)
if dontvalidate == 'yes':
bookform = BookForm(instance = book)
else:
booknoteform = BookForm(request.POST, request.Files)
if bookform.is_valid():
bookform.save()
return render_to_response(
'books/update_book.html',
{
'bookform':bookform,
'dontvalidate': dontvalidate,
'is_authenticated' : request.user.is_authenticated(),
},
context_instance = RequestContext(request)
)

the problem is simply that I can return back the information of the
model to be updated except that of the front_page field.
I did add enctype in the form template but this also didn't work. is
there a way out?
On Jan 18, 10:31 am, Derek <gamesb...@gmail.com> wrote:
> First read and
> understand:http://docs.djangoproject.com/en/dev/topics/forms/modelforms/>
> Then:
> 1.http://stackoverflow.com/questions/1645444/django-modelform-accessing...
> 2.http://neverfear.org/blog/view/123/Auto_Generate_Forms_With_Django_s_...
>
> On Jan 16, 8:43 am, Benchouia Ahmed Abdelhakim <abhakim1...@gmail.com>
> wrote:
>
> > Hello there,
>
> > I am new to django developement model.  If someone know
> > 1- how to access to the ModelForm fields instance in the view?
> > 2- I am trying to developpe a website that contains many forms,  is
> > there a way to make the same template (new and update) for a given
> > model.Models class
>
> > thanks,

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 18th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support