Пример #1
0
def useradd(request):
    style = 'home'
    username = request.user.first_name
    u = request.user
    if request.method == 'POST':
        form = UserForm(request.POST)
        if form.is_valid():
            if u.has_perm("auth.add_user", "auth.user"):
                form.save()
                form.save_m2m()
                return HttpResponse(json.dumps({"code": 1}))
            else:
                auth = u'没有添加用户权限'
                return HttpResponse(json.dumps({"code": 0, "message": {"auth": auth}}))
        else:
            return HttpResponse(json.dumps({"code": 0, "message": {"username": form['username'].errors,
                                                                   "first_name": form['first_name'].errors,
                                                                   "password": form['password'].errors,
                                                                   "email": form['email'].errors,
                                                                   "last_name": form['last_name'].errors}}))
    else:
        form = UserForm()
        return render(request, "myprofile/useradd.html", {'form': form, 'index': style, 'username': username})
Пример #2
0
def useredit(request, id):
    style = 'home'
    username = request.user.first_name
    user = User.objects.get(id=id)
    u = request.user
    if request.method == 'POST':
        form = UserForm(request.POST, instance=user)
        if form.is_valid():
            if u.has_perm("auth.change_user", "auth.user"):
                form.save()
                form.save_m2m()
                return HttpResponse(json.dumps({"code": 1}))
            else:
                auth = u'没有修改用户权限'
                return HttpResponse(json.dumps({"code": 0, "message": {"auth": auth}}))
        else:
            return HttpResponse(json.dumps({"code": 0, "message": {"username": form['username'].errors,
                                                                   "first_name": form['first_name'].errors,
                                                                   "password": form['password'].errors,
                                                                   "email": form['email'].errors,
                                                                   "last_name": form['last_name'].errors}}))
    else:
        form = UserForm(instance=user)
        return render(request, "myprofile/useredit.html", {'form': form, 'index': style, 'username': username})