Thursday, September 1, 2011

Re: Need Help Regarding Forms.py

You can use ModelForms, it is the easiest way of creating the forms
for your template:
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/
That will generate the necessary form and you just need to put the
form templatetags in your template. An example for your code would be:

forms.py
-------------

from foo import Doctor
from django.forms import ModelForm

class DoctorForm(ModelForm):
class Meta:
model = Doctor


doctor_form.html
------------------------

{{ form.as_p }}

That will autogenerate the form distributed in paragraphs, with no
pain at all. Hope it helps.

Regards,
Oscar


El 01/09/2011 12:31, "Showket Bhat" <scorpion.schizo@gmail.com> escribió:
> Hi All....
>
> I am new to Django and I have created a small application for learning
> Django.. Well I have not used forms in my application.. Is it
> necessary to use forms in Django.. Can't we work without forms.. If
> yes then I want to create a form where in i ll be inserting field
> values for a class.. I have a selection field gender in Doctor Class
> which i have declared in Models.py and Have specialization as a
> foreign key... How would i show these fields as selection fields in my
> HTML form..
> Here I am pasting my code
>
> Please Help
>
> models.py
> =======
>
> class Doctor(models.Model):
> title = models.CharField(max_length=30)
> first_name = models.CharField(max_length=30)
> last_name = models.CharField(max_length=30)
> special_id = models.ForeignKey(Specialization)
> experience =models.IntegerField(max_length=120)
> gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
> active = models.BooleanField(default=False)
>
> def __unicode__(self):
> return self.name
>
>
> class Doctor_address(models.Model):
> street = models.CharField(max_length=30)
> street1 = models.CharField(max_length=50)
> city = models.CharField(max_length=60)
> state = models.CharField(max_length=30)
> country = models.CharField(max_length=50)
> zip = models.CharField(max_length=50)
> doctor = models.ForeignKey(Doctor)
> def __unicode__(self):
> return self.name
>
> views.py
> =======
> def doctor_form(request):
> return render_to_response('doctor_form.html')
>
> template.html
> ==========
>
> <table width="100%" border="0" cellpadding="3px" class="htmlForm"
> cellspacing="0">
> <tr>
> <td colspan="2">&nbsp;</td>
> </tr>
> <tr>
> <td align="left">Title</td>
> <td width="220px"><input type="text" size="32"
> name = "title"/></td>
> </tr>
> <tr>
> <td align="left">First Name</td>
> <td width="220px"><input type="text" size="32"
> name = "first_name"/></td>
> </tr>
> <tr>
> <td align="left">Last Name</td>
> <td width="220px"><input type="text" size="32"
> name = "last_name"/></td>
> </tr>
> <tr>
> <td align="left">Specialization</td>
> <td><input type="text" size="32" name =
> "special_id"/></td>
> </tr>
> <tr>
> <td align="left">Experience</td>
> <td><input type="text" size="32" name =
> "experience"/></td>
> </tr>
> <tr>
> <td align="left">Gender</td>
> <td><input type="text" size="32" name =
> "gender" id = "gender"/></td>
> </tr>
> <tr>
> <td align="left">Is Active</td>
> <td><input type= 'checkbox' name = "active"/></
> td>
> </tr>
> </table>
>
>
>
>
> --
> 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+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment