Issue
I've looked at the other SO threads about unrecognized argparse arguments, but none of them seem to match up with my situation.
Here's my argparse code, located at apps/create_pdf/management/commands/create_pdf.py
:
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
help = 'html to pdf for specified website.'
def add_arguments(self, parser):
print('cp#1')
parser = argparse.ArgumentParser(description='html to pdf for specified website')
parser.add_argument('-w', '--website', nargs =1, action = 'store', choices = ['SITE_1', 'SITE_2'], default='', help='site to be scraped.')
args = parser.parse_args()
print(args)
breakpoint()
def handle(self, *args, **options):
print('cp#2')
# create_pdf_from_html()
I run this from the terminal like so:
python3 manage.py create_pdf --website=SITE_1 --settings=server_config.settings
...and I'm getting this error:
manage.py: error: unrecognized arguments: create_pdf --settings=server_config.settings
The settings path is correct and if I'm not using argparse, everything runs fine.
What am I missing?
Solution
The correct way to add arguments to your command:
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
help = 'html to pdf for specified website.'
def add_arguments(self, parser):
parser.add_argument('-w', '--website', nargs =1, action = 'store', choices = ['SITE_1', 'SITE_2'], default='', help='site to be scraped.')
def handle(self, *args, **options):
print(options['website'])
# create_pdf_from_html()
Answered By - Oleksii Tambovtsev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.