0

This is a time critical bug otherwise I wouldn't have posted here and it's also my very first try with django and python so consider accordingly .

I am getting error

%o format: a number is required, not str

in my django app . The place where it shows error :(I am trying to create a message string)

msg_donor = 'Dear %s,\nThank you for contributing to %s \'s fundraising campaign 
on Milaap.org. You\'ve made our day.\nRemember, since this is a loan, and not 
donation, 100% of your money will come back to you!\nYou will shortly receive 
your milaap login details. You can check who your money has gone to and track 
your repayments through your account. Be sure to sign up and check your account
regularly for updates.\n\n%s' % (d.name, c.fundraiser_name, regardsStr)

I haven't written any %o in my application andI am wondering how this error can be produced ??

rtcoms
  • 783
  • 1
  • 8
  • 19

2 Answers2

1

You have 100% of your money in your string. % is a formatting character. Use 100%% of your money to put a literal % in there.

(I'm sort of surprised that Python skipped over the space between % and o, but whatever.)

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Thats actually interesting. I never realized it before but you are right. Its apparently legal to have any amount of whitespace in between. – jdi Feb 26 '12 at 06:20
  • the problem is something else I think, because even if I comment that line putting # before it even then it show the error in the same line. – rtcoms Feb 26 '12 at 06:30
  • Well, that means that you're not running the code you think you're running. Is Django caching something? Do you need to tell Django to reload your code? – Greg Hewgill Feb 26 '12 at 06:32
  • But in the error log it shows the # I put before that specific line – rtcoms Feb 26 '12 at 06:37
  • 1
    Yes, because when an error is encountered, Python goes back and finds the source file at the line where the error happened, and writes that source line to the error message. However, Python doesn't know that you changed the source file, it's still running the original code (which is still loaded into your web server or Django or something). – Greg Hewgill Feb 26 '12 at 06:43
  • sudo /etc/init.d/apache2 restart ... did the trick . Thanks for quick response. – rtcoms Feb 26 '12 at 06:48
0

The problem is being cause by a stray % in your string at: 100%

When you do string formatting, you have to make sure to escape literal % by doing %%

Try this:

msg_donor = """Dear %s,\nThank you for contributing to %s's fundraising campaign 
on Milaap.org. You've made our day.\nRemember, since this is a loan, and not 
donation, 100%% of your money will come back to you!\nYou will shortly receive 
your milaap login details. You can check who your money has gone to and track 
your repayments through your account. Be sure to sign up and check your account
regularly for updates.\n\n%s""" % (d.name, c.fundraiser_name, regardsStr)
jdi
  • 90,542
  • 19
  • 167
  • 203
  • the problem is something else I think, because even if I comment that line putting # before it even then it show the error in the same line. – rtcoms Feb 26 '12 at 06:31
  • Then you haven't posted enough code, because that error was being raised by your example, and was fixed by both mine and @Greg's answers – jdi Feb 26 '12 at 06:35