9

Can someone please explain in details how to make a registration and authentication in as easy words as possible ? I made authentication (login) with django.contrib.auth but what I want to get is a full register(social/non)+login. Already saw the django-allauth, django-social-auth, django-social but still can't get it working without hacking a lot. Heard that django-registration and django-profiles can make it a lot easier, but i can't handle it. For example,

~/.virtualenvs/plinter/lib/python2.7/site-packages/registration/backends/default/urls.py

needs a small hack to work:

# from django.views.generic.simple import direct_to_template
from django.views.generic import RedirectView
...
                           RedirectView.as_view(url='/registration/activation_complete.html'),
                           # direct_to_template,
                           # {'template': 'registration/activation_complete.html'},
...

The DjangoBook gives simple examples of Contact and search forms. But i can't expand it on user registration and login. So can anyone give kis example of working registration and login?

Update

Here is a simple example of login. Now django-allauth or social auth or registration2 are in consideration...

Update2

django-allauth seems to be the best solution for easier authentication. Add correctly apps in settings, register fb/google/etc apps and register through admin and use template inheritance to change default pages design.

8
  • 1
    Why did you have to hack django-registration? Are you using django 1.5+? If so, don't install registration from pipy, it's not updated for compatibility. Install from the bitbucket repo instead .
    – Adrián
    Jun 3, 2013 at 18:10
  • Added hg+https://bitbucket.org/ubernostrum/django-registration/downloads/django-registration-0.8.tar.gz to requirements.txt, will try now.
    – boldnik
    Jun 5, 2013 at 8:43
  • no, that's the same version that is in pipy. Get the trunk instead of the tag: hg+https://bitbucket.org/ubernostrum/django-registration
    – Adrián
    Jun 5, 2013 at 9:57
  • $ pip freeze | grep django-registration django-registration==0.8 - the same version which demands simple module. And it is weird because others also use this app with django-1.5...
    – boldnik
    Jun 5, 2013 at 17:51
  • 1
    The version you need is (0, 9, 0, 'beta', 1). If you don't have that your installation is not compatible with 1.5
    – Adrián
    Jun 5, 2013 at 17:54

2 Answers 2

13

THIS is a very good tutorial about login & Co. It explains very well how to perform login by ourself ad override existing django login pages.

UPDATE:

Here Overview for Registration and Login. For more details go to the link.

To Register:

Views and URLs

Go to the lower site folder (where the settings.py file is) and open the views.py file. At the top make sure the following imports are included. Add them if not:

from django.shortcuts import
 render_to_response from django.http import HttpResponseRedirect from
 django.contrib.auth.forms import UserCreationForm from
 django.core.context_processors import csrf

Below that add the following functions (you can put them after the Login functions):

 def
 register(request):
     if request.method == 'POST':
         form = UserCreationForm(request.POST)
         if form.is_valid():
             form.save()
             return HttpResponseRedirect('/accounts/register/complete')

     else:
         form = UserCreationForm()
     token = {}
     token.update(csrf(request))
     token['form'] = form

     return render_to_response('registration/registration_form.html', token)

 def registration_complete(request):
     return render_to_response('registration/registration_complete.html')

Open the urls.py file in the site folder (same folder as settings.py). Below urlpatterns = patterns('', insert the following lines.

     # Registration URLs
     url(r'^accounts/register/$', 'simplesite.views.register', name='register'),
     url(r'^accounts/register/complete/$', 'simplesite.views.registration_complete',
 name='registration_complete'),

Templates We will assume your site already has a templates directory and a base.html file with the navigation bar. Open the base.html file and in the nav element add a navigation menu link to the login page

<a href="/accounts/register">register</a>

If one does not already exist, go to the templates folder and create a folder inside it named registration. Create a file called registration_form.html, save it to the templates/registration folder, then populate it with the following:

{% extends "base.html" %} {% block title %}Register{%
 endblock %} {% block content %}

   <h2>Registration</h2>

   <form action="/accounts/register/" method="post">{% csrf_token %}
     {{form.as_p}}   <input type="submit" value="Register" />

   </form>

 {% endblock %}

Create a file called registration_complete.html, save it to the templates/registration folder, and populate it with the following:

{% extends "base.html" %} {% block title %}You are
 Registered{% endblock %} {% block content %}

   <h2>Thank you for Registering</h2>   <p><a
 href="/accounts/login/">Please Login</a></p>

 {% endblock %}

To Login:

Views and URLs Open the views.py file in the lower site folder (where the settings.py file is). If there isn't one then create and save it. At the top of the file insert the following import: from django.shortcuts import render_to_response Below that you only need to add one function rendering the loggedin page. The other functions (login and logout) are in the views.py file in the Django Auth folder.

def loggedin(request):
    return render_to_response('registration/loggedin.html')

# Optionally, if you want to show their username when they login then call their username in the view. Change the loggedin function to:

def loggedin(request):
    return render_to_response('registration/loggedin.html',
                              {'username': request.user.username})

Open the urls.py file in the site folder (same folder as settings.py). Below urlpatterns = patterns('', insert the following lines.

# Auth-related URLs:
url(r'^accounts/login/$', 'django.contrib.auth.views.login', name='login'),
url(r'^accounts/logout/$', 'django.contrib.auth.views.logout', name='logout'),
url(r'^accounts/loggedin/$', 'simplesite.views.loggedin', name='loggedin'),

With simplesite being the name of the folder that holds the views.py file that you are calling. Open the settings.py file and at the bottom insert LOGIN_REDIRECT_URL = '/accounts/loggedin/'. Django's default is to redirect to /accounts/profile when you log in, which is fine if you have an profile page at that url. If not you need to change your settings default for the Login redirect url to the one holding your loggedin.html page.

Templates

We will assume your site already has a templates directory and a base.html file with the navigation bar. Open the base.html file and in the nav element add a navigation menu link to the login page <a href="/accounts/login">login</a> Add a logout link too <a href="/accounts/logout">logout</a> Create a directory called registration inside the templates folder. If you do this through the command line, type mkdir registration Create a file called login.html, save it to the templates/registration folder, and populate it with the following:

{% extends "base.html" %}
{% block title %}Log In{% endblock %}
{% block content %}

<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<table>
  {{ form.as_table }}
</table>

<input type="submit" value="login" />
</form>

{% endblock %}

{{ form.as_table }} uses the Django Forms module to create the form. You can create an unformatted form by using {{ form }} without the HTML table tags, or have each field put inside paragraph tags with {{ form.as_p }}, or as an unordered list {{ form.as_ul }}. Optionally, you can also lay out your own form structure and use the form field tags as follows:

{% extends "base.html" %}
{% block title %}Log In{% endblock %}
{% block content %}

<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}

{% if form.errors %}
<p>Your Username or Password were not entered correctly. Please try again.</p>
{% endif %}

<table>
<tr>
    <td>{{ form.username.label_tag }}</td>
    <td>{{ form.username }}</td>
    <td>{{ form.username.errors }}</td> 
</tr>
<tr>
    <td>{{ form.password.label_tag }}</td>
    <td>{{ form.password }}</td>
    <td>{{ form.password.errors }}</td>
</tr>
</table>

<input type="submit" value="login" />
</form>

{% endblock %}

Create a file called loggedin.html, save it to the templates/registration folder, and populate it with the following:

{% extends "base.html" %}
{% block title %}Logged In{% endblock %}
{% block content %}

  <h2>You are logged in</h2>

{% endblock %}

If you want to display the username, you would make the adjustment to the view discussed in the views section. Then change the loggedin.html template to the below (change the wording as you see fit):

{% extends "base.html" %}
{% block title %}Logged In{% endblock %}
{% block content %}

<h1>Welcome {{username}}</h1>
<p>Thank you for logging in.</p>
<p><a href="/accounts/logout/">Logout</a></p>

{% endblock %}

Create a file called logged_out.html, save it to the templates/registration folder and populate it with the following:

{% extends "base.html" %}
{% block title %}Logged Out{% endblock %}
{% block content %}

  <h2>Logged out!</h2>
  <p><a href="/accounts/login/">Log back in</a></p>

{% endblock %}
6
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
    – ljk321
    May 6, 2015 at 8:47
  • It is not a simple thing to report the code if you look at the tutorial! I try to put some essential code...
    – Trix
    May 6, 2015 at 8:50
  • @skyline75489: I hope that now the answer is acceptable, so please could you remove your downvote?
    – Trix
    May 6, 2015 at 9:14
  • Actually I didn't downvote. I just came through this answer while doing reviewing.
    – ljk321
    May 6, 2015 at 9:15
  • 1
    Ok, I hope that they are correct and revise their downvote because that link is really very useful and it solved my days when I studied authentication.
    – Trix
    May 6, 2015 at 9:18
0

Trix's approach worked for me, but the logout link was redirecting to the admin logout, instead of logged_out.html.

To fix the redirect, I added a next option to the href:

<a href="{% url 'logout' %}?next={% url 'loggedout' %}">Logout</a>

In templates/registration:

Renamed logged_out.html to loggedout.html; the underscore caused it to still route to the admin logout page.

In views.py:

def loggedout(request):
    return render_to_response('registration/loggedout.html')

And finally, in urls.py:

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.