如何在django 2.0中设置注册和信号

问题描述 投票:1回答:1

有人可以帮我弄清楚我在做什么错误尝试设置身份验证和信号与django 2.0中创建的模型一起工作。

models.朋友

from django.db import models
from django.conf import settings.AUTH_USER_MODEL
from django.db.models.signals import post_save
from django.dispatch import receiver

class Profile(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    bio = models.TextField(foo)
    location = models.CharField(foo)


    # Override save method
    # Run after the method is saved to add signal functionality below
    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)
    else:
        instance.profile.save()

forms.朋友

from django import forms
from django.contrib.auth import get_user_model
from django.forms import ModelForm
from .models import Profile

Profile = get_user_model()

class UserRegisterForm(forms.ModelForm):
    email = forms.EmailField(foo)
    first_name = forms.CharField(foo)
    last_name = forms.CharField(foo)
    password = forms.CharField(foo)
    password2 = forms.CharField(foo)

    class Meta:
        model = Profile
        fields = [
            'username',
            'first_name',
            'last_name',
            'email',
            'password1',
            'password2'
        ]

views.朋友

from django.shortcuts import render, redirect
from django.contrib.auth import get_user_model
from .forms import UserRegisterForm
from .models import Profile
from django.conf import settings

def register(request):
    next = request.GET.get('next')
    form = UserRegisterForm(request.POST or None)
    if form.is_valid():
        user = form.save(commit=False)
        password = form.cleaned_data.get('password')
        user.set_password(password)
        user.save()
        new_user = authenticate(username=user.username, password=password)
        login(request, new_user)
        if next:
            return redirect(next)
        return redirect('accounts:login')

    context = {
        'form': form,
    }
    return render(request, "accounts/register.html", context)

Foo =只是一个占位符

上面的代码创建了用户,但没有创建与之关联的配置文件。

任何帮助将非常感激。

django python-3.x authentication django-registration django-2.0
1个回答
0
投票

这是我发现的一个来自youtube的视频,它以最好的方式解释(我认为)关于用他们的个人资料创建用户:Youtube Tutorial,希望它帮助你。


推荐问答