简体   繁体   中英

Am I able to create a User instance by CBV rather than create a form first? (Django)

Now I used forms.py, models.py, views.py to create a User instance like below and it works:

models.py:

from django.db import models
from django.contrib import auth
from django.utils import timezone

# Create your models here.
class User(auth.models.User, auth.models.PermissionsMixin):

    def __str__(self):
        return "@{}".format(self.username)

views.py:

from django.shortcuts import render
from django.views import generic
from django.urls import reverse,reverse_lazy
from . import forms, models
from django.contrib.auth import get_user_model
# Create your views here.


class SignUp(generic.CreateView):
    form_class = forms.UserCreateForm
    success_url = reverse_lazy("login")
    template_name = "accounts/signup.html"

forms.py

from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm


class UserCreateForm(UserCreationForm):
    class Meta:
        fields = ("username", "email", "password1", "password2")
        model = get_user_model()
# below code is not necessary, just want to customize the builtin attribtue of
# the User class
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["username"].label = "Display name"
        self.fields["email"].label = "Email address"

However, I am wondering if I am possible to create a User by editing the views.py like below. views.py:

from django.shortcuts import render
from django.views import generic
from django.urls import reverse,reverse_lazy
from . import forms, models
from django.contrib.auth import get_user_model
# Create your views here.


class SignUp(generic.CreateView):
    model = get_user_model()
    success_url = reverse_lazy("login")
    template_name = "accounts/signup.html"
    fields = ("username", "email","password1","password2")



# below is original version
# class SignUp(generic.CreateView):
#     form_class = forms.UserCreateForm
#     success_url = reverse_lazy("login")
#     template_name = "accounts/signup.html"

It came up an error when I am in the "accounts/signup.html" Unknown field(s) (password1) (password2 )specified for User.

If I remove this two fields, "password1","password2", I would be able to reach "accounts/signup.html" and create a User instance without password which I can see in the admin page though it's not useful.

So I would like to if there's any good way to create a user just using generic.Createview and User model?

And why did I get the error Unknown field(s) (password1) (password2 )specified for User?

Looking forward to get any suggesstions soon!

The UserCreationForm takes care of checking that password1 == password2 , then hashes the password and sets user.password .

If you don't have a custom form, then you can't use password1 and password2 , because these aren't model fields.

You could add password to fields in the view, but it wouldn't display as a password input. You would also need to add code to the view to hash the password when the user is saved.

So it might be possible to remove the form from SignUp , but I don't recommend it, because it will make the view more complicated, and it might lose functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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