Showing Django Registration/Signup form at home page


#1

I wanted to show the user registration form in my project to the home page because I thought it is redundant to send user to another link of /accounts/register because I want to encourage them to signup right from home page. I spent almost 2 full days trying different things and finally found the solution which is so simple and easy. The trick is using Django’s class based views. Here is my solution and I hope it can help other people. By the way I’m using module registration-redux in the app.

views.py:

from registration.backends.default.views import RegistrationView

class IndexView(RegistrationView):
	template_name = "myapp/index.html"

urls.py:

url(r'^$', views.IndexView.as_view(), name="home"),

index.html

<form method="post" action="." class="col-xs-4">
  {% csrf_token %}
  {{ form.as_p }}
  <input type="submit" value="Submit" />
</form>

Note: If you are using Django’s built-in user registration process, then simply change the views.py to following:

views.py

from django.contrib.auth.forms import UserCreationForm
from django.views.generic import CreateView


    class IndexView(CreateView):
        form_class = UserCreationForm
        template_name = "myapp/index.html"

Thanks and please let me know if someone has a question.


#2

Very very cool! Thanks so much — I updated your subject line a bit to help Google find this as I think a lot of people would be Googling for this kind of solution. :)