示例#1
0
def add_parent(request, student):

        
    context = RequestContext(request)

    registered = False

    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = ProfileForm(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()

            profile = profile_form.save(commit=False)
            profile.user = user
            profile.save()

            parent_profile = ParentProfile()
            parent_profile.user_profile = profile
            parent_profile.save()
            
            #get the student's user account. sloppy
                 
            #student_profile_form = student_profile_form.save(commit=False)
            #student_profile_form.user_profile = profile
            #student_profile_form.educator = request.user
            #student_profile_form.save()

            #user.set_password(student_profile_form.plaintext_pw)
            #user.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 = ProfileForm()


    # Render the template depending on the context.
    return render_to_response(
            'educator/add_parent.html',
            {'forms': [user_form, profile_form], 
            'registered': registered,
            'student': student,},
            context)  
示例#2
0
文件: views.py 项目: bookaround/dev
def educator_register(request):
    context = RequestContext(request)

    registered = False

    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = ProfileForm(data=request.POST)
        educator_profile_form = EducatorProfileForm(data=request.POST)        

        # If the two forms are valid...
        if user_form.is_valid() and profile_form.is_valid() and educator_profile_form.is_valid():
            # Save the user's form data to the database.
            user = user_form.save()

            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user
            profile.save()

            educator_profile = educator_profile_form.save(commit=False)
            educator_profile.user_profile = profile
            educator_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, educator_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 = ProfileForm()
        educator_profile_form = EducatorProfileForm()

    # Render the template depending on the context.
    template = 'bookaround/index.html' if registered else 'bookaround/educator_register.html'
    return render_to_response(
            template,
            {'forms': [user_form, profile_form, educator_profile_form], 'registered': registered},
            context)