用我自己在Django中制作的Custom_user_model的UpdateView

时间:2018-08-19 04:20:26

标签: python django django-models django-views

我想使用电子邮件进行身份验证,所以我对默认用户进行了更改,并由AbstractUser进行了新的自定义,但是当我进行视图更新配置文件时,该配置文件没有给出任何查询,显示页面未找到,没有用户与给定查询。 这是示例代码

**models.py**
from django.db import models
from django.contrib.auth.models import AbstractUser,BaseUserManager
from django.utils.translation import ugettext_lazy as _
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db.models.signals import pre_save
from django.utils.text import slugify
from django.urls import reverse
from django.utils import timezone
from .choices import branch_choices

#from app_name.models import skills
# Create your models here.

class UserManager(BaseUserManager):
    """Define a model manager for User model with no username field."""

    use_in_migrations = True

    def _create_user(self, email, password, **extra_fields):
        """Create and save a User with the given email and password."""
        if not email:
            raise ValueError('The given email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        """Create and save a regular User with the given email and password."""
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        """Create and save a SuperUser with the given email and password."""
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self._create_user(email, password, **extra_fields)







class User(AbstractUser):
    #age=models.CharField(max_length=2,blank=True)
    bio = models.TextField(max_length=500, blank=True)
    slug=models.SlugField(unique=True)
    #locatition = models.CharField(max_length=30, blank=True)
    designation=models.CharField(max_length=50,blank=True)
    company=models.CharField(max_length=255,blank=True)
    #skills=models.ManyToManyField(skills)
    passing=models.PositiveIntegerField(validators=[MinValueValidator(2002),MaxValueValidator(timezone.now().year)],default=timezone.now().year,blank=True)
    branch=models.CharField(max_length=10,choices=branch_choices,blank=True)
    #mobile=models.IntegerField(blank=True)
    website=models.URLField(max_length=255,blank=True)
    mtj=models.TextField(max_length=500,blank=True)
    username=None
    email=models.EmailField(_('email address'),unique=True)
    USERNAME_FIELD='email'
    REQUIRED_FIELDS=[]
    objects = UserManager()

    def __str__(self):
        return self.email
    def get_absolute_url(self):
        return reverse("my_profile",kwargs={"slug":self.slug})  

def create_slug(instance,new_slug=None):
    s = instance.email
    result = s[:s.find("@")]
    slug=slugify(result)
    if new_slug is not None:
        slug=new_slug
    qs=User.objects.filter(slug=slug).order_by("-id")
    exists=qs.exists()
    if exists:
        new_slug="%s-%s"%(slug,qs.first().id)
        return create_slug(instance,new_slug=new_slug)
    return slug


def pre_save_post_reciever(sender,instance,*args,**kwargs):
    if not instance.slug:
        instance.slug=create_slug(instance)
pre_save.connect(pre_save_post_reciever,sender=User)

这是我对UpdateProfile的看法

    from django.shortcuts import render,redirect,get_object_or_404
from .forms import SignUpForm
from django.contrib.auth import login 
from django.utils.decorators import method_decorator
from django.conf import settings
from django.contrib.auth.forms import UserChangeForm
# Create your views here.
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required
from django.views.generic import UpdateView,ListView
from django.contrib.auth import get_user_model
#from django.contrib.auth.models import User
User = get_user_model()


@method_decorator(login_required, name='dispatch')
class UserUpdateView(UpdateView):
    model = User
    fields = ('first_name', 'last_name','bio','designation','company','passing','branch','website','mtj')
    template_name = 'account/my_account.html'

    def get_object(self):
        return self.request.user

Urls.py

from .views import UserUpdateView
path('profile/edit/',UserUpdateView,name='my_account')

谢谢

0 个答案:

没有答案