title: django-registration, registration and login form on index page together, did I do it right?
body: <p>I'd appreciate some code review, I used django-registration app and django.contrib.auth module. What I wanted to do is have both the login and registration form on the index page, and manage it from there. What I did is I just copied code from registration.views.py and contrib.auth.views.py and banged it together.</p>
  
  <p>It works but I feel it's very hack-ish, non elegant, and that there is another, proper way to do it. For example I feel it might be better to call or extend view methods in registration and auth instead of copy pasting them.</p>
  
  <pre><code>def index(request, success_url=None,
               form_class=RegistrationForm,
               authentication_form=AuthenticationForm,
               profile_callback=None,
               template_name='index.html',
               extra_context=None, **kwargs):
  
      redirect_to = request.REQUEST.get('next', '')
  
      if request.method == 'POST':
          form = form_class(data=request.POST, files=request.FILES)
          form_auth = authentication_form(data=request.POST)
  
          if form.is_valid():
              new_user = form.save(profile_callback=profile_callback)
              # success_url needs to be dynamically generated here; setting a
              # a default value using reverse() will cause circular-import
              # problems with the default URLConf for this application, which
              # imports this file.
              return HttpResponseRedirect(success_url or reverse('registration_complete'))
  
          if form_auth.is_valid():
              netloc = urlparse.urlparse(redirect_to)[1]
  
              # Use default setting if redirect_to is empty
              if not redirect_to:
                  #redirect_to = settings.LOGIN_REDIRECT_URL
                  redirect_to = "/"
  
              # Security check -- don't allow redirection to a different
              # host.
              elif netloc and netloc != request.get_host():
                  #redirect_to = settings.LOGIN_REDIRECT_URL
                  redirect_to = "/"
  
              # Okay, security checks complete. Log the user in.
              auth_login(request, form_auth.get_user())
  
              if request.session.test_cookie_worked():
                  request.session.delete_test_cookie()
  
              return HttpResponseRedirect(redirect_to)
  
      else:
          form = form_class()
          form_auth = authentication_form()
  
  
      if extra_context is None:
          extra_context = {}
      context = RequestContext(request)
      for key, value in extra_context.items():
          context[key] = callable(value) and value() or value
      return render_to_response(template_name,
                                { 'form': form, 'form_auth': form_auth},
                                context_instance=context) 
  </code></pre>
  
  <p>And forms in the index.html:</p>
  
  <pre><code>    {% if form.errors %}
          &lt;p class="errors"&gt;Please correct the errors below: {{ form.non_field_errors }}&lt;/p&gt;
      {% endif %}
  
      &lt;h3&gt;Create an account&lt;/h3&gt;
  
      &lt;form method="post" action="" class="wide"&gt;
      {% csrf_token %}
          &lt;p&gt;
            &lt;label for="id_username"&gt;Your Username:&lt;/label&gt;
            {% if form.username.errors %}
              &lt;p class="errors"&gt;{{ form.username.errors.as_text }}&lt;/p&gt;
            {% endif %}
            {{ form.username }}
          &lt;/p&gt;
          &lt;p&gt;
            &lt;label for="id_email"&gt;Email address:&lt;/label&gt;
            {% if form.email.errors %}
              &lt;p class="errors"&gt;{{ form.email.errors.as_text }}&lt;/p&gt;
            {% endif %}
            {{ form.email }}
          &lt;/p&gt;
          &lt;p&gt;
            &lt;label for="id_password1"&gt;Password:&lt;/label&gt;
            {% if form.password1.errors %}
              &lt;p class="errors"&gt;{{ form.password1.errors.as_text }}&lt;/p&gt;
            {% endif %}
            {{ form.password1 }}
          &lt;/p&gt;
          &lt;p&gt;
            &lt;label for="id_password2"&gt;Password (type again to catch typos):&lt;/label&gt;
            {% if form.password2.errors %}
              &lt;p class="errors"&gt;{{ form.password2.errors.as_text }}&lt;/p&gt;
            {% endif %}
            {{ form.password2 }}
          &lt;/p&gt;
          &lt;p class="submit"&gt;&lt;input type="submit" value="Register"&gt;&lt;/p&gt;
      &lt;/form&gt;
  
      {% if form_auth.errors %}
      &lt;p class="error"&gt;Please correct the errors below:&lt;/p&gt;
      {% endif %}
  
  
      &lt;h3&gt;Log in&lt;/h3&gt;
  
      &lt;form method="post" action="?next={{ next|default:"/" }}"&gt;
      {% csrf_token %}
      &lt;dl&gt;
      &lt;dt&gt;&lt;label for="id_username"&gt;Username:&lt;/label&gt;{% if form.username.errors %} &lt;span class="error"&gt;{{ form.username.errors|join:", " }}&lt;/span&gt;{% endif %}&lt;/dt&gt;
      &lt;dd&gt;{{ form_auth.username }}&lt;/dd&gt;
      &lt;dt&gt;&lt;label for="id_password"&gt;Password:&lt;/label&gt;{% if form.password.errors %} &lt;span class="error"&gt;{{ form.password.errors|join:", " }}&lt;/span&gt;{% endif %}&lt;/dt&gt;
      &lt;dd&gt;{{ form_auth.password }}&lt;/dd&gt;
      &lt;dt&gt;&lt;input type="submit" value="Log in" /&gt;&lt;/dt&gt;
      &lt;/dl&gt;
      &lt;/form&gt;
  </code></pre>