TypeError receive_money()只需要3个参数(给定1个)-Django

时间:2016-12-16 11:06:05

标签: django django-views django-urls

我正在尝试将2个参数传递给视图,但它会出现此类型错误 我不知道它是否与网址或我的重定向有问题

// urls.py

urlpatterns = [
    #
    url(r'^receive/[a-zA-Z]+/[0-9]+/$', receive_money)
]

// subtract_money view

def subtract_money(request):

    if request.user:
        users = User.objects.all()
        users_ids = users.values_list('id', flat=True)
        users_list = []
        for id in users_ids:
            user = users.get(pk=id)
            if user.username != "ravinkohli" and user.username != request.user.username:
                users_list.append(user)
        if request.POST and request.POST.get('amount'):
            username = request.user.username
            withdraw = request.POST.get('amount')
            wallet = Wallet.objects.get(pk=request.user.userprofile.wallet_id_id)
            # if withdraw > wallet.amount:
            #     return render(request, 'send_money.html', {'error': 'Amount can not be greater than balance','users': users_list})
            wallet.subtract_money(withdraw)
            wallet.save()
            now = datetime.now()
            trans = Transaction(from_name=username, wallet_id=wallet,to=request.POST.get('receiver'), date=now, amount=withdraw)
            trans.save()
            return redirect('/receive/%s/%s/' % (request.POST.get('receiver'), withdraw))
        else:
            return render(request, 'send_money.html',{'users': users_list})
    else:
        return HttpResponseRedirect('/login/?next={}'.format('/subtract_money/'))

//接收者视图

def receive_money(request, username, amount):
    add_amount = amount
    wallet = Wallet.objects.get(username=username)
    wallet.add_money(add_amount)
    wallet.save()
    return redirect('user_profile.html', {'user': request.user,'userprofile': Userprofile.objects.get(user=request.user), 'wallet': wallet})

2 个答案:

答案 0 :(得分:3)

因为你期待2个参数,所以它应该是

url(r'^receive/(?P<username>[a-zA-Z]+)/(?P<amount>[0-9]+)/$', receive_money)

答案 1 :(得分:1)

尝试使用以下网址,但我不认为构建这样的网址是个好主意。

urlpatterns = [
    #
    url(r'^receive/(?P<username>[a-zA-Z]+)/(?P<amount>[0-9]+)/$', receive_money)
]