scrapy可以在设置中设置自定义变量吗?

时间:2015-02-12 07:53:20

标签: python django scrapy

我想在ENV中设置变量settings.py,如:

import sys, os
path_django_site = os.path.join(os.path.dirname(__file__), "../../")  
sys.path.append(path_django_site)
os.environ['DJANGO_SETTINGS_MODULE'] = 'pjt1.settings.dev'  
ENV = os.environ['DJANGO_SETTINGS_MODULE']

所以在我的蜘蛛项目中,我可以编写这样的代码:

if ENV =='pjt1.settings.dev':
    obj = Group.objects.filter(id__lte=10)
    print 'this is test'

elif ENV =='pjt.settings.prod':
    obj = Group.objects.all()
    print 'This is normal environvent'

它可以吗? 请指导我。谢谢。

1 个答案:

答案 0 :(得分:1)

我不确定我是否理解实际问题,但您可以在settings.py中设置任何变量并使用它:

# your wsgi file probably includes something like this
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pjt1.settings.dev")

# settings.py
ENV = os.environ['DJANGO_SETTINGS_MODULE']

# other files
from django.conf import settings
if settings.ENV == 'pjt1.settings.dev':
    ...