views:

135

answers:

2

i started to learn django using the official documentation tutorial and in turn created this registration form. i have the whole app setup but am stuck at how to process the data. i mean the documentation is a little tough to understand. if i can get some help, it would be great.

this is how the models are setup:

class user (models.Model):
    username = models.CharField(max_length=20)
    name = models.CharField(max_length=25)
    collegename = models.CharField(max_length=50)
    event = models.CharField(max_length=30)
    col_roll = models.CharField(max_length=15)

    def __unicode__(self):
     return self.username

and this is the form in index.html:

<form action="" method="post"></form>
    <input name="username" type="text" maxlength="40" />
    <input name="name" type="text" maxlength="40" />
    <input name="collegename" type="text" maxlength="40" />
    <input name="event" type="text" maxlength="40" />
    <input name="col_roll" type="text" maxlength="40" />
    <input type="submit" value="Register" />
</form>

i do not follow how to create the view to process this registration of a new user. if anybody could help me, it would be great. The database (MySQL) is created with the name (register_user). I do not understand how to put the values from the above form in to the database. If it would have been regular python, it would have been easily done, but DJANGO i dont understand.

Thanks a lot.

+1  A: 

You don't seem to have read the documentation on forms. It explains in detail how to create a form from your model, how to output it in a template (so you don't need to write the HTML input elements manually), and how to process it in a view.

Daniel Roseman
A: 

It took me a bit of time to figure out the form processing bit when I started. The first thing you need before you get to the form part is to create a view function. The view will use your index.html file as a template. In that view you'll want to create a form (either from Daniel's link above or by creating a form from your model). Once you have a form class, provide an instantiated copy of that in the view (e.g., form = MyRegistrationModelForm()) and then replace all of your input tags in index.html with {{ form }}. That will output a pre-formatted form, which may not be exactly what you want, but you can then view source and see what the form fields need to be named and then you can customize from there.

One other thing. Not sure if you are, but you should be using django-registration for this. Rather than creating your own user class, use the built-in User class in django.contrib.auth.models and then use django-registration for the custom fields you need. You'll be able to plug in other modules much more easily if you use the defined User class.

As for the database, assuming you have put the proper MySQL connection information into settings.py, running 'python manage.py syncdb' in the root of your project should create all the necessary tables. Make sure to re-run it as you add new models.

Tom