本文整理汇总了Python中accounts.forms.UserForm.is_valid方法的典型用法代码示例。如果您正苦于以下问题:Python UserForm.is_valid方法的具体用法?Python UserForm.is_valid怎么用?Python UserForm.is_valid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类accounts.forms.UserForm
的用法示例。
在下文中一共展示了UserForm.is_valid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: signup
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
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
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
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: test_success_user_form
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
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())
示例4: user_profile
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
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
示例5: registration
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
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} )
示例6: register
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
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 )
示例7: edit_user
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
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))
示例8: profile
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
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())
示例9: register
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
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))
示例10: register
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
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,} )
示例11: edit_my_profile
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
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})
示例12: test_failure_user_form
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
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()))
示例13: register
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
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)
示例14: register_view
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
def register_view(request):
form = UserForm()
if request.method == 'POST':
form = UserForm(request.POST)
if form.is_valid():
form.save()
return HttpResponse('<h2>Thank you! You are now registered!</h2>')
return render(request, 'accounts/registration_form.html', {'form': form})
示例15: post
# 需要导入模块: from accounts.forms import UserForm [as 别名]
# 或者: from accounts.forms.UserForm import is_valid [as 别名]
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)