Now that we have taken care of validation and two password field, the next question is how to make sure our information can be saved and show this by making our fields green when they can be saved and red if we have a conflict. To do this we will create a helper page that can be called using ajax to check for valid email and user. We want all our users to have a unique user name and email since these can be used for logging on DG3. The plan than is to call the server when username or email is changed and return valid or not valid for email and a username that is for the username. This is advanced coding and an error in this code may make your server not start if done wrong, so use and change with care.

The valid user page

I chose to add this page in the authentication folder with the rest of authentication related pages. Since this will be used by registration, no user login can be added. We will let the page take two paramters field and makeunique. The field parameter will take email og username as parameter and makeunique will make a new unique username by adding numbers to the suggested one if set to any true value (none blank or not 0 means true).

gdict = request.GET
gdict = viewtools.splitgdict(gdict)
field = gdict.get('field','')
value = gdict.get('value','')
makeunique=gdict.get('makeunique')
result = {'result' : 'error', 'field' : field, 'value' : value,'makeunique' : makeunique}
if field:
    from django.contrib.auth import get_user_model
    table=get_user_model()
    try:
        if field=='username':
            rec = table.objects.get(username=value)
        elif field=='email':
            rec = table.objects.get(email=value)
        if makeunique:
            idx = 0
            isunique = False
            while not isunique:
                try:
                    idx += 1
                    nval = "%s%d" % (value,idx)
                    if field=='username':
                        rec = table.objects.get(username=nval)
                    elif field=='email':
                        rec = table.objects.get(email=nval)
                    else:
                        break
                except:
                    isunique=True
                    result['result']='ok'
                    result['value']=nval                        
    except:        
        result['result']= 'ok'
return HttpResponse(json.dumps(result), content_type="application/json")

The custom code

Using the valid user page from registration

To use this page, we will borrow a method from our datasource library. Since we are manipulating fields, this will already be loaded and active to use by the javascript name datasource.