Django:如何避免使用元标记重定向到首页?

时间:2019-04-26 21:58:25

标签: django

搜索我发现,为了在用户看到确认页面后重定向用户,我可以将此meta tag放在html中:

 <meta http-equiv="refresh" content="20;url=www.mysite.com/email_confirmation_needed.html"/>

但是,有人告诉我这不是一个好习惯,应该避免。

  

如何将用户从www.mysite.com/email_confirmation_needed.html发送到   在{strong> N 秒后www.mysite.com

views.py:

注册后显示确认页面。

### Email Confirmation Needed ###

def email_confirmation_needed(request):
    return render(request, "accounts/email_confirmation_needed.html")

@transaction.atomic
def signupView(request):

    if request.method == 'POST':


        user_form = SignUpForm(request.POST, request.FILES)
        profile_form = ProfileForm(request.POST, request.FILES)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save(commit=False)
            user.is_active = False
            user.save()
            username = user_form.cleaned_data.get('username')
            signup_user = User.objects.get(username=username)
            customer_group = Group.objects.get(name='Clientes')
            customer_group.user_set.add(signup_user)
            raw_password = user_form.cleaned_data.get('password1')
            user.refresh_from_db()  # This will load the Profile created by the Signal

            profile_form = ProfileForm(request.POST, request.FILES,
                                       instance=user.profile)  # Reload the profile form with the profile instance
            profile_form.full_clean()  # Manually clean the form this time. It is implicitly called by "is_valid()" method


            profile_form.save()  # Gracefully save the form

            # user = authenticate(username=username, password=raw_password)
            # login(request, user) #Cannot login a NOT Active user

            current_site = get_current_site(request)
            mail_subject = 'Confirmación de correo electrónico'
            message = render_to_string('accounts/acc_active_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
                'token': account_activation_token.make_token(user),
            })
            # to_email = user_form.cleaned_data.get('email')
            to_email = 'oma.gonzales@gmail.com'
            from_email = 'stickersgallito@stickersgallito.pe'
            email = EmailMessage(
                mail_subject, message, to=[to_email], from_email=from_email
            )
            email.send()

            return redirect('shop:email_confirmation_needed')


        else:
            print(user_form.errors)
            print(profile_form.errors)

    else:

        user_form = SignUpForm()

        profile_form = ProfileForm()


    return render(request, 'accounts/signup.html', {
        'user_form': user_form,
        'profile_form': profile_form
    })

确认页面上的计时器:

确认页面中还有一个计时器,确切的秒数无关紧要。但理想情况下,重定向应该在计数器达到0时发生。

带有JS CountDown计数器的模板:

{% extends 'base.html' %}
{% load staticfiles %}
{% load embed_video_tags %}
{% block metadescription %}
    {% if category %}
        {{ category.description|truncatewords:155 }}
    {% else %}
        ¡Bienvenidos a Stickers Gallito Perú!
    {% endif %}
{% endblock %}
{% block meta_redirect %}
    <meta http-equiv="refresh" content="20;url=www.mysite.com"/>
{% endblock %}
{% block title %}
    <p>Stickers Gallito - Confirma tu correo electrónico</p>
{% endblock %}

{% block content %}

    <div class="container">
        <br>
        <br>
        <br>

        <div class="col-md-12">

            <h1> ¡Gracias por registrarte! </h1>

            <p>Acabamos de enviarte un correo electrónico con un enlace.</p>

            <p>Al darle click al enlace que enviamos validaremos tu registro y automáticamente ingresarás con tu
                usuario y clave</p>

            <h4>Será redirigido a la página de inicio en <span id="time">00:16</span> segundos.</h4>

            <p>Recuerde que deberá confirmar su correo electrónico con el enlace que le acabamos de enviar.</p>

            <style>
                #time {
                    color: red;
                }
            </style>


            <script>
                function startTimer(duration, display) {
                    var timer = duration, minutes, seconds;
                    setInterval(function () {
                        minutes = parseInt(timer / 60, 10)
                        seconds = parseInt(timer % 60, 10);

                        minutes = minutes < 10 ? "0" + minutes : minutes;
                        seconds = seconds < 10 ? "0" + seconds : seconds;

                        display.textContent = minutes + ":" + seconds;

                        if (--timer < 0) {
                            timer = duration;
                        }
                    }, 1000);
                }

                window.onload = function () {
                    var fiveMinutes = 12,
                        display = document.querySelector('#time');
                    startTimer(fiveMinutes, display);
                };

            </script>

        </div>

    </div>

{% endblock %}

0 个答案:

没有答案