Hello,
I have a model Investor associated with a model User. Now I'm
implementing the registration form.
I want to store the name of the Investor in the Investor model and the
rest of information in the User model. So I have username, password,
name and email.
I have a registration form in order to input this information:

class RegisterForm(forms.Form):
        username = forms.CharField(max_length=30)
        password = forms.CharField(max_length=20, widget=forms.PasswordInput())
        name = forms.CharField(max_length=50)
        email = forms.EmailField(max_length=30)

Now I have a view to show this form, retrive the data, validate it and
store it in Investor and in User:

def register_user(request):
        register_form = RegisterForm()
        message = "User Registration"
        
        
        if request.POST.get('submit') == 'Register':
                postDict = request.POST.copy()
                UserForm = forms.form_for_model(User)
                user_form = UserForm(postDict)
                                        
                if user_form.is_valid():
                        try:
                                user_form.save()
                                message = "User registered succesfully"
                        except:
                                message = "Database error"
                
                        invDict = {}
                        invDict['name'] = postDict.get('name', '')
                        invDict['userID'] = user_form.id
                        InvestorForm = forms.form_for_model(Investor)
                        iForm = InvestorForm(invDict)
                
                        if iForm.is_valid():
                                try:
                                        i = iForm.save()
                                        message = "User registered succesfully"
                                        return 
render_to_response('Authentication/login.html', {
                                                'lForm': LoginForm(),
                                                'message': message })
                                except:
                                        message = 'Database Error'
                                        user.delete()
                        else:
                                message = 'You entered invalid data in the form'
                                user.delete()
                else:
                        message = "You entered invalid data in the form"
        
        
        return render_to_response('Authentication/register.html',
                {'uForm': register_form, 'message': message})

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?

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.

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