> First of all, I want to say that I do not understand the relationship
> between forms and models in Django. A form to ask and a model to
> store? How to pass the form data to a model? Have I to pass it in
> order to validate?

There is not necessarily a relationship between the two. You are
correct - forms are for gathering user input and models are for
storing application data.  Django does provide a ModelForm which is
meant to provide a convenience of generating a Form based on a Model.
However, try not to get too hung up on this.  In practice it can be
rare that our web forms match exactly with a particular Model
definition.  Rather it is often the case that a particular web form
will collect fields that map to fields on multiple models.  In this
case you want to build a custom Form containing exactly the fields you
need to collect.  The act of doing this is meant to save you time in
the generation of the actual HTML and in writing validation code.  Now
that you have your form, a typical use pattern is:

1. In your view, on a GET request you'll instantiate the form without
any data and pass if off to the render context.
2. On a POST request (form submit), you'll instantiate the form with
the POST data.
3. Call is_valid() on your form to determine if the data validates
4. If the form data is valid, you will then instantiate or lookup your
required model(s), update the model fields with the appropriate
cleaned_data fields from the form, and save() your models.
5. If the form did not validate, you will again pass it to the render
context and this time it will contain the user's submitted values as
well as errors.

>
> The problem with this code is that when I do not enter the name field
> it says the form is invalid but the name field is optional. And the
> second problem is that when I enter invalid data I don't see the
> errors in each field.

Are you referring to "name" on your registration form?  You would need
to add a "required=False" attribute in order for that to be the case.
However, the biggest problem with your code is that you are not using
your Registration form to validate the data.  Your template is
rendering the Registration form, however you have never populated it
with data or called is_valid() on it.


Also, form_for_model is being deprecated - use ModelForms if you
choose to go that route.

Hope that helps
-Brian
>
> This is the template used:
>
> {% block title %}Registration form{% endblock %}
> {% block content %}
> <h3>{{ message }}</h3>
>
> <form action="." method="post" >
>         <table>
>                 <tr>
>                         <td>Username:</td>
>                         <td>{{ uForm.username }}</td>
>                         <td>{{ uForm.username.errors }}</td>
>                 </tr>
>                 <tr>
>                         <td>Password:</td>
>                         <td>{{ uForm.password }}</td>
>                         <td>{{ uForm.password.errors }}</td>
>                 </tr>
>                 <tr>
>                         <td>Name:</td>
>                         <td>{{ uForm.name }}</td>
>                         <td>{{ uForm.name.errors }}</td>
>                 </tr>
>                 <tr>
>                         <td>Email:</td>
>                         <td>{{ uForm.email }}</td>
>                         <td>{{ uForm.email.errors }}</td>
>                 </tr>
>         </table>
>         <input type="submit" name="submit" value="Register" />
> </form>
> {% endblock %}
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to