Django的。 Сheck激活码

时间:2015-12-02 23:54:43

标签: django

UPDATE!

激活码存储在数据库中,但我无法将其称为激活的功能 给出错误消息:

  

TypeError at / account / activation / 18d2ecbee1fd15440bbcfdf942c071a2f5b8d0ff /   activation()得到了一个意外的关键字参数'activation_key'

forms.py

from django import forms
from django.contrib.auth.models import User


class EmailForm(forms.Form):

    email = forms.EmailField(widget=forms.EmailInput(
        attrs={'class': 'form-control input-lg',
               'placeholder': 'Ваш E-mail'}))

views.py

import datetime
import hashlib
import random

from django.conf import settings
from django.contrib.auth import authenticate, logout, login
from django.contrib.auth.decorators import login_required
from django.core.mail import send_mail
from django.http import HttpResponseRedirect, HttpResponse
from django.http import Http404
from django.shortcuts import render

from forms import *
from models import *


def register(request):

    if request.method == "POST":
        form = EmailForm(request.POST)

        if form.is_valid():
            form_email = form.cleaned_data['email']
            salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
            activation_key = hashlib.sha1(salt+form_email).hexdigest()
            key_expires = datetime.datetime.today() + datetime.timedelta(2)

            subject = 'Activation your e-mail'
            from_email = settings.EMAIL_HOST_USER
            to_email = [form_email]
            message_email = "Hi, i'm link activate e-mail! \
            http://127.0.0.1:8000/account/activation/%s" % activation_key
            send_mail(subject,
                      message_email,
                      from_email,
                      to_email,
                      fail_silently=True)
            code = ActivationEmail.objects.create(activation_key=activation_key)
            code.save()

            return render(request, 'registration/activate.html', locals())

    else:
        form = EmailForm()

    return render(request, 'registration/register.html', locals())


def activation(request, code):
    try:
        active = ActivationEmail.objects.get(activation_key=code)
    except ActivationEmail.DoesNotExist:
        active = None
        active.validated = True
    if not active:
        raise Http404
    print "USER WAS HERE?!"
    return HttpResponseRedirect('/account/wizard/')

urls.py

from django.conf.urls import patterns, url
from . import views

urlpatterns = patterns('',
                       url(r'^register/', views.register, name='registration'),
                       url(r'^activation/(?P<activation_key>\w+)/', views.activation, name='activation'),
                       url(r'^login/$', views.user_login, name='login'),
                       url(r'^logout/$', views.user_logout, name='logout'),

无法弄清楚接下来该怎么做; (

1 个答案:

答案 0 :(得分:1)

你需要

1-您需要创建一个新表来存储激活密钥,以便您可以在发送电子邮件之前创建新条目

2.-在用户点击激活链接时,该链接应足以让您在发送电子邮件之前找到您创建的记录

3.-如果一切都匹配,则在您的用户模型上设置user.is_active类型的东西。

但是所有这一切,你在这里重新发明轮子。有几个顶级包可以使用这个和更多。我建议django-allauth并给你社交登录支持(例如facebook)。如果您只想要激活部分,则会有一个名为django-registration的旧软件包。如果你四处搜索,还有其他几个,但重点是你不需要实现这个(如果你不是专家,你可能不想要注册)