Django: How to use AJAX to check if username exists on registration form?
<p>When the user enters a username, I want to check the db if that username already exists and display an error message. How would I do that without refreshing the page? With AJAX?</p>

<p>registration_form.html</p>

<pre><code>&lt;div class="container-fluid userformcontainer"&gt;
  &lt;div class="row"&gt;
      &lt;div class="col-md-offset-2 col-sm-offset-1 col-sm-8 userformdiv"&gt;
          &lt;h1 class="title userformtitle"&gt;Sign up&lt;/h1&gt;
          &lt;form method="POST" class="post-form"&gt;
              {% csrf_token %}
              {% bootstrap_form form %}

              {% buttons %}
              &lt;button type="submit" class="save btn btn-default btn-lg userformbutton center-block"&gt;Register&lt;/button&gt;
              {% endbuttons %}
          &lt;/form&gt;
      &lt;/div&gt;&lt;!--col--&gt;
    &lt;/div&gt;&lt;!--row--&gt;
&lt;/div&gt;&lt;!--container--&gt;
</code></pre>

<p>views.py</p>

<pre><code>class UserFormView(View):
  form_class = SignUpForm
  template_name = 'card/registration_form.html'

  def get(self, request):
      form = self.form_class(None)
      return render(request, self.template_name, {'form': form})

  def post(self, request):
      form = self.form_class(request.POST)

      if form.is_valid():
          user = form.save(commit=False)

          username = form.cleaned_data['username']
          user.first_name = form.cleaned_data['first_name']
          user.last_name = form.cleaned_data['last_name']

          if not User.objects.filter(username=username).exists():
              password = form.cleaned_data['password']
              user.set_password(password)
              user.save()

              user = authenticate(username=username, password=password)

              if user is not None:

                  if user.is_active:
                      login(request, user)
                      return redirect('card:deck_list')
              else:
                  pass
          else:
              pass
</code></pre>

<p>forms.py</p>

<pre><code>class SignUpForm(forms.ModelForm):
  password = forms.CharField(widget=forms.PasswordInput)

  class Meta:
      model = User 
      fields = ['username', 'first_name', 'last_name', 'password']
</code></pre>

<p>You can build your form manually (at least the username field) as per django documentation below:</p>

<p><a href="https://docs.djangoproject.com/en/1.10/topics/forms/#rendering-fields-manually" rel="nofollow">https://docs.djangoproject.com/en/1.10/topics/forms/#rendering-fields-manually</a></p>

<p>Then you can set "onchange" event for your field to call a javascript fuction that does the AJAX work for you and display the success/failure message in a div tag of your own.</p>

<p>Alternatively, you may want to let a javascript code that runs on page load to attach an "onchange" event to the "username" field based on its name to the same job for you.</p>

python ajax django