0

I'm having a bit of trouble using str.format(*args, **kwargs):

import datetime
from django.conf import settings
from django.core.management.base import BaseCommand
from django.core.mail import send_mass_mail
from django.contrib.auth.models import User
from django.db.models import Count


class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument('--days', dest='days', type=int)

    def handle(self, *args, **options):
        emails = []
        subject = 'Enroll in a course'
        date_joined = datetime.date.today() - datetime.timedelta(days=options['days'])
        users = User.objects.annotate(course_count=Count('courses_joined'))
                    .filter(course_count=0, date_joined__lte=date_joined)
        for user in users:
            message = 'Dear {},\n\n What are you waiting for?'.format(user.first_name)
            emails.append((subject, message, settings.DEFAULT_FROM_EMAIL, [user.email]))
        send_mass_mail(emails)
        countEmails = len(emails)
        if countEmails:
            # self.stdout.write(countEmails)
            messageConsole = 'Sent {} reminders.' % format(int(countEmails))
            self.stdout.write(messageConsole)

Run command "python manage.py enroll_reminder --days=1", then the errro comes:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/polar/.pyenv/versions/my_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/home/polar/.pyenv/versions/my_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/polar/.pyenv/versions/my_env/lib/python3.5/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/polar/.pyenv/versions/my_env/lib/python3.5/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/home/polar/PythonLab/DjangoByExample/educa/students/management/commands/enroll_reminder.py", line 30, in handle
    messageConsole = 'Sent {} reminders.\n' % format(int(countEmails))
TypeError: not all arguments converted during string formatting

when I uncomment "self.stdout.write(countEmails)", there comes another problem:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/polar/.pyenv/versions/my_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/home/polar/.pyenv/versions/my_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/polar/.pyenv/versions/my_env/lib/python3.5/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/polar/.pyenv/versions/my_env/lib/python3.5/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/home/polar/PythonLab/DjangoByExample/educa/students/management/commands/enroll_reminder.py", line 29, in handle
    self.stdout.write(countEmails)
  File "/home/polar/.pyenv/versions/my_env/lib/python3.5/site-packages/django/core/management/base.py", line 111, in write
    if ending and not msg.endswith(ending):
AttributeError: 'int' object has no attribute 'endswith'

Can anyone help show me what I am doing wrong?

Thank you!

1
  • 1
    How silly I am for this stupid question......shame on me. Commented Jul 8, 2016 at 13:10

2 Answers 2

3

You're confused between the old C-style string substitution and the new format method. Either do:

'Sent {} reminders.'.format(int(countEmails))

or

'Sent %s reminders.' % (int(countEmails),)
Sign up to request clarification or add additional context in comments.

1 Comment

This is the second time that you helped me, thank you!
3

You are confusing % and .format() formatting

You could do either:

'Sent {} reminders'.format(countEmails)

or

'Sent %d reminders.' % countEmails

Note that countEmails is already an integer, there is no need to call int(countEmails).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.