本文整理汇总了Python中forms.UserProfileForm.save_m2m方法的典型用法代码示例。如果您正苦于以下问题:Python UserProfileForm.save_m2m方法的具体用法?Python UserProfileForm.save_m2m怎么用?Python UserProfileForm.save_m2m使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类forms.UserProfileForm
的用法示例。
在下文中一共展示了UserProfileForm.save_m2m方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: edit_user
# 需要导入模块: from forms import UserProfileForm [as 别名]
# 或者: from forms.UserProfileForm import save_m2m [as 别名]
def edit_user(request, membership, user=None):
""" Edit an existing user, or add a new one if no user specified
"""
# Make sure that we are attempting to attach to a valid
# membership.
try:
membership = Membership.objects.get(id=membership)
except ObjectDoesNotExist:
error = "That Membership does not appear to exist."
return error_404(request, error)
# And make sure that we're talking about a valid user, if user was
# specified.
if user:
user = int(user)
try:
user = User.objects.get(id=user)
profile = UserProfile.objects.get(user=user)
except ObjectDoesNotExist:
error = """That user does not exist, or you are attempting
to edit a user without a profile (you must use the admin
interface to edit this sort of user)."""
return error_404(request, error)
if request.method == 'POST':
if user:
# save changes to an existing user
user_form = UserForm(request.POST, instance=user)
profile_form = UserProfileForm(request.POST, instance=profile)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
profile = profile_form.save()
return HttpResponseRedirect(
'/memberships/' + str(membership.id) + '/view/'
)
else:
template = 'edit_user.html'
ctxt = {
'request' : request,
'membership' : membership,
'user' : user.id,
'edit_user' : user_form,
'edit_profile' : profile_form,
'page_name' : 'Edit User',
'media_url' : MEDIA_URL,
}
return render_to_response(template, ctxt)
else:
# save a new user
user_form = UserForm(request.POST)
profile_form = UserProfileForm(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.objects.get(id=user.id)
profile.membership = membership
profile.save()
profile_form.save_m2m()
# try:
# Try to send an email w/ password reset link to
# the user
subject = "Boston Mycological Club Account "
subject += "Created"
email_template = 'registration/new_user_email.html'
ctxt = { 'user' : user, 'media_url' : MEDIA_URL, }
message = loader.get_template(email_template)
message = message.render(Context(ctxt))
send_mail(subject, message, SERVER_EMAIL,
[user.email], fail_silently=False,
)
# except:
# error = "Account created, but email was not sent"
# return error_404(request, error)
return HttpResponseRedirect(
'/memberships/' + str(membership.id) + '/view/'
)
else:
template = 'edit_user.html'
ctxt = {
'request' : request,
'membership' : membership,
'user' : None,
'edit_user' : user_form,
'edit_profile' : profile_form,
'page_name' : 'Create User',
'media_url' : MEDIA_URL,
}
return render_to_response(template, ctxt)
#.........这里部分代码省略.........