本文整理汇总了Python中accounts.forms.UserForm类的典型用法代码示例。如果您正苦于以下问题:Python UserForm类的具体用法?Python UserForm怎么用?Python UserForm使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UserForm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: signup
def signup(request):
context = {}
context['title'] = _('Register')
user = User()
if request.user.is_authenticated():
return redirect('accounts_accounts')
userProfile = UserProfile()
if request.method == 'POST':
userForm = UserForm(request.POST, instance=user)
userProfileForm = UserProfileForm(request.POST, instance=userProfile)
if userForm.is_valid() and userProfileForm.is_valid():
userData = userForm.cleaned_data
user.username = userData['username']
user.first_name = userData['first_name']
user.last_name = userData['last_name']
user.email = userData['email']
user.set_password(userData['password'])
user.save()
userProfile = user.get_profile()
userProfileData = userProfileForm.cleaned_data
userProfile.gender = userProfileData['gender']
userProfile.birthday = userProfileData['birthday']
userProfile.save()
user = authenticate(username=userData['username'], password=userData['password'])
login(request, user)
return redirect('accounts_accounts')
else:
userForm = UserForm(instance=user)
userProfileForm = UserProfileForm(instance=userProfile)
context['userForm'] = userForm
context['userProfileForm'] = userProfileForm
return render_to_response('accounts/register.html', context, context_instance=RequestContext(request))
示例2: post
def post(self, request, *args, **kwargs):
# Attempt to grab information from the raw form information.
# Note that we make use of both UserForm and PublicForm.
form_args = {
'data': request.POST,
}
user_form = UserForm(data=request.POST, instance=request.user)
instance = UserProfile.objects.get(user=request.user)
kwargs.setdefault('curruser', instance)
profile_form = UserProfileForm(data=request.POST, instance=instance)
# If the two forms are valid...
if user_form.is_valid() and profile_form.is_valid():
# Save the user's form data to the database.
user = user_form.save()
# Now sort out the Public instance.
# Since we need to set the user attribute ourselves, we set commit=False.
# This delays saving the model until we're ready to avoid integrity problems.
profile = profile_form.save(commit=False)
profile.user = user
# Did the user provide a profile picture?
# If so, we need to get it from the input form and put it in the Public model.
if 'picture' in request.FILES:
profile.picture = request.FILES['picture']
# Now we save the Public model instance.
profile.save()
# Invalid form or forms - mistakes or something else?
# Print problems to the terminal.
# They'll also be shown to the user.
else:
print user_form.errors, profile_form.errors
kwargs.setdefault("user_form", self.user_form_class(instance=instance.user))
kwargs.setdefault("profile_form", self.profile_form_class(instance=instance))
return super(Profile, self).get(request, *args, **kwargs)
示例3: user_profile
def user_profile(request):
profile = request.user.get_profile()
if request.method == "POST":
# Read params
form = ProfileForm(request.POST, instance=profile)
subscriptionform = SubscriptionForm(request.POST, instance=profile)
userform = UserForm(request.POST, instance=request.user)
if appsettings.DEMO_SERVER and request.user.username == "demo":
messages.warning(request, _("You can not change demo profile on the demo server."))
return redirect("profile")
if form.is_valid() and userform.is_valid() and subscriptionform.is_valid():
# Save changes
form.save()
subscriptionform.save()
userform.save()
# Change language
set_lang(request.user, request=request, user=request.user)
# Redirect after saving (and possibly changing language)
response = redirect("profile")
# Set language cookie and activate new language (for message below)
lang_code = profile.language
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code)
translation.activate(lang_code)
messages.info(request, _("Your profile has been updated."))
return response
else:
form = ProfileForm(instance=profile)
subscriptionform = SubscriptionForm(instance=profile)
userform = UserForm(instance=request.user)
social = request.user.social_auth.all()
social_names = [assoc.provider for assoc in social]
new_backends = [x for x in load_backends(BACKENDS).keys() if x == "email" or x not in social_names]
response = render_to_response(
"accounts/profile.html",
RequestContext(
request,
{
"form": form,
"userform": userform,
"subscriptionform": subscriptionform,
"profile": profile,
"title": _("User profile"),
"licenses": Project.objects.exclude(license=""),
"associated": social,
"new_backends": new_backends,
},
),
)
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, profile.language)
return response
示例4: user_profile
def user_profile(request):
"""
Form to update User profile
"""
user_ = get_user_in_token(request)
if request.method == 'POST':
nic = request.POST.get('nicname')
profileForm = UserProfileForm(request.POST, request.FILES, instance=user_.get_profile())
userForm = UserForm(request.POST, instance=user_)
# if profileForm.is_valid() and userForm.is_valid():
if profileForm.is_valid():
profileForm.save()
userForm.save()
return output_format_json_response(201, message='프로필이 변경 되었습니다.', statusCode='0000')
else:
profile = request.user.get_profile()
profile_data = {
'nickname': profile.nickname,
'picture_url': profile.get_image_url(),
'phone_number': profile.phone_number
}
return output_format_response(200, statusCode='0000', data=profile_data)
return output_format_json_response(message='요청이 잘못되었습니다.', statusCode='5555')
示例5: register
def register(request):
context = RequestContext(request)
registered = False
if request.method == 'POST':
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()
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
registered = True
else:
print user_form.errors, profile_form.errors
else:
user_form = UserForm()
profile_form = UserProfileForm()
#Render the template depending on the context
return render_to_response('register.html',
{'user_form':user_form,'profile_form':profile_form,
'registered':registered}, context )
示例6: register
def register(request):
registered = False
profile = UserProfile.objects.get(user=request.user)
if request.method == 'POST':
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()
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
registered = True
else:
print user_form.errors, profile_form.errors
else:
user_form = UserForm()
profile_form = UserProfileForm()
return render(request,
'accounts/reg.html',
{'user_form': user_form, 'profile_form': profile_form, 'registered': registered, 'profile': profile,} )
示例7: test_failure_user_form
def test_failure_user_form(self):
"""正常ではない入力を行いエラーになることを検証"""
print('Test Case 4-2')
print()
user = User()
form = UserForm(data={'username': '', 'password': 'pass01'}, instance=user)
self.assertTrue(not(form.is_valid()))
示例8: edit_my_profile
def edit_my_profile(request):
u = request.user
try: profile = u.get_profile()
except UserProfile.DoesNotExist: profile = None
if request.method == 'POST':
POST = request.POST.copy()
POST['user'] = u.id
profile_form = UserProfileForm(POST, request.FILES, instance=profile)
user_form = UserForm(request.POST, request.FILES, instance=u)
if user_form.is_valid() and profile_form.is_valid():
u = user_form.save()
profile = profile_form.save()
profile.user = u
request.user.message_set.create(message="Your Profile was updated")
return HttpResponseRedirect(profile.get_absolute_url())
else:
user_form = UserForm(instance=u)
if profile: profile_form = UserProfileForm(instance=profile)
else: profile_form = UserProfileForm(initial={'user':request.user})
return render(request, 'edit_profile.html', {'profile_form':profile_form, 'user_form':user_form})
示例9: edit_user
def edit_user(request):
profile = User_Profile.objects.get(User_associated=request.user)
user = User.objects.get(id=request.user.id)
# img = None
if request.method == "POST":
profileForm = ProfileForm(request.POST, instance=profile, prefix="profile_form")
userForm = UserForm(request.POST, instance=user, prefix="user_form")
# passwordForm = PasswordChangeForm(data=request.POST, user=request.user, prefix="password_form")
if profileForm.is_valid() and userForm.is_valid():
profileForm.save()
userForm.save()
# passwordForm.save()
else:
profileForm = ProfileForm(prefix="profile_form", instance=profile,
initial={"Degree": profile.Degree,
"Year_first_enrolled": profile.Year_first_enrolled,})
userForm = UserForm(prefix="user_form", instance=user,
initial={'username': user.username,
'email': user.email,
'first_name': user.first_name,
'last_name': user.last_name})
return render_to_response("accounts/settings.html",
{"profile_form": profileForm, "user_form": userForm}, context_instance=RequestContext(request))
示例10: profile
def profile(request):
form = UserForm(instance=request.user)
pwd_form = PasswordChangeForm(request.user)
associations = {'twitter': False, 'google_oauth2': False, 'github': False}
for association in request.user.social_auth.all():
associations[association.provider.replace('-', '_')] = True
if request.method == 'POST':
if request.POST.get('do_password'):
pwd_form = PasswordChangeForm(request.user, request.POST)
if pwd_form.is_valid():
pwd_form.save()
messages.success(request, "Password successfully changed.")
else:
messages.error(request, "Could not update password. See errors below.")
elif request.POST.get('do_profile'):
form = UserForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
messages.success(request, 'Profile successfully updated.')
else:
messages.error(request, 'You have an error in your profile. See below errors.')
else:
messages.error(request, "Er, something weird happened. Contact the site admin.")
return render(request, 'accounts/profile.html', locals())
示例11: registration
def registration(request):
registered = False
if request.method == 'POST':
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()
profile = profile_form.save(commit=False)
profile.user = user
if 'picture' in request.FILES:
profile.picture = request.FILES['picture']
profile.save()
registered = True
else:
print user_form.errors, profile_form.errors
else:
user_form = UserForm()
profile_form = UserProfileForm()
return render(request,
'accounts/register.html',
{'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
示例12: register
def register(request):
if request.method == 'POST':
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()
profile = profile_form.save(commit=False)
profile.user = user
if 'picture' in request.FILES:
profile.picture = request.FILES['picture']
profile.save()
else:
print user_form.errors, profile_form.errors
else:
user_form = UserForm()
profile_form = UserProfileForm()
context = {'user_form' : user_form, 'profile_form' : profile_form}
return render_to_response('accounts/register.html', context, context_instance=RequestContext(request))
示例13: test_success_user_form
def test_success_user_form(self):
"""正常な入力を行いエラーにならないことを検証"""
print('Test Case 4-1')
print()
user = User()
form = UserForm(data={'username': 'user01', 'password': 'pass01'}, instance=user)
self.assertTrue(form.is_valid())
示例14: register
def register(request):
# Like before, get the request's context.
context = RequestContext(request)
# A boolean value for telling the template whether the registration was successful.
# Set to False initially. Code changes value to True when registration succeeds.
registered = False
# If it's a HTTP POST, we're interested in processing form data.
if request.method == 'POST':
# Attempt to grab information from the raw form information.
# Note that we make use of both UserForm and UserProfileForm.
user_form = UserForm(data=request.POST)
profile_form = UserProfileForm(data=request.POST)
# If the two forms are valid...
if user_form.is_valid() and profile_form.is_valid():
# Save the user's form data to the database.
user = user_form.save()
# Now we hash the password with the set_password method.
# Once hashed, we can update the user object.
user.set_password(user.password)
user.save()
# Now sort out the UserProfile instance.
# Since we need to set the user attribute ourselves, we set commit=False.
# This delays saving the model until we're ready to avoid integrity problems.
profile = profile_form.save(commit=False)
profile.user = user
# Did the user provide a profile picture?
# If so, we need to get it from the input form and put it in the UserProfile model.
if 'picture' in request.FILES:
profile.picture = request.FILES['picture']
# Now we save the UserProfile model instance.
profile.save()
# Update our variable to tell the template registration was successful.
registered = True
# Invalid form or forms - mistakes or something else?
# Print problems to the terminal.
# They'll also be shown to the user.
else:
print user_form.errors, profile_form.errors
# Not a HTTP POST, so we render our form using two ModelForm instances.
# These forms will be blank, ready for user input.
else:
user_form = UserForm()
profile_form = UserProfileForm()
# Render the template depending on the context.
return render_to_response(
'register.html',
{'user_form': user_form, 'profile_form': profile_form, 'registered': registered},
context)
示例15: post
def post(self, request):
profile_form = UserForm(request.POST, request.FILES, instance=request.user)
if profile_form.is_valid():
profile_form.save()
return redirect('accounts_profile')
else:
return self._render(request, profile_form=profile_form)