当我尝试在我的django项目中导入我的机场文件时,我得到以下内容:
INFO Downloading: airports.dat
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/airports/management/commands/airports.py", line 50, in handle
with open(self.download(), 'rt') as f:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/airports/management/commands/airports.py", line 64, in download
response.raise_for_status()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 909, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat
为什么会这样?非常感谢
答案 0 :(得分:0)
此修复程序是旁路:
def handle(self, *args, **options):
self.options = options
if self.options['flush'] is True:
self.flush_airports()
else:
columns = self.default_format.split(',')
columns = dict(list(zip(columns, itertools.count())))
with open("/home/<download_dir>/airports.dat", 'rt') as f:
# with open(self.download(), 'rt') as f:
self.stdout.flush()
最后,第158行的代码中存在一个小错误,在命令完成之前还需要修复(cities__in而不是city__in):
try:
c = qs.filter(cities__in=City.objects.filter(
location__distance_lte=(point, D(km=25))))[0] # second attempt
except KeyError:
c = None # shit happens
请注意,处理需要一段时间,但最后,我确实得到了我所追求的机场名单。