Django注册视图

Django注册视图,django,django-views,django-registration,Django,Django Views,Django Registration,我想创建一个注册视图。这里是我的代码: from django.shortcuts import get_object_or_404, render, redirect from django.contrib.auth import authenticate, login as auth_login from django.contrib.auth.models import User # create a function to resolve email to username def g

我想创建一个注册视图。这里是我的代码:

from django.shortcuts import get_object_or_404, render, redirect
from django.contrib.auth import authenticate, login as auth_login
from django.contrib.auth.models import User

# create a function to resolve email to username
def get_user(email):
    try:
        return User.objects.get(email=email.lower())
    except User.DoesNotExist:
        return None

def signup(request):
    if request.method == "GET":
        return render(request, 'accounts/signup.html')
    if request.method == "POST":
        email = request.POST['email']
        last_name = request.POST['last_name']
        first_name = request.POST['first_name']
        password = request.POST['password']
        password2 = request.POST['password2']
        user = get_user(email)
        if password == password2:
            if user is None:
                user = User.objects.create_user(last_name, email, password)
                user.first_name = first_name
                user.save()
                user = authenticate(username=user, password=password)
                #login
                return redirect('/')
            else:
                #messages
                return render(request, 'accounts/signup.html')
        else:
            #messages
            return render(request, 'accounts/signup.html')
我需要你让事情变得更好,因为就像你看到的,他有点乱

我不知道在这个任务中使用django注册这样的插件是否更好


非常感谢

首先,在应用程序的forms.py中创建一个表单类。在这里,您可以定义“电子邮件”、“名字”等字段。。。在表单类中重写
clean
方法,然后在视图中使用它

class RegistrationForm(forms.Form):
    username = forms.EmailField(max_length=30, widget=forms.TextInput(attrs=attrs_dict))
    password1 = forms.PasswordField()
    password2 = forms.PasswordField()
    first_name = forms.CharField(max_length=100)
    # rest of the fields

    def clean(self):
        cleaned_data = super(RegistrationForm, self).clean()
        username = cleaned_data.get("username")
        password1 = cleaned_data.get("password1")
        password2 = cleaned_data.get("password2")

        # you can validate those here

    class Meta:
        model = User
那么在你看来你

from forms import RegistrationForm

def signup(request):
    if request.method == "GET":
        return render(request, 'accounts/signup.html')
    if request.method == "POST":
        form = RegistrationForm(data = request.POST):
        if form.is_valid():
            user = form.save(False)
            user.set_password(user.password)
            user.save()
            user = authenticate(username=user.username, password=request.POST['password1'])
            login(request, user)

            return redirect('/')

它充分利用了django免费提供给您的内容。

首先,您要在应用程序的forms.py中创建一个表单类。在这里,您可以定义“电子邮件”、“名字”等字段。。。在表单类中重写
clean
方法,然后在视图中使用它

class RegistrationForm(forms.Form):
    username = forms.EmailField(max_length=30, widget=forms.TextInput(attrs=attrs_dict))
    password1 = forms.PasswordField()
    password2 = forms.PasswordField()
    first_name = forms.CharField(max_length=100)
    # rest of the fields

    def clean(self):
        cleaned_data = super(RegistrationForm, self).clean()
        username = cleaned_data.get("username")
        password1 = cleaned_data.get("password1")
        password2 = cleaned_data.get("password2")

        # you can validate those here

    class Meta:
        model = User
那么在你看来你

from forms import RegistrationForm

def signup(request):
    if request.method == "GET":
        return render(request, 'accounts/signup.html')
    if request.method == "POST":
        form = RegistrationForm(data = request.POST):
        if form.is_valid():
            user = form.save(False)
            user.set_password(user.password)
            user.save()
            user = authenticate(username=user.username, password=request.POST['password1'])
            login(request, user)

            return redirect('/')

它充分利用了django免费提供给您的内容。

首先,您要在应用程序的forms.py中创建一个表单类。在这里,您可以定义“电子邮件”、“名字”等字段。。。在表单类中重写
clean
方法,然后在视图中使用它

class RegistrationForm(forms.Form):
    username = forms.EmailField(max_length=30, widget=forms.TextInput(attrs=attrs_dict))
    password1 = forms.PasswordField()
    password2 = forms.PasswordField()
    first_name = forms.CharField(max_length=100)
    # rest of the fields

    def clean(self):
        cleaned_data = super(RegistrationForm, self).clean()
        username = cleaned_data.get("username")
        password1 = cleaned_data.get("password1")
        password2 = cleaned_data.get("password2")

        # you can validate those here

    class Meta:
        model = User
那么在你看来你

from forms import RegistrationForm

def signup(request):
    if request.method == "GET":
        return render(request, 'accounts/signup.html')
    if request.method == "POST":
        form = RegistrationForm(data = request.POST):
        if form.is_valid():
            user = form.save(False)
            user.set_password(user.password)
            user.save()
            user = authenticate(username=user.username, password=request.POST['password1'])
            login(request, user)

            return redirect('/')

它充分利用了django免费提供给您的内容。

首先,您要在应用程序的forms.py中创建一个表单类。在这里,您可以定义“电子邮件”、“名字”等字段。。。在表单类中重写
clean
方法,然后在视图中使用它

class RegistrationForm(forms.Form):
    username = forms.EmailField(max_length=30, widget=forms.TextInput(attrs=attrs_dict))
    password1 = forms.PasswordField()
    password2 = forms.PasswordField()
    first_name = forms.CharField(max_length=100)
    # rest of the fields

    def clean(self):
        cleaned_data = super(RegistrationForm, self).clean()
        username = cleaned_data.get("username")
        password1 = cleaned_data.get("password1")
        password2 = cleaned_data.get("password2")

        # you can validate those here

    class Meta:
        model = User
那么在你看来你

from forms import RegistrationForm

def signup(request):
    if request.method == "GET":
        return render(request, 'accounts/signup.html')
    if request.method == "POST":
        form = RegistrationForm(data = request.POST):
        if form.is_valid():
            user = form.save(False)
            user.set_password(user.password)
            user.save()
            user = authenticate(username=user.username, password=request.POST['password1'])
            login(request, user)

            return redirect('/')

它充分利用了django免费提供给你的东西。

我觉得它很不错。您可以使用基于类的视图:。为什么不使用表单?这会让你的生活简单得多。django注册,IMO可能是一个很好的选择,因为它将减少您需要管理的大量代码。这可能很有用:它看起来对我很好。您可以使用基于类的视图:。为什么不使用表单?这会让你的生活简单得多。django注册,IMO可能是一个很好的选择,因为它将减少您需要管理的大量代码。这可能很有用:它看起来对我很好。您可以使用基于类的视图:。为什么不使用表单?这会让你的生活简单得多。django注册,IMO可能是一个很好的选择,因为它将减少您需要管理的大量代码。这可能很有用:它看起来对我很好。您可以使用基于类的视图:。为什么不使用表单?这会让你的生活简单得多。django注册,IMO可能是一个不错的选择,因为它将减少您必须管理的大量代码。这可能很有用: