Hello!!!
I want max_value to be checked in the form after entering, not through a static value, but through a function, in this case check_balance()
forms.py
class WwmoneyForm(forms.Form):
amount = forms.FloatField(min_value=1, max_value=check_balance(), label = "Amount")
models.py
def check_balance(request):
name = request.user.username
all_users = All_users.objects.all()
for i in all_users:
if i.nick == name:
balance = i.balance
return balance
when executed , it outputs the following:
TypeError: check_balance() missing 1 required positional argument: ârequestâ
You have a couple things wrong here:
Your function definition:
def check_balance(request):
^^^^^^^
Your usage in the form:
max_value=check_balance()
^^
Your form doesnât normally get the request passed to it, so it canât pass the request to the check_balance
method.
(Weâd need to see the view and the model to be able to provide more detailed information about what the appropriate resolution would be.)
Second item:
It is an extremely bad idea to try and use a FloatField
for any type of monetary value. You want to use a Decimal field for that purpose. See my comment at Calculating Cost of Items for Finance - #2 by KenWhitesell and the posts and links that it references.
Third, do yourself a favor and change your check_balance
function to perform a query rather than iterating over all users to find a match. (Also, what is the All_users
model?)
1 Like
THANK YOU!
views.py
I did not prescribe anything for their functions in âviewâ, I thought that it was not necessary
models.py
class Wwmoney(models.Model):
nick = models.CharField('Nick', max_length=50)
amount = models.FloatField()
![:handshake: :handshake:](https://emoji.discourse-cdn.com/twitter/handshake.png?v=12)
class All_users(models.Model):
nick = models.CharField('Nick', max_length=50)
account = models.CharField('Acc', max_length=50)
email = models.EmailField("Email", max_length=50)
balance = models.FloatField()
A form doesnât initiate any activity by itself. Forms are used by views to process data being submitted from a browser.
yes, you are right
def wwmoney(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = WwmoneyForm(request.POST) # A form bound to the POST data
if form.is_valid():
name = request.user.username
cd = form.cleaned_data
pc = Wwmoney(amount = cd['amount'], datetime_addmoney = dtime(), method = "1", nick = name)
all_users = All_users.objects.all()
for i in all_users:
if i.nick == name:
bal = i.balance
balance1 = i.balance - cd['amount']
balance1 = All_users(id = i.id, nick = i.nick, account = i.account, email = i.email, balance = balance1)
else:
pass
balance1.save()
pc.save()
return HttpResponseRedirect('/list_users')
# if a GET (or any other method) we'll create a blank form
else:
form = WwmoneyForm()
all_users = All_users.objects.all()
name1 = request.user.username
for k in all_users:
if k.nick == name1:
bal = k.balance
return render(request, 'money/wwmoney.html', {'form': form, "all_users": all_users, "bal":bal})
Just out of curiousity, have you worked your way through either the Official Django Tutorial or the Django Girls Tutorial? There are a number of fundamental mistakes in the code here with both the view and the handling of data in the form.
If you havenât worked your way through those tutorials, I suggest you do so before trying to move forward from here.
If you have, then I suggest you go back and review the work you had done with an eye toward understanding how those views, forms, and queries are constructed.
1 Like