我在使用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)
运行命令“python manage.py enroll_reminder --days = 1”, 那么错误来了:
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
当我取消注释“self.stdout.write(countEmails)”时,又出现了另一个问题:
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'
任何人都可以帮我看看我做错了吗?
谢谢!
答案 0 :(得分:3)
您会混淆%
和.format()
格式
您可以这样做:
'Sent {} reminders'.format(countEmails)
或
'Sent %d reminders.' % countEmails
请注意countEmails
已经是整数,无需拨打int(countEmails)
。
答案 1 :(得分:3)
您在旧的C风格字符串替换和新格式方法之间感到困惑。要么:
'Sent {} reminders.'.format(int(countEmails))
或
'Sent %s reminders.' % (int(countEmails),)