本文整理汇总了Python中users.forms.UserForm类的典型用法代码示例。如果您正苦于以下问题:Python UserForm类的具体用法?Python UserForm怎么用?Python UserForm使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UserForm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_user
def create_user(request):
if request.method == 'POST':
uform = UserForm(data=request.POST)
pform = UserProfileForm(data=request.POST)
if uform.is_valid() and pform.is_valid():
new_user = User.objects.create_user(
username=uform.cleaned_data['username'],
email=uform.cleaned_data['email'],
password=uform.cleaned_data['password1'],
first_name=uform.cleaned_data['first_name'],
last_name=uform.cleaned_data['last_name'])
role = pform.cleaned_data['role']
if role == 'M':
manager = Manager(manager=new_user)
manager.save()
if role == 'W':
waiter = Waiter(waiter=new_user)
waiter.save()
if role == 'B':
bartender = Bartender(bartender=new_user)
bartender.save()
return HttpResponseRedirect('/manager/')
else:
uform = UserForm(data=request.POST)
pform = UserForm(data=request.POST)
variables = RequestContext(request, {
'uform': uform,
'pform': pform
})
return render_to_response('create_user.html',
variables)
示例2: user_register
def user_register(request):
if request.method == "GET":
user_form = UserForm()
profile_form = ProfileForm()
elif request.method == "POST":
user_form = UserForm(request.POST)
profile_form = ProfileForm(request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
password = user.password
# The form doesn't know to call this special method on user.
user.set_password(password)
user.save()
# You must call authenticate before login. :(
user = authenticate(username=user.username,
password=password)
login(request, user)
messages.add_message(
request,
messages.SUCCESS,
"Congratulations, {}, on creating your new account! You are now logged in.".format(
user.username))
return redirect('index')
return render(request, "users/register.html", {'user_form': user_form,
'profile_form': profile_form})
示例3: register
def register(request):
Context = RequestContext(request)
if request.method == 'POST': #user bilgilerini girmis kayit ol butonuna basmis.
user_form = UserForm(data = request.POST)
profile_form = UserProfileForm(data = request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
#registered = True
profile = profile_form.save(commit = False)
profile.user = user
if 'picture' in request.FILES:
profile.picture = request.FILES['picture']
profile.save()
return render(request, 'users/register_success.html')
else:
print user_form.errors, profile_form.errors
else: # henuz yeni register ekrani goren user icin
user_form = UserForm()
profile_form = UserProfileForm()
return render_to_response(
'users/registration.html',
{'user_form':user_form, 'profile_form':profile_form}, Context)
示例4: register
def register(request):
if request.method == 'POST':
msj = {}
username = request.POST.get('username', None)
password = request.POST.get('password', None)
confirmation = request.POST.get('Confirmar', None)
if password==confirmation:
form = UserForm(request.POST)
existente=User.objects.filter(username=username)
if not existente:
if form.is_valid():
user = User.objects.create_user(username =username, email='None', password = password)
authenticated = authenticate(username = username, password = password)
auth.login(request, authenticated)
user.save()
return redirect('show_books')
else:
request.session['username']=username
msj['msj'] = '<center>The username already exists</center>'
msj['id']='error'
return render_to_response('register.html', {'form':form, 'msj':msj}, context_instance = RequestContext(request))
else:
return render_to_response('register.html', {'form':form}, context_instance = RequestContext(request))
return render_to_response('register.html', context_instance = RequestContext(request))
示例5: register_rater
def register_rater(request):
if request.method == "POST":
user_form = UserForm(request.POST)
rater_form = RaterForm(request.POST)
if user_form.is_valid() and rater_form.is_valid():
user = user_form.save()
rater = rater_form.save(commit=False)
rater.user = user
rater.save()
password = user.password
user.set_password(password)
user.save()
user = authenticate(username=user.username,
password=password)
login(request, user)
messages.add_message(
request,
messages.SUCCESS,
"Welcome, {}. You have successfully created an account and are now logged in".format(user.username))
return redirect('top20')
else:
user_form = UserForm()
rater_form = RaterForm()
return render(request, "users/register.html", {'user_form': user_form,
'rater_form': rater_form})
示例6: post
def post(self, request):
form = UserForm(request.POST)
if form.is_valid():
new_user = User.objects.create_user(**form.cleaned_data)
return redirect('/users/login/?error={}'.format("signup a success! now please login") )
else:
return render(request, 'users/create.html', {'error':"Not a valid name or password", 'form':UserForm(request.POST) } )
示例7: register
def register(request):
if request.method == 'GET':
form = UserForm()
greeting = 'Please register an account'
context = {
'form': form,
'greeting': greeting
}
return render(request, 'users/register.html', context)
else:
form = UserForm(request.POST)
if form.is_valid():
user = User(username=form.cleaned_data['username'],
email=form.cleaned_data['email'],
first_name=form.cleaned_data['first_name'],
last_name=form.cleaned_data['last_name'])
user.set_password(form.cleaned_data['password'])
user.save()
profile = UserProfile(user=user)
profile.save()
return redirect('/login/')
else:
context = {
'form': form,
'greeting': 'Invalid fields, please check errors.'
}
return render(request, 'users/register.html', context)
示例8: addUser
def addUser(request):
form = UserForm(request.POST)
if form.is_valid():
try:
with transaction.atomic():
enterprise = Enterprise()
enterprise.save()
request.session['idEnterprise'] = enterprise.id;
location= Location(enterprise=enterprise, lat=0, lng=0, name='Main Office')
location.save()
user = User(location = location, email=form.cleaned_data['email'],
password=form.cleaned_data['password'])
user.save()
request.session['idUser'] = user.id;
profile = Profile(user = user, role="Administrator")
profile.save()
return render(request, 'users/dashboard.html')
except Exception as e:
print(e)
messages.error(request, 'Sorry, Internal Error')
else:
messages.error(request, 'Please fill the form')
return HttpResponseRedirect('/signup')
示例9: post
def post(self, request):
user_form = UserForm(request.POST)
profile_form = ProfileForm(request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
password = user.password
user.set_password(password)
user.save()
user = authenticate(username=user.username,
password=password)
login(self.request, user)
messages.add_message(
request,
messages.SUCCESS,
"Account Successfully Created.")
return redirect("/")
else:
return render(request, "users/register.html", {"form1": user_form, "form2": profile_form})
示例10: post
def post(self, request, *args, **kwargs):
user_form = UserForm(request.POST)
user_profile_form = SignUpProfileForm(request.POST)
if user_form.is_valid() and user_profile_form.is_valid():
user = user_form.save()
user_profile = UserProfile(
user = user,
city = user_profile_form.data['city'],
country = user_profile_form.data['country']
)
try:
user_profile.save()
user = authenticate(
username=user_form.data.get('username'),
password=user_form.data.get('password1')
)
login(self.request, user)
except:
transaction.rollback()
return HttpResponseRedirect(self.success_url)
else:
context = {}
context['user_form'] = user_form
context['user_profile_form'] = user_profile_form
context['cities'] = City.objects.all()
context['countries'] = Country.objects.all()
return self.render_to_response(context)
示例11: post
def post(self, request):
if request.GET.get('login'):
self.post_login(request)
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user:
if user.is_active:
login(request, user)
return redirect('tasks:index')
return render(request, 'users/login.html', {'user_form': UserForm(), 'profile_form': ProfileForm()})
else:
user_form = UserForm(data=request.POST)
profile_form = ProfileForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid():
with atomic():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.creating = True
profile.updating = True
thread = Thread()
thread.save()
profile.thread = thread
profile.save()
login(request, user)
return redirect('tasks:index')
else:
return render(request, 'users/login.html', {'user_form':user_form, 'profile_form': profile_form})
示例12: _update
def _update(request, user):
was_superuser = user.is_superuser
form = UserForm(instance = user, data = request.PUT)
if form.is_valid():
user = form.save()
# Check if modified user is the last superuser in which case
# his super-user status must remain True
if was_superuser \
and not user.is_superuser \
and not User.objects.filter(is_superuser = True):
message = INFO_MSG%"Au moins un utilisateur doit être super-utilisateur."
request.user.message_set.create( message = message )
user.is_superuser = True
user.save()
# Changes have been made.
message = SUCCESS_MSG % "Modification effectuée avec succès."
request.user.message_set.create( message = message )
if request.user.is_superuser:
return redirect( 'user_collection' )
else:
return redirect( 'user_edit', request.user.id )
else:
message = ERROR_MSG % 'Le formulaire est invalide, veuillez corriger les erreurs suivantes.'
request.user.message_set.create( message = message )
return direct_to_template(request, "users/edit.html", {
'user': user,
'form': form
})
示例13: user_login
def user_login(request):
post = request.POST.copy()
if request.method == 'POST':
post['username'] = post['username'].lower()
email = post['username']
first_name = post['first_name']
form = UserForm(post)
user_profile_form = UserProfileForm(post)
if email and first_name:
try:
user = User.objects.get(email=email)
except User.DoesNotExist:
if form.is_valid():
user = form.save(commit=False)
user.set_password(user.username)
user = form.save()
user = authenticate(username=email, password=email)
if user is not None:
if user.is_active:
login(request, user)
# FIXME
post['user'] = user.id
user_profile_form = UserProfileForm(post)
if user_profile_form.is_valid():
try:
profile = user.get_profile()
except:
aux = UserProfile.objects
profile, created = aux.get_or_create(user=user)
newsletter = user_profile_form.clean()['newsletter']
profile.newsletter = newsletter
profile.save()
try:
spectacle = Spectacle.objects.get(status=True)
if spectacle.mode == SPECTACLE_MODE_EASY or \
spectacle.mode == SPECTACLE_MODE_RESET:
url = spectacle.get_easy_show_url()
else:
url = spectacle.get_hard_show_url()
return HttpResponseRedirect(url)
except Spectacle.MultipleObjectsReturned:
msg = '<h1>%s</h1>' % _('Spectacle not found')
return HttpResponseNotFound(msg)
else:
form = UserForm()
user_profile_form = UserProfileForm()
c = { 'form':form, 'user_profile_form':user_profile_form }
return render(request, 'login.html', c)
示例14: signup
def signup(request):
form=UserForm(request.POST or None)
if form.is_valid():
save_it=form.save(commit=False)
save_it.save()
messages.success(request,'Thank you for Joining us')
return HttpResponseRedirect('/signup/')
return render_to_response("signup.html",locals(),context_instance=RequestContext(request))
示例15: register
def register(request):
context = RequestContext(request)
# Set boolean to false
registered = False
# if request is post
if request.method == 'POST':
# Initialize forms to collect user data
user_form = UserForm(data=request.POST)
profile_form = UserProfileForm(data=request.POST)
# create user and userprofile classes to add data
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
# retrieve profile registration information
if 'first name' in request.FILES:
profile.fistName = request.FILES['first name']
if 'last name' in request.FILES:
profile.lastName = request.FILES['last name']
if 'picture' in request.FILES:
profile.picture = request.FILES['picture']
if 'school' in request.FILES:
profile.school = request.FILES['school']
if 'are you a teacher?' in request.FILES:
profile.isTeacher = request.FILES['are you a teacher?']
profile.save()
registered = True
# logs you in if your registration details check out
user = authenticate(username=request.POST['username'], password=request.POST['password'])
login(request, user)
return HttpResponseRedirect('/forum/')
else:
print user_form.errors, profile_form.errors
# if request is not post
else:
user_form = UserForm()
profile_form = UserProfileForm()
return render_to_response(
'users/register.html',
{'user_form': user_form, 'profile_form': profile_form, 'registered': registered},
context)