Pada artikel sebelumnya kita sudah membahas tentang :
buat sebuah form registrasi dengan nama register.html, perhatikan kode berikut :
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, Helvetica, sans-serif;
background-color: black;
}* {
box-sizing: border-box;
}/* Add padding to containers */
.container {
padding: 16px;
background-color: white;
}/* Full-width input fields */
input[type=text], input[type=password] {
width: 100%;
padding: 15px;
margin: 5px 0 22px 0;
display: inline-block;
border: none;
background: #f1f1f1;
}input[type=text]:focus, input[type=password]:focus {
background-color: #ddd;
outline: none;
}/* Overwrite default styles of hr */
hr {
border: 1px solid #f1f1f1;
margin-bottom: 25px;
}/* Set a style for the submit button */
.registerbtn {
background-color: #4CAF50;
color: white;
padding: 16px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
opacity: 0.9;
}.registerbtn:hover {
opacity: 1;
}/* Add a blue text color to links */
a {
color: dodgerblue;
}/* Set a grey background color and center the text of the "sign in" section */
.signin {
background-color: #f1f1f1;
text-align: center;
}
</style>
</head>
<body><form action="{% url 'register_views' %}" method="POST">
<div class="container">
<h1>Register</h1>
<p>Please fill in this form to create an account.</p>
<hr><label for="email"><b>Email</b></label>
<input type="text" placeholder="Enter Email" name="email" required><label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required><label for="psw-repeat"><b>Repeat Password</b></label>
<input type="password" placeholder="Repeat Password" name="psw-repeat" required>
<hr>
<p>By creating an account you agree to our <a href="#">Terms & Privacy</a>.</p><button type="submit" class="registerbtn">Register</button>
</div>
<div class="container signin">
<p>Already have an account? <a href="#">Sign in</a>.</p>
</div>
</form></body>
</html>
buat sebuah class, kita beri nama RegisterView. perhatikan kode di bawah ini
from django.shortcuts import render
from django.views.generic import TemplateView
from django.contrib import messages# Create your views here.
class RegisterView(TemplateView):
template_name = 'register.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return contextdef post(self,request,*args, **kwargs):
context = self.get_context_data()
email = request.POST.get('email')
psw = request.POST.get('psw')
psw_repeat = request.POST.get('psw-repeat')
if psw == psw_repeat:
messages.error(request, {'alert':'register success'})
else:
messages.error(request, {'alert':'register failed'})
context['alert'] = True
return super(TemplateView, self).render_to_response(context)
Task
diatas kita telah membuat form register, selanjutnya kita akan membuat task ashinc untuk melakukan proses pengiriman email
buat file dengan nama task.py dan function dengan nama send_email, perhatikan kode dibawah ini :
from celery.utils.log import get_task_logger
from celery import shared_tasklogger = get_task_logger(__name__)
@shared_task
def send_email():
logger.info("email berhasil di kirim")
panggil function send email, perhatikan kode di bawah ini
if psw == psw_repeat:
send_email.delay()
messages.error(request, {'alert':'register success'})
else:
messages.error(request, {'alert':'register failed'})
pada kode diatas kita telah mengintegrasikan celery dan django @share_task, akan megeksekusi function send_email menjadi Asynchronous. sebelum menguji apakah aplikasi kita berhasil atau tidak jalankan celery
celery -A firstproject worker -l info
pada sesi akhir tutorial Task Scheduler Celery Redis akan membahas tentang Task scheduler dengan django celery beat