Karega hela...

Django - Customize User Creation With Email Confirmation

Customize User Creation With Email Confirmation

Generally, we sign up in any website, it sends the confirmation link to activate the account. Or sometime it asks to email to change the email or reset the password of an account.

In this tutorial, we will learn how to send the confirmation mail using the Django when someone registers on our web app. I will explainining an easy way to build this. But Django provides many other options like django allauth application, django-registration, django-registration-redux. Those applications make this task very straightforward.

Before starting, make sure that you must knowledge of Django and Python programming language. Create the basic setup of Django and configure the settings.


Let's Get started

Make sure your django project is setup already (https://onydev.pykafe.org/tet/detail/kria-projetu-uza-django/)

Use git to clone this repository into your existing Django project

git clone https://github.com/marobo/user_profile.git


To include the app in your project, you need to add a reference to its configuration class in the INSTALLED_APPS section in   settings.py, 


Edit project settings your_project/settings.py file, and add the user_profile app to the INSTALLED_APPS setting. It’ll look like this:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'user_profile', # New app
]


To configure the email host server ll in the settings.py, for confirmation mail. Add the below configuration in the your_project/settings.py file.

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

It will send out the text confirmation mail via terminal on your running server, if the creation account form is valid.


Add a app url to the project/base configuration URL in your_project/urls.py 

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('user_profile.urls')), # New app urls
    path('accounts/', include('django.contrib.auth.urls')), # New or maybe you have added already
]


Make a link for the user to click and visit the Create new account form in somewhere place, (i.e. in index.html or in base_site.html)

<a href="{% url 'create_account' %}">Create New Account</a>


Let's run server and then try to Create a new account (example of create form address https://127.0.0.1/en/signup/ or Live Demo).

If the create form is valid, you'll see the feedback message Please check your email!.

Now go to the terminal server you running, you'll see email confirmation messages like below:

Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Welcome to localhost:8009
From: webmaster@localhost
To: emailyouresgistered@gmail.com
Date: Mon, 11 Mar 2024 05:21:28 -0000
Message-ID: <171013448854.74991.4945198941969335774@onorios-air.busa>

Hello newusername,
Please verify your email by clicking on the following link.
Link: http://localhost:8009/activate/Mw/c3pljs-4d8452e391dee5d266616a6bfa7ace8a

Many thanks
Dev Team
-------------------------------------------------------------------------------

Click on that confirmation link to login with your new account you just created.


Note

This is just for playing in locally. If you wanna like to send the confirmation email to the real email address, you need to change  console  to  smtp :

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

To:

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"

and add those email variables in your settings.py

EMAIL_USE_TLS = True  
EMAIL_HOST = 'smtp.gmail.com'  
EMAIL_HOST_USER = 'youremail@gmail.com'  
EMAIL_HOST_PASSWORD = 'yourpassword'  
EMAIL_PORT = 587


Please don't use your real email password in EMAIL_HOST_PASSWORD. Try generate it from gmail app password.


To Generate/create an app password in Gmail, you can do the following:

1. Log in to your Google account

2. Go to https://myaccount.google.com/apppasswords

3. Select Security

4. Under "Signing in to Google," select 2-Step Verification

5. At the bottom of the page, select App passwords

6. Select Other from the Select app drop-down

7. Name the app password

8. Click Generate

9. Copy the new app password to the EMAIL_HOST_PASSWORD = 'yournewpassword' in settings.py file

10. Click Don


Visit this more instruction https://support.google.com/mail/answer/185833?sjid=4090351568356337842-AP

0 Komentárius

Post ne'ebé ligadu