Ravin Kohli
Ravin Kohli

Reputation: 153

TypeError receive_money() takes exactly 3 arguments (1 given)-Django

I am trying to pass 2 parameters to a view but it gives this type error i dont know if its a problem with the urls or my redirect

//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/'))

//receiver view

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})

Upvotes: 0

Views: 292

Answers (2)

e4c5
e4c5

Reputation: 53734

Since you are expecting 2 arguments it should be

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

Upvotes: 3

shady
shady

Reputation: 455

try something with the following url, but I don't think its a good idea to construct a url like this.

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

Upvotes: 1

Related Questions