def signup(request): if request.method == 'POST': form = forms.UserForm(request.POST) if form.is_valid(): form.save() return redirect('home') else: return render(request, 'signup.html', {'form': form}) else: form = forms.UserForm() return render(request, 'signup.html', {'form': form})
def users(user_id=None): # TODO(hop): change way to log in # TODO(hop): add info about admin user in settings if not g.user.is_admin: return render_template('404.html') form = forms.UserForm() if user_id is not None: if form.validate_on_submit(): # edit user setting db.session.query(models.User).filter_by(id=user_id).update({ 'state': form.is_active.data, 'role': form.is_admin.data }) db.session.commit() return redirect('/users') else: # show user setting user = models.User.query.get(user_id) if user: form.is_active.data = user.is_active form.is_admin.data = user.is_admin return render_template('users_details.html', user=user, form=form) else: user_list = models.User.query.all() return render_template('users.html', user_list=user_list, form=form)
def regsiter(request): if request.method == 'POST' and request.FILES: userform = forms.UserForm(request.POST) profileform = forms.ProfileForm(request.POST, request.FILES) if userform.is_valid() and profileform.is_valid(): user = userform.save(commit=False) user.set_password(userform.cleaned_data["password"]) user.save() profile = profileform.save(commit=False) profile.user = user profile.save() userform = forms.UserForm() profileform = forms.ProfileForm() return render(request, 'register.htm', context={ 'user': userform, 'profile': profileform })
def register(request): register = False if request.method == "POST": user_form = forms.AppointmentForm(request.POST) user_data_form = forms.UserForm(request.POST, request.FILES) if user_form.is_valid() and user_data_form.is_valid(): user = user_form.save(commit=True) user.set_password(user.password) user.save() user_data = user_data_form.save(commit=False) user_data.user = user if 'profile_pic' in request.FILES: user_data.profile_pic = request.FILES['profile_pic'] user_data.save() register = True else: user_form = forms.AppointmentForm() user_data_form = forms.UserForm() d = {'form': user_data_form, 'form_user': user_form, 'register': register} return render(request, 'reg.html', context=d)
def update(id) : action = 'update' user = models.User.query.get_or_404(id) form = forms.UserForm(obj=user) if form.validate_on_submit(): user.username = form.username.data user.email = form.email.data try: db.session.commit() flash('Edit success', 'success') return redirect(url_for('list')) except: flash('Error : Username already exists', 'error') form.username.data = user.username form.email.data = user.email return render_template('user/add_user.html', form= form, action = action)
def store(): action = 'store' form = forms.UserForm() if form.validate_on_submit(): user = models.User( username = form.username.data, email = form.email.data ) try: db.session.add(user) db.session.commit() flash('Create success', 'success') return redirect(url_for('list')) except: flash('Error : Username already exists', 'error') return render_template("user/add_user.html", form = form, action = action)
def user(request): ret={} if request.is_ajax(): userForm = forms.UserForm(request.POST) # username=request.POST.get('username') # telphone=request.POST.get('telphone') # email=request.POST.get('email') # avatar=request.FILES.get('avatar') if userForm.is_valid(): username=userForm.cleaned_data.get('username') # extra = {} # if avatar_obj: # extra['avatar'] = avatar_obj #models.User.objects.filter(username=username).update(**userForm.cleaned_data,**extra) ##更新数据 models.User.objects.filter(username=username).update(**userForm.cleaned_data) avatar_obj = request.FILES.get('avatar') if avatar_obj: user_obj = models.User.objects.filter(username=username).first() user_obj.avatar=avatar_obj user_obj.save() ret['status'] = 0 else: ##错误信息 errors=userForm.errors.get_json_data() error={} for key, message_dicts in errors.items(): messages = [] for message in message_dicts: messages.append(message['message']) error[key] = messages ret['status'] = 1 ret['error'] = error return JsonResponse(ret)