当前位置: 首页>>代码示例>>Python>>正文


Python UserProfileForm.save_m2m方法代码示例

本文整理汇总了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)

#.........这里部分代码省略.........
开发者ID:petevg,项目名称:bostonmycologicalclub,代码行数:103,代码来源:views.py


注:本文中的forms.UserProfileForm.save_m2m方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。