我正在为非专家用户开发一个带Django的网站,并且困扰我创建新用户的第2步。 我希望创建用户的过程将在第一步完成。为了方便我的客户创建或编辑用户。
答案 0 :(得分:1)
注册新用户:
<强> register.html 强>
{{state}}
<form method="post">{% csrf_token %}
First Name: <input type="text" name="fname">
Last Name: <input type="text" name="lname">
Email: <input type="email" name="email">
Username: <input type="text" name="username">
Password: <input type="password" name="password">
<input type="submit" value="Register">
</form>
<强> views.py 强>
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render
def register(request, state=""):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
email = request.POST.get('email')
fname = request.POST.get('fname')
lname = request.POST.get('lname')
username_exist = User.objects.filter(username=username)
email_exist = User.objects.filter(email=email)
if username_exist and email_exist:
state = "Username and Email are already exists. Please enter another."
elif username_exist:
state = "Username is already exist. Please try another one."
email_session = email
elif email_exist:
state = "Email is already exist. Please use other account."
username_session = username
else:
add_user = User.objects.create_user(username=username,
password=password, email=email )
add_user.is_active = True
add_user.first_name = fname
add_user.last_name = lname
add_user.save()
user = authenticate(username=username, password=password)
login(request, user)
return HttpResponseRedirect(reverse('app_name:url_name'))
return render(request, 'register.html', {'state':state})
要更新用户,请创建其基本信息显示的profile.html,然后创建用于更新个人资料的按钮。