Exemplo n.º 1
0
def cadastro(request):
    form = UserModelForm(request.POST or None)
    context = {'form': form}
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            return redirect('home')
    return render(request, 'users/cadastro.html', context)
Exemplo n.º 2
0
def create_user(request):
    form = UserModelForm(request.POST or None)

    if request.method == 'POST':
        if form.is_valid():
            form.save()
            return redirect('/')
    return render(request, 'register.html', {'form': form})
Exemplo n.º 3
0
def update_user(request, id):
    user = User.objects.get(id=id)
    form = UserModelForm(request.POST or None, instance=user)

    if form.is_valid():
        form.save()
        return redirect('/books')

    return render(request, 'users-form.html', {'form': form, 'user': user})
Exemplo n.º 4
0
def register_users(request):
    form = UserModelForm
    if request.method == 'POST':
        form = UserModelForm(request.POST)

        if form.is_valid():
            form.save(commit=True)
            return users(request)
        else:
            print('Error, Form is Invalid')

    return render(request, 'users/users_register.html', {'form': form})
Exemplo n.º 5
0
def edit_user(request):
    user = request.user
    user_form = UserModelForm(instance=user)
    profile_inline_formset = inlineformset_factory(User,
                                                   UserProfile,
                                                   fields=('bio', 'photo'),
                                                   can_delete=False,
                                                   form=UserProfileModelForm)
    formset = profile_inline_formset(instance=user)

    if request.user.is_authenticated():
        message = ""
        if request.method == "POST":
            user_form = UserModelForm(request.POST,
                                      request.FILES,
                                      instance=user)
            formset = profile_inline_formset(request.POST,
                                             request.FILES,
                                             instance=user)
            message = _(
                u'😭 Ha ocurrido un error al guardar la información. Inténtalo mas tarde. '
            )
            if user_form.is_valid():
                created_user = user_form.save(commit=False)
                formset = profile_inline_formset(request.POST,
                                                 request.FILES,
                                                 instance=created_user)
                if formset.is_valid():
                    if user_form.save():
                        if formset.save():
                            message = _(u'Información actualizada.')
                            return HttpResponseRedirect(
                                reverse('user_profile', args=[user]))

        return render(request, "users/update.html", {
            "form": user_form,
            "formset": formset,
            "message": message,
        })
    else:
        raise PermissionDenied
Exemplo n.º 6
0
def center(request, pk):
    """
    用户个人中心,建议自定义
    :param request:
    :param pk:
    :return:
    """
    obj = models.UserInfo.objects.filter(id=pk).first()
    if not obj:
        return render(request, '404.html')
    if request.method == 'GET':
        form = UserModelForm(instance=obj)
        return render(request, 'users/personal_center.html', {'form': form})

    form = UserModelForm(instance=obj, data=request.POST)
    if form.is_valid():
        userobj = form.save()
        return redirect(reverse('users:center', kwargs={'pk': userobj.pk}))

    return render(request, 'users/personal_center.html', {'form': form})