📜 ⬆️ ⬇️

We fasten django-registration

It was necessary to fasten the registration to the site. I remembered about django-registration (classic django reusable app). The first thing that surprised me was that there were no templates in the source. I had to dig a little on the Internet, look for answers to questions and share the result.

In general, I downloaded django-registration , read the docks and google, began to connect:

# setting.py
ACCOUNT_ACTIVATION_DAYS = 2 # -

#
AUTH_USER_EMAIL_UNIQUE = True
EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
DEFAULT_FROM_EMAIL = 'info@google.ru'

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'registration', # reusable app
)

# urls.py
urlpatterns = patterns('',
(r'^accounts/', include('registration.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)


We start the python server ./manage.py runserver 8004 , open http: // localhost: 8004 / accounts / register / and get the error TemplateDoesNotExist at / accounts / register / - there are no templates. So create templates in registration / templates.
')
# templates/registration/registration_form.html
{% extends "base.html" %}
{% block content %}
<h1></h1>
<form method="post" action="">
<dl class="register">
{% for field in form %}
<dt>{{ field.label_tag }}</dt>
<dd class="clearfix">{{ field }}
{% if field.help_text %}<div class="clearfix">{{ field.help_text }}</div>{% endif %}
{% if field.errors %}<div class="myerrors clearfix">{{ field.errors }}</div>{% endif %}
</dd>
{% endfor %}
</dl>
<input type="submit" value="" / class="clearfix">
</form>
{% endblock %}


The result was a registration form. To the name of the fields sounded in Russian, you must not forget to register in setting.py

LANGUAGE_CODE = 'ru-RU' #
USE_I18N = True # -


And to complete the registration, you need to synchronize the python database ./manage.py syncdb (the registration_registrationprofile table is created). It is also worth noting that by default django-registration does not check the new user's e-mail for uniqueness. To fix this, in the registration / views.py file you need to change:

"""
# RegistrationForm
# RegistrationFormUniqueEmail
def register(request, success_url=None,
#form_class=RegistrationForm,
form_class=RegistrationFormUniqueEmail,
profile_callback=None,
template_name='registration/registration_form.html',
extra_context=None):


But, as correctly corrected in the comments of lizendir - to edit the library code is bad. It is better to add url () to urls.py before connecting registration.urls of this type:
url(r'^register/$', 'registration.views.register', {'form': RegistrationFormUniqueEmail}, name='registration_register'),
url('', include('registration.urls')),


For minimally sufficient functioning it is necessary to create in templates / registration / some more files:

# -
# activation_email_subject.txt
– {{ site }}

#
# activation_email.txt

:
{{ site }}/accounts/activate/{{ activation_key }}/
!

#
# registration_complete.html
{% extends "base.html" %}
{% block content %}
<h1> </h1>
. e-mail
. ,
.<br/><br/>
{% endblock %}

#
# activate.html
{% extends "base.html" %}
{% block content %}
<h1></h1>
, {{ account }}!<br/>
. <a href="{% url auth_login %}"></a> .
<br/><br/>
{% endblock %}


But in order to register successfully, you need to make a test mail-server, where the letter with the text and the activation code will be sent. Fortunately, in Python there is a quick solution - just type on the command line:

python -m smtpd -n -c DebuggingServer localhost:1025

We leave the terminal open, start the django server in an additional terminal window, go to http: // localhost: 8004 / accounts / register / and register. The debugger will display the text of the letter in quoted-printable encoding. But we just need to copy and paste a line like example.com/accounts/activate/b3842d8f0b08a548a0372de9e79b6bd909bf8e6e/ and add to our localhost: 8004. It turns out: http: // localhost: 8004 / accounts / activate / b3842d8f0b08a548a0372de9e79b6bd909bf8e6e / . Go and activate your account. Registration is complete. Hooray!

For authorization we need 2 more templates - login.html and logout.html:

# templates/registration/login.html
{% extends "base.html" %}
{% block content %}
<h1></h1>

{% if user.is_authenticated %}
, {{ user.username }}? .
, <a href="{% url auth_logout %}"></a>.<br/><br/>
{% else %}

{% if form.non_field_errors %}
{{ form.non_field_errors }}
{% endif %}

<form method="post" action="">
<dl class="register">
{% for field in form %}
<dt>{{ field.label_tag }}</dt>
<dd class="clearfix">{{ field }}
{% if field.help_text %}<div class="clearfix">{{ field.help_text }}</div>{% endif %}
{% if field.errors %}<div class="myerrors clearfix">{{ field.errors }}</div>{% endif %}
</dd>
{% endfor %}
</dl>
<input type="submit" value="" / class="clearfix">
</form>

<script type="text/javascript">
document.getElementById('id_username').focus()
</script>

<br/><br/>
<ul>
<li><a href="{% url auth_password_reset %}"> ?</a></li>
<li><a href="{% url registration_register %}"></a></li>
</ul>
{% endif %}
{% endblock %}


# templates/registration/logout.html
{% extends "base.html" %}
{% block content %}
<h1></h1>
, . .<br/><br/>
{% endblock %}


Of course, this is not all - in registration / urls.py there are URLs for sending a forgotten password and reset. But for the minimum functionality is enough. The only thing that - django-admin does not show by default, the user is activated or not. Let's fix it. Let's create a new application django-admin.py startapp customuseradmin , register it in INSTALLED_APPS after django.contrib.admin and in customuseradmin / admin.py we write something like:

# -*- coding:utf-8 -*-
from django.contrib import admin
from django.contrib.auth.models import User, Group
from django.contrib.auth.admin import UserAdmin

admin.site.unregister(User)

class CustomUserAdmin(UserAdmin):
list_display = ('username', 'email', 'is_staff','is_active',)
list_filter = ('is_staff', 'is_superuser', 'is_active',)
admin.site.register(User, CustomUserAdmin)


That's it. If where wrong - correct, please.

Source: https://habr.com/ru/post/74165/


All Articles