简体   繁体   English

如何纠正 django 中的帐户激活错误

[英]How to rectify account activation error in django

I'm trying to click on an email activation link so as to activate a user account after submitting a registration form but I keep getting this error: The connection for this site is not secure; 127.0.0.1 sent an invalid response.我正在尝试单击 email 激活链接,以便在提交注册表后激活用户帐户,但我不断收到此错误: The connection for this site is not secure; 127.0.0.1 sent an invalid response. The connection for this site is not secure; 127.0.0.1 sent an invalid response. . . I ought to be redirected to the dashboard.我应该被重定向到仪表板。 Account is successfully created since the new user appears in the database but clicking on the activation link sent to the email throws back an error.由于新用户出现在数据库中,因此帐户已成功创建,但单击发送到 email 的激活链接会返回错误。 I'm following a tutorial though, but I can't figure out why the problem occurs.我正在关注一个教程,但我无法弄清楚为什么会出现问题。 url.py url.py

urlpatterns = [
    path('activate/<slug:uidb64>/<slug:token>)/', views.account_activate, name='activate'),
    path('dashboard/', views.dashboard, name='dashboard')
]

templates模板

account-activation_email.html:

{% autoescape off %}
Great {{ user.user_name }}!
Please click on the link below to activate your account
https://{{ domain }}{% url 'account:activate' uidb64=uid token=token %}
{% endautoescape %}

register.html

<form class="account-form p-4 rounded col-lg-10 mx-auto" method="post">
                                {% csrf_token %}
                                <h3 class="mb-2 font-weight-bold">Create an account</h3>
                                <p class="mb-4">Sign Up</p>
                                <label>{{ form.user_name.label }}<span class="text-muted small">
                                        (Required)</span></label>
                                {{ form.user_name }}
                                <label>{{ form.email.label}}<span class="text-muted small">
                                        (Required)</span></label>
                                {{ form.email }}
                                <label>{{ form.company.label}}<span class="text-muted small">
                                        (Required)</span></label>
                                {{ form.company }}
                                <label>{{ form.license_number.label}}<span class="text-muted small">
                                        (Required)</span></label>
                                {{ form.license_number }}
                                <label>{{ form.state.label}}<span class="text-muted small">
                                        (Required)</span></label>
                                {{ form.state }}
                                <label>{{ form.city.label}}<span class="text-muted small">
                                        (Required)</span></label>
                                {{ form.city }}
                                <label>{{ form.address.label}}<span class="text-muted small">
                                        (Required)</span></label>
                                {{ form.address }}
                                <label>{{ form.postcode.label}}<span class="text-muted small">
                                        (Required)</span></label>
                                {{ form.postcode }}
                                <label>{{ form.phone_number.label}}<span class="text-muted small">
                                        (Required)</span></label>
                                {{ form.phone_number }}
                                <label>{{ form.password.label}}<span class="text-muted mb-4">
                                        </span></label>
                                 <small class="form-text text-muted mb-4 small">
                                    At least 8 characters and 1 digit
                                </small>
                                {{ form.password }}
                                <label>{{ form.password2.label}}<span class="text-muted small">
                                        (Required)</span></label>
                                {{ form.password2 }}
                                <button class="btn btn-primary btn-block py-2 mb-4 mt-5 fw500 w-100" type="submit">Register</button>
                                <p class="text-center">
                                    <a>Already have an account?</a>
                                </p>
                            </form>

views.py视图.py

def account_register(request):
 #   if request.user.is_authenticated:
 #       return redirect('account:dashboard')

    if request.method == 'POST':
        registerForm = RegistrationForm(request.POST)
        if registerForm.is_valid():
            user = registerForm.save(commit=False)
            user.email = registerForm.cleaned_data['email']
            user.set_password(registerForm.cleaned_data['password'])
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            subject = 'Activate your Account'
            message = render_to_string('account/registration/account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
            })
            user.email_user(subject=subject, message=message)
            return HttpResponse('registered succesfully and activation sent')
    else:
        registerForm = RegistrationForm()
    return render(request, 'account/registration/register.html', {'form': registerForm})


def account_activate(request, uidb64, token):
    try:
        uid = force_text(urlsafe_base64_decode(uidb64))
        user = UserBase.objects.get(pk=uid)
    except(TypeError, ValueError, OverflowError, user.DoesNotExist):
        user = None
    if user is not None and account_activation_token.check_token(user, token):
        user.is_active = True
        user.save()
        login(request, user)
        return redirect('account:dashboard')
    else:
        return render(request, 'account/registration/activation_invalid.html')

After some research I found out that the error occurs because I was using https instead of http, since I'm using a local server.经过一些研究,我发现错误发生是因为我使用的是 https 而不是 http,因为我使用的是本地服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM