阅读背景:

Django - 如何使用两种形式从视图中访问文件

来源:互联网 

I have a view with two forms:

我有两种形式的观点:

def CreateNewUserView(request):
    if request.method == 'POST':
        user_form = UserCreationForm(request.POST, prefix='user_form')
        user_profile_form = UserProfileCreationForm(request.POST, request.FILES, prefix='user_profile_form')
        if user_form.is_valid():
            new_user = user_form.save()

            if user_profile_form.is_valid():            
                profile_picture = request.FILES['profile_picture']

                new_user_profile = UserProfile(user=new_user, profile_picture=profile_picture,
                )
                new_user_profile.save()

                username = user_form.cleaned_data['username']
                password = user_form.cleaned_data['password1']
                user = authenticate(username=username, password=password)
                login(request, user)

                return HttpResponseRedirect('/dashboard/')
    else:
        # Allow user to select a role
        user_form = UserCreationForm(prefix='user_form')
        user_profile_form = UserProfileCreationForm(prefix='user_profile_form')
        return render(request, 'dashboard/create_user.html', {
            'user_form':user_form,
            'user_profile_form':user_profile_form,
        })
de



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: