example

models.py

1
2
3
4
5
6
7
#email confirmation
class email_conf(models.Model):
    username = models.CharField(max_length=300, unique=True)
    slug = models.SlugField(max_length=300)
    time = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.username

forms.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from django import forms

#jon_us form
class join_us(forms.Form):
    username = forms.CharField(max_length=300)
    email = forms.EmailField()
    password=forms.CharField(widget=forms.PasswordInput())
    confirm_password=forms.CharField(widget=forms.PasswordInput())

    def clean(self):
        cleaned_data = super(join_us, self).clean()
        password = cleaned_data.get("password")
        confirm_password = cleaned_data.get("confirm_password")

        if password != confirm_password:
            raise forms.ValidationError(
                "password and confirm_password does not match"
            )

views.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#join_us
def join_us_v(request):
    #form
    join_us_form = join_us()
    if request.method == "POST":
        join_us_form = join_us(request.POST)
        if join_us_form.is_valid():
            username = join_us_form.cleaned_data['username']
            password = join_us_form.cleaned_data['password']
            email = join_us_form.cleaned_data['email']
            if username and password:
                #create new user
                User.objects.create_user(username=username, password=password, email=email)
                #create Confirmation Email
                random_str = get_random_string(length=50)
                email_conf.objects.create(username=username, slug=random_str)
                return render(request, 'join_us.html', {'valid_link':random_str})

                #
    return render(request, 'join_us.html', {'form':join_us_form})




from django.views.generic import DetailView
# validition link
class url_validition(DetailView):
    model = email_conf
    template_name  = 'confirm-account.html'
    slug_field = 'slug'
    def dispatch(self, request, *args, **kwargs):
        self._object = super(url_validition, self).get_object()
        #active user
        User.objects.filter(username=self._object.username).update(is_staff=True)
        #activate msg
        return HttpResponse('<h1>your account has been activated successfully</h1>')
        return super().dispatch(request, *args, **kwargs)

urls.py

1
2
3
4
    # join us
    path('join-us/', join_us_v, name="join_us"),
    #validition
    path('confirm/<slug:slug>/', url_validition.as_view(), name='url_validition_n')

join_us.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<html>
    <head>
        <title>How to make a pagination with django and jquery</title>
        <script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>
    </head>
    <body>

<!-- register form -->        
<form method="POST" action="{% url 'join_us' %}">{% csrf_token %}
{{form.as_p}}
<input type="submit" name="submit">
</form>


{% if valid_link %}
<h2>you have registered successfully</h2>
<p>click link below to activate your account</p>
<a href="{% url 'url_validition_n' valid_link %}">validition link</a>
{% endif %}
    </body></html>

Django confirmation page

English today is not an art to be mastered it's just a tool to use to get a result