示例#1
0
def address(request):
    if request.method == 'GET':
        user = request.user
        # 获取用户的收货地址信息
        user_addresses = UserAddress.objects.filter(user=user).order_by('-id')
        return render(request, 'user_center_site.html',
                      {'user_addresses': user_addresses})

    if request.method == 'POST':
        # 使用表单验证,验证收货地址的参数是否填写完整
        form = UserAddressForm(request.POST)
        if form.is_valid():
            user = request.user
            address_info = form.cleaned_data
            # 保存收货地址信息
            UserAddress.objects.create(**address_info, user=user)
            # 保存成功收货地址
            return HttpResponseRedirect(reverse('user:user_address'))
        else:
            user = request.user
            # 获取用户的收货地址信息
            user_addresses = UserAddress.objects.filter(
                user=user).order_by('-id')
            return render(request, 'user_center_site.html', {
                'form': form,
                'user_addresses': user_addresses
            })
示例#2
0
def edit_address_action(request):
    # no user address situation
    if UserLocation.objects.filter(user=request.user).count() == 0:
        print("hihi")
        return redirect(reverse('myaddress'))

    user = request.user
    address = UserLocation.objects.get(user=user)
    try:
        if request.method == "GET":
            print("Get edit address page")
            form = UserAddressForm(instance=address)
            context = {
                'form': form,
                'address': address,
            }
            return render(request, 'edit-address.html', context)

        edit_address = UserLocation.objects.select_for_update().get(user=user)
        form = UserAddressForm(request.POST, instance=address)
        if not form.is_valid():
            context = {'form': form, 'address': address}
            return render(request, 'edit-address.html', context)

        form.save()
        address = Address.objects.get(user=user)
        context = {'form': form}
        return redirect(reverse("myaddress"))
    except:
        context = {'form': form, 'address': address}
        return redirect(reverse('myprofile'))
示例#3
0
def user_site(request):
    if request.method == 'GET':
        user_id = request.session['user_id']
        user_address = UserAddress.objects.filter(user_id=user_id)
        return render(request, 'user_center_site.html',
                      {'user_address': user_address})

    if request.method == 'POST':
        # 使用表单做校验
        form = UserAddressForm(request.POST)
        if form.is_valid():
            user_id = request.session['user_id']
            address = form.cleaned_data['address']
            signer_name = form.cleaned_data['signer_name']
            signer_postcode = form.cleaned_data['postcode']
            signer_mobile = form.cleaned_data['mobile']
            UserAddress.objects.create(user_id=user_id,
                                       address=address,
                                       signer_name=signer_name,
                                       signer_postcode=signer_postcode,
                                       signer_mobile=signer_mobile)
            return HttpResponseRedirect(reverse('user:user_site'))
        else:
            errors = form.errors
            return render(request, 'user_center_site.html', {'errors': errors})
示例#4
0
def user_site(request):
    if request.method == 'GET':
        return render(request, 'user_center_site.html')
    if request.method == 'POST':
        form = UserAddressForm(request.POST)
        if form.is_valid():
            user_id = request.session['user_id']
            addr = form.cleaned_data['addr_name']
            name = form.cleaned_data['site_name']
            email = form.cleaned_data['email_name']
            tel = form.cleaned_data['tel_name']
            UserAddress.objects.create(user_id=user_id,
                                       address=addr,
                                       signer_name=name,
                                       signer_postcode=email,
                                       signer_mobile=tel)
            return HttpResponseRedirect(reverse('user:user_site'))
        else:
            errors = form.errors
            return render(request, 'user_center_site.html', {'errors': errors})
示例#5
0
def create_address_action(request):
    if request.method == "GET":
        return render(request, 'create-address.html')

    # one user limit to one address information
    if UserLocation.objects.filter(user=request.user).count() > 0:
        redirect('/myaddress')

    address1 = request.POST.get("address1")
    address2 = request.POST.get("address2")
    city, state = request.POST.get('city'), request.POST.get('state'),
    zip_code = request.POST.get('zip_code')

    userLocation = UserLocation(user=request.user, address1=address1, address2=address2,
                                city=city, state=state, zip_code=zip_code)
    create_address_form = UserAddressForm(request.POST, instance=userLocation)
    if not create_address_form.is_valid():
        print("User create address form is invalid ...")
        context = {'form': create_address_form}
        return render(request, 'create-address.html', context)
    print('User create address form is valid.')

    create_address_form.save()
    return redirect(reverse('myprofile'))