将CSV导入到Django,并且无法识别设置 [英] Importing CSV to Django and settings not recognised

查看:183
本文介绍了将CSV导入到Django,并且无法识别设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我要掌握Django,或者试图。我有一些代码,不依赖于被网页调用 - 它的目的是用信息填充数据库。最终它将被设置为cron作业,以运行一夜。这是它的第一个裂缝,这是做一个初始人口(一旦我有工作,我会移动到一个添加结构,只有新的记录推送)。我使用Python 2.7,Django 1.5和Sqlite3 。当我运行这个代码,我得到

So i'm getting to grips with Django, or trying to. I have some code that isn't dependent on being called by the webpage - it's designed to populate the database with information. Eventually it will be set up as a cron job to run overnight. This is the first crack at it, which is to do an initial population (once I have that working, I'll move to an add structure, where only new records are pushed.) I'm using Python 2.7, Django 1.5 and Sqlite3. When I run this code, I get

请求设置DATABASES,但设置没有配置。您必须在访问设置之前定义环境变量DJANGO_SETTINGS_MODULE或调用settings.configure()。

这看起来很明显,花了几个小时现在试图找出如何调整该设置。如何调用/打开连接/什么正确的术语在这里?我有一些像这样的功能,将被安排的工作,这一直令我沮丧所有下午。

That seems fairly obvious, but I've spent a couple of hours now trying to work out how to adjust that setting. How do I call / open a connection / whatever the right terminology is here? I have a number of functions like this that will be scheduled jobs, and this has been frustrating me all afternoon.

import urllib2
import csv
import requests

from django.db import models
from gmbl.models import Match   

master_data_file = urllib2.urlopen("http://www.football-data.co.uk/mmz4281/1213/E0.csv", "GET")
data = list(tuple(rec) for rec in csv.reader(master_data_file, delimiter=','))

for row in data:
    current_match = Match(matchdate=row[1], 
        hometeam=row[2], 
        awayteam = row [3], 
        homegoals = row [4], 
        awaygoals = row[5],
        homeshots = row[10],
        awayshots = row[11],
        homeshotsontarget = row[12],
        awayshotsontarget = row[13],
        homecorners = row[16],
        awaycorners = row[17])
    current_match.save()

我最初开始使用 http:// django- csv-importer.readthedocs.org/en/latest/ 但我有同样的错误,而且文档没有什么意义,试图调试它。当我尝试在函数中调用settings.configure时,它说它不存在;大概是我不得不进口它,但不能做这项工作。

I had originally started out with http://django-csv-importer.readthedocs.org/en/latest/ but I had the same error, and the documentation doesn't make much sense trying to debug it. When I tried calling settings.configure in the function, it said it didn't exist; presumably I had to import it, but couldn't make that work.

推荐答案

确保Django和您的项目位于PYTHONPATH中,然后您可以:

Make sure Django, and your project are in PYTHONPATH then you can do:

import urllib2
import csv
import requests
from django.core.management import setup_environ
from django.db import models
from yoursite import settings

setup_environ(settings)

from gmbl.models import Match   

master_data_file = urllib2.urlopen("http://www.football-data.co.uk/mmz4281/1213/E0.csv", "GET")
data = list(tuple(rec) for rec in csv.reader(master_data_file, delimiter=','))

# ... your code ...

参考资料: http://www.b-list。 org / weblog / 2007 / sep / 22 / standalone-django-scripts /

希望它有帮助!

这篇关于将CSV导入到Django,并且无法识别设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆