コード例 #1
0
ファイル: views.py プロジェクト: NazmulHaque/books
def update_basic_info_first_time(request):
    try:
        user_profile = UserProfile.objects.get(user=request.user)
    except ObjectDoesNotExist:
        context = {}
        context.update(csrf(request))
        form = UserBasicInfoForm()
        context['form'] = form

        if request.user.is_authenticated():
            context['login_mode'] = True
        if request.method == 'POST':
            form = UserBasicInfoForm(data=request.POST, instance=request.user.profile)
            if form.is_valid():
                form.save()
                dict = {'submit': True, 'username': request.user.username}

                data = json.dumps(dict, cls=JSONEncoder)
                return HttpResponse(data, content_type="application/json")
            else:
                errors_dict = {}
                if form.errors:
                    for error in form.errors:
                        e = form.errors[error]
                        errors_dict[error] = unicode(e)
                data = json.dumps(errors_dict, cls=JSONEncoder)
                return HttpResponse(data, content_type="application/json")
        return render_to_response('user-profile/basic-info-first-time-form.html', context)
    else:
        return redirect('/user/' + request.user.username)
コード例 #2
0
ファイル: views.py プロジェクト: NazmulHaque/books
def update_basic_info(request):
    context = {}
    context.update(csrf(request))
    form = UserBasicInfoForm()
    context['form'] = form

    if request.method == 'POST':
        form = UserBasicInfoForm(data=request.POST, instance=request.user.profile)
        if form.is_valid():
            form.save()
            return HttpResponse('true')
        else:
            errors_dict = {}
            if form.errors:
                for error in form.errors:
                    e = form.errors[error]
                    errors_dict[error] = unicode(e)
            data = json.dumps(errors_dict, cls=JSONEncoder)
            return HttpResponse(data, content_type="application/json")

    if UserProfile.objects.filter(user=request.user).count():
        context['user_profile'] = UserProfile.objects.get(user=request.user)

    return render_to_response('user-profile/basic-info-form.html', context)