0

Could you review my code for improvement? Essentially, it works but it isn't user friendly. For example,(1) If the user puts in an invalid field, it doesn't tell them what field is invalid. (2) If a user tries to register (sign up) with an existing username, there is no way the user can distinguishes that error, specifically. Or if a user mis-type password confirm, the user won't know that mistake either. (3) Also, if the user produces an input error(s), the sign up page blanks out all the field and so the user has to re-enter in all the info. As you can imagine, that could be very frustrating for that user.

I tried to see if form.error_messages might help the user, the error_messages is useless.

How would you improve the code so that (1), (2), (3) is not a nuisance for the user? And one more thing, just curious about how you would change the css? I noticed that most professional website sign-up page highlight the input box that was invalid. I am clueless on how to apply changes to css for {{form|as_bootstrap}}

Many thanks for your help!

form.py:

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm


class MyRegistrationForm(UserCreationForm):
    email = forms.EmailField(required=True,help_text="Email field is required.")
    firstname = forms.CharField(max_length=50, required=False, help_text="Optional. Can fill in later.")
    lastname = forms.CharField(max_length=50, required=False, help_text="Optional. Can fill in later.")

    class Meta:
        model = User
        fields = (
                  'username',
                  'email',
                  'password1',
                  'password2',
                  'firstname',
                  'lastname'
        )

        def save(self, commit=True):
            user = super(UserCreationForm, self).save(commit=False)
            user.email = self.cleaned_data["email"]
            user.firstname = self.cleaned_data["firstname"]
            user.lastname = self.cleaned_data["lastname"]
            if commit:
                user.save()
            return user

views.py

from django.shortcuts import render, render_to_response
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.core.context_processors import csrf
from forms import MyRegistrationForm


def register_user(request):
    if request.method == 'POST':
        form = MyRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/account/register_success')
        else:
            form = MyRegistrationForm()

        args = {}
        args.update(csrf(request))
        args['form']= form
        print form.errors #this line doesn't print anything
        return render_to_response('register.html', args)
    else:
        return HttpResponseRedirect('/')

def register_success(request):
    return render_to_response('register_success.html')

register.html

{% extends 'base.html' %}
{% load bootstrap_toolkit %}
{
{% block title %}
    <title>Register</title>
{% endblock %}
<body>
{% block content %}

    <div class="col-sm-9 col-sm-offset-3 col-md-6 col-md-offset-2 main">
        <h2>Register</h2>
        {% if form.error_messages %}
            <p class="error"> {{ form.error_messages }}.</p>
        {% endif %}

        <form class="form-signup" role="form"  action="/account/register/" method="post"> {% csrf_token %}

        {{ form|as_bootstrap }}
            <input type="submit" value="Register" class="btn btn-primary"/>
        </form>
    </div>
{% endblock %}
</body>
vt2424253
  • 1,387
  • 4
  • 25
  • 39
  • I think removing `form = MyRegistrationForm()` should solve #3 – arocks Jun 17 '14 at 06:26
  • Thanks! Your suggestion works and I also see that it shows error message below the input box (ie, when I type "username w/ space" in username box). Do you know if there is a way to have bootstrap return a message in red? – vt2424253 Jun 17 '14 at 06:35
  • I think it is CSS class `alert-danger` or `alert-error`. Use `django-crispy-forms` to solve such issues and to reduce lines of template code :) – arocks Jun 17 '14 at 06:38
  • I actually installed `django-crispy-forms` and tried to follow the instruction at [git](https://gist.github.com/maraujop/1838193) and [crispy-form](https://django-crispy-forms.readthedocs.org/en/latest/install.html#template-packs). I had spent a good part of the day debugging errors after errors and got stuck on CRISPY_TEMPLATE_PACK error from **settings.py** file, when I added `{% crispy form %}` to the template. Sadly, I gave up and removed crispy-form. – vt2424253 Jun 17 '14 at 07:11

0 Answers0