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})
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)
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})
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})
def do_login(request): form = UserModelForm(request.POST or None) error = '' if request.method == 'POST': user = authenticate(username=request.POST['username'], password=request.POST['password']) if user is not None: if user.is_active: try: login(request, user) return redirect('/product') except: error = 'Internal error' else: error = 'Inactive user' else: error = 'Invalid username or password!' return render(request, 'login.html', {'form': form, 'error': error})
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
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})