以用户输入创建Django中的用户 [英] Taking User Input to create Users in Django

查看:81
本文介绍了以用户输入创建Django中的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用用户输入在Django中创建/添加新用户到我的应用程序。我使用django提供的默认登录。我正在尝试将用户添加到默认登录。 https://docs.djangoproject.com/en/dev/topics/auth中的示例/

 >>>来自django.contrib.auth.models import User 
>>> user = User.objects.create_user('john','lennon@thebeatles.com','john password')

传递用户名和密码。但是我希望用户输入会发生这种情况。我如何做到这一点?



需要一些指导...



似乎工作:

  def lexusadduser(request):

添加用户

如果request.method ==POST:
user.save()

auth_user = authenticate(username = user.username,password = user.password )

如果auth_user不是None:
lexusmain(request)
else:
return render('adduser.html')


解决方案

首先需要做的是创建一个 ModelForm



forms.py

 从django.contrib.auth.models import User 

class UserForm(ModelForm):
class Meta:
model = User
fields =('username','email','password')

一个ModelForm会自动根据您提供的模型构建您的表单。它根据字段处理验证。



views.py



<$ p $从django.contrib.auth导入登录
从django.http导入HttpResponseRedirect

def lexusadduser(request):
如果request.method ==POST:
form = UserForm(request.POST)
如果form.is_valid():
new_user = User.objects.create_user(** form
login(new_user)
#redirect,或者你想要获得主视图
return HttpResponseRedirect('main.html')
else:
form = UserForm()

return render(request,'adduser.html',{'form':form})

如果是POST请求,我们从POST值创建一个UserForm。那么我们检查一下它的有效表单。如果是,我们创建用户,否则我们返回带有错误的表单。如果它不是POST请求,我们发送一个干净的表单



模板

 < form method =postaction => 
{%csrf_token%}
{{form}}
< input type =submitvalue =创建新用户帐户/>
< / form>


I would like to create/add new users to my app in Django using the user input. I am using the default login provided by django. I am trying to add users to the default login. The example in https://docs.djangoproject.com/en/dev/topics/auth/:

>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'john password')

passes the username and password. But i would like this to happen with user input. How do i do this?

Need some guidance...

I tried this but it doesn't seem to work:

def lexusadduser(request):
    """
    add user
    """
    if request.method == "POST":
        user.save()

        auth_user = authenticate(username=user.username,password=user.password)

        if auth_user is not None:
            lexusmain(request)
        else:
            return render('adduser.html') 

解决方案

First thing you need to do is create a ModelForm:

forms.py

from django.contrib.auth.models import User

class UserForm(ModelForm):
    class Meta:
        model = User
        fields = ('username', 'email', 'password')

A ModelForm automatically builds your form off a model you provide. It handles the validations based on the fields.

views.py

from forms import UserForm
from django.contrib.auth import login
from django.http import HttpResponseRedirect

def lexusadduser(request):
    if request.method == "POST":
        form = UserForm(request.POST)
        if form.is_valid():
            new_user = User.objects.create_user(**form.cleaned_data)
            login(new_user)
            # redirect, or however you want to get to the main view
            return HttpResponseRedirect('main.html')
    else:
        form = UserForm() 

    return render(request, 'adduser.html', {'form': form}) 

If its a POST request, we create a UserForm from the POST values. Then we check if its a valid form. If it is, we create the user, otherwise we return the form with the errors. If its not a POST request, we send a clean form

template

<form method="post" action="">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Create new user account" />
</form>

这篇关于以用户输入创建Django中的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆