Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8996057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:42:09+00:00 2026-06-15T23:42:09+00:00

In my django app I have a form where I ask users to enter

  • 0

In my django app I have a form where I ask users to enter an email adress and a username.

I would like to be able to check if the username or the email adress already exist an raise an error they do. But I would like to do this without reloading my page, so using javascript or Jquery.

My first idea would be to be something like this (for username):

html:

   <form  method="post" onsubmit="return validateForm();">
    {% csrf_token %}

        <div id='error_message'></div>
        <input class='username' type="text" name="username" value=""/>

    <button type="submit">Valider</button>

  </form>

In my views.py:

  def myview(request):
      users = User.objects.all()
      return render_to_response('mytemplate.html', 'users':users, context_instance=RequestContext(request))

Then in my template:

    <div class='search'>
   {% for user in users %}
    <input type='hidden' value='{{user.username}}'
   {% endfor %}
    </div>

And then, in the js:

   function validateForm() { 
   var value = $('.username').val()

   if( // find 'value' in all the <input type='hidden'>  ) { 
    var error = 'Username already exists';
    $("#error_message").html(error);
    return false;
  } 
 };

I think this is quite complex. Is there a simpler way to accomplish that?

Thank for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:42:10+00:00Added an answer on June 15, 2026 at 11:42 pm

    Sorry to say but your approach is very bad in terms of security and efficiency. You are disclosing all the usernames of your users (no matter if hidden input). You should check some already built authentication apps e.g django-usernea django-allauth

    I would go with ajax validation:

    First give your form an id e.g. my_form

    <script>
        $('#my_form').submit(function(){
          var username = $('#username').val();
          if (username == ''){
             alert('please enter username');
             return false;
          }
    
          $.ajax({
                   type: "POST",
                   url: "{% url auth_validate %}",
                   data: {'username': $('#username').val(), 'csrfmiddlewaretoken': '{{csrf_token}}'},
                   dataType: "text",
                   success: function(response) {
                          var response = $.parseJSON( response );
                          if (response.success){
                              return true;
                          }
                          else{
                              alert(response.error);
                          }
                    },
                    error: function(rs, e) {
                           alert(rs.responseText);
                    }
              }); 
        })
    </script>
    

    In urls.py add auth_validate url:

    url(r'^auth_validate/$', 'myapp.views.auth_validate', name='auth_validate'),
    

    Now in myapp.views:

    from django.http import HttpResponse
    from django.utils import simplejson
    from django.utils.translation import ugettext_lazy as _
    
    def auth_validate(request):
        error = ''
        success = False
        if request.method == 'POST':
            username = request.POST.get('username', None)
            if not username:
                error = _('Please enter username')
            elif User.objects.filter(username__exact=username).exists():
                error = _('Sorry this username is already taken')
            else:
                success = True
    
        ajax_vars = {'success': success, 'error': error}
        return HttpResponse(simplejson.dumps(ajax_vars),
                        mimetype='application/javascript')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form in my django app where users can upload files. How
I have a form in my Django app (not in admin) that allows staff
I have some troublesome form in my django app. Problem is that it doesn't
I am having problem in django. I have created a form in my app
I have a django app where user can submit a form .I have put
I have a model in my dJango app called event . I want users
Trying my first app in Django, and have some problem on form validation (following
I've installed this django-contact-form app. When I want to send an email to my
I've installed a django reusable app (Django-Userena) and would like to overwrite the given
I would like to build a Django app that allows searching for a movie

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.