Registration and registration in Django - explained by example

Can someone please explain in detail how to make registration and authentication in as simple words as possible? I did authentication (login) using django.contrib.auth , but I want to get a full registration (social / non) + login. Already seen django-allauth , django-social-auth , django-social , but still can not get it to work without hacking. I heard that django-registration and django-profiles can make this a lot easier, but I can't handle it. For example,

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

need a little 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'}, ... 

DjangoBook provides simple examples of contact and search forms . But I can not expand it at user registration and login. So can anyone provide an example and login ?

Update

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

Update2

django-allauth seems to be the best solution to simplify authentication. Add the correct applications to the settings, register the fb / google / etc applications and register via admin and use template inheritance to change the default page design.
+8
authentication django login forms registration
source share
2 answers

THIS is a very good tutorial about entering the system and Co. He explains very well how to log in through our ad, overriding existing django login pages.

UPDATE:

Here is an overview for registering and logging in. For more information, click here.

To register:

Views and URLs

Go to the bottom folder of the site (where the settings.py file is located) and open the views.py file. At the top, make sure the following import is 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 

The following functions are added below (you can put them after the Login Function):

  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 (the same folder as settings.py). Below urlpatterns = pattern ('', insert the following line.

  # 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 that your site already has templates and a base.html file with a navigation bar. Open base.html, and in the nav element add a navigation menu link for the login page

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

If it does not already exist, go to the templates folder and create a folder inside it called registration. Create a file called register_form.html, save it in the templates / registration folder, then fill it as follows:

 {% 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 in the templates / registration folder and fill it as follows:

 {% 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 log in:

Views and URLs Open the views.py file in the bottom folder of the site (where the settings.py file is located). If not, create and save it. At the top of the file, paste the following import: from django.shortcuts import render_to_response import Below you only need to add one function that displays the loggedin page. Other functions (input and output) are located in the views.py file in the Django Auth folder.

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

# Optional, if you want to show your username at login, call its 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 (the 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 the name simplesite, is the name of the folder that contains the view.py file that you call. Open the settings.py file and insert LOGIN_REDIRECT_URL = '/accounts/loggedin/' from the bottom. Django default is a redirect to / accounts / profile when you log in, which is great if you have a profile page at that URL. If not, you need to change the default settings for the redirect URL to login to the one that holds your loggedin.html.

Patterns

We will assume that your site already has a template directory and base.html with a navigation bar. Open the base.html file and add a navigation menu link to the login page in the nav element <a href="/accounts/login">login</a> Add the exit link too <a href="/accounts/logout">logout</a> Create a directory named registration in the templates folder. If you do this via the command line, enter mkdir registration Create a login.html file, save it in the templates / registration folder and fill it in 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 using {{ form }} without HTML tags, or each field is placed inside paragraph tags using {{ form.as_p }} or as an unordered list {{ form.as_ul }} . Perhaps 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 in the templates / registration folder and fill it as follows:

 {% 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 must configure the view described in the "Views" section. Then change the loggedin.html template below (change the wording as you like):

 {% 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 logged_out.html file, save it in the templates / registration folder and fill it in as follows:

 {% 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 %} 
+14
source share

The Trix approach worked for me, but the exit link was redirected to the administrator exit, instead of logged_out.html.

To fix the redirection, I added the following parameter to href:

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

In templates / registration:

Renamed logged_out.html to loggedout.html ; the underline made him still go to the admin exit 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'), 
0
source share

All Articles