我正在为非专家用户开发一个带有 Django 的网站,并打扰我创建新用户的第 2 步。我希望创建用户的过程将在第一步完成。为我的客户创建或编辑用户提供最简单的方式。
问问题
80 次
1 回答
1
对于新用户的注册:
register.html
{{状态}}
<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>
视图.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,然后创建用于更新配置文件的按钮。
于 2013-02-11T03:50:40.900 回答