Exemplo n.º 1
0
def register(request):
    if request.POST:
        user_form = UserForm(request.POST)
        profile_form = UserProfileForm(request.POST, request.FILES)

        if user_form.is_valid():
            print('forms are valid')
        else:
            print('forms ar invalid')

        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 profile.avatar:
                profile.avatar = request.FILES['avatar']
            profile.save()
        else:
            print(user_form.errors, profile_form.errors)
        return redirect('person:home')
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()
        return render (request, "register.html", {'user_form': user_form,
                                                  'profile_form': profile_form})
Exemplo n.º 2
0
def edit(request, profile_id):
    profile = UserProfile.objects.get(id=profile_id)
    profile_form = UserProfileForm(instance=profile)
    if not request.user.is_authenticated():
        return redirect("person:login")
    elif int(profile_id) != request.user.id:
        return  redirect("person:edit", request.user.id)
    else:
        if request.POST:
            profile_form = UserProfileForm(request.POST, request.FILES, instance=profile)
            if profile_form.is_valid():
                profile_form.save()
                return redirect("person:details", profile.id)
            else:
                print("Form is not valid!")
        return render(request, "edit.html", {'profile':profile,
                                         'profile_form':profile_form})
Exemplo n.º 3
0
def register(request):
    if request.POST:
        user_form = UserForm(request.POST)
        profile_form = UserProfileForm(request.POST, request.FILES)

        if user_form.is_valid():
            print('forms are valid')
        else:
            print('forms ar invalid')

        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 profile.avatar:
                profile.avatar = request.FILES['avatar']
            profile.save()
        else:
            print(user_form.errors, profile_form.errors)
        return redirect('person:home')
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()
        return render(request, "register.html", {
            'user_form': user_form,
            'profile_form': profile_form
        })
Exemplo n.º 4
0
def edit(request, profile_id):
    profile = UserProfile.objects.get(id=profile_id)
    profile_form = UserProfileForm(instance=profile)
    if not request.user.is_authenticated():
        profile_form = UserProfileForm(instance=profile)
        return redirect("person:login")
# elif int(profile_id) != request.user.id:
#     return redirect("person:edit", request.user.id)
    else:
        if request.POST:
            profile_form = UserProfileForm(request.POST,
                                           request.FILES,
                                           instance=profile)
            if profile_form.is_valid():
                profile_form.save()
                return redirect("person:details", profile.id)
        return render(request, "edit.html", {
            'profile': profile,
            'profile_form': profile_form
        })