Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can scrapy setting custom variable in settings?

I want to set a variable ENV in settings.py like:

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']

So that in my spider project,I can write code like this:

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'

is it posible? Please guide me. Thank you.

like image 635
user2492364 Avatar asked Mar 02 '26 02:03

user2492364


1 Answers

I'm not sure I understand the actual question, but you can set any variable in your settings.py and use it like this:

# 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':
    ...
like image 120
inejc Avatar answered Mar 04 '26 15:03

inejc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!