How do you configure Django for simple development and deployment?

后端 未结 14 1442
闹比i
闹比i 2020-11-30 16:14

I tend to use SQLite when doing Django development, but on a live server something more robust is often needed (MySQL/PostgreSQL, for example). Invariably, there are other c

相关标签:
14条回答
  • In addition to the multiple settings files mentioned by Jim, I also tend to place two settings into my settings.py file at the top BASE_DIR and BASE_URL set to the path of the code and the URL to the base of the site, all other settings are modified to append themselves to these.

    BASE_DIR = "/home/sean/myapp/" e.g. MEDIA_ROOT = "%smedia/" % BASEDIR

    So when moving the project I only have to edit these settings and not search the whole file.

    I would also recommend looking at fabric and Capistrano (Ruby tool, but it can be used to deploy Django applications) which facilitate automation of remote deployment.

    0 讨论(0)
  • 2020-11-30 16:50

    So many complicated answers!

    Every settings.py file comes with :

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    

    I use that directory to set the DEBUG variable like this (reaplace with the directoy where your dev code is):

    DEBUG=False
    if(BASE_DIR=="/path/to/my/dev/dir"):
        DEBUG = True
    

    Then, every time the settings.py file is moved, DEBUG will be False and it's your production environment.

    Every time you need different settings than the ones in your dev environment just use:

    if(DEBUG):
        #Debug setting
    else:
        #Release setting
    
    0 讨论(0)
  • 2020-11-30 16:51

    I use environment:

    if os.environ.get('WEB_MODE', None) == 'production' :
       from settings_production import *
    else :
       from settings_dev import *
    

    I believe this is a much better approach, because eventually you need special settings for your test environment, and you can easily add it to this condition.

    0 讨论(0)
  • 2020-11-30 16:52

    Personally, I use a single settings.py for the project, I just have it look up the hostname it's on (my development machines have hostnames that start with "gabriel" so I just have this:

    import socket
    if socket.gethostname().startswith('gabriel'):
        LIVEHOST = False
    else: 
        LIVEHOST = True
    

    then in other parts I have things like:

    if LIVEHOST:
        DEBUG = False
        PREPEND_WWW = True
        MEDIA_URL = 'http://static1.grsites.com/'
    else:
        DEBUG = True
        PREPEND_WWW = False
        MEDIA_URL = 'http://localhost:8000/static/'
    

    and so on. A little bit less readable, but it works fine and saves having to juggle multiple settings files.

    0 讨论(0)
  • 2020-11-30 16:55

    I think it depends on the size of the site as to whether you need to step up from using SQLite, I've successfully used SQLite on several smaller live sites and it runs great.

    0 讨论(0)
  • 2020-11-30 16:55

    In fact you should probably consider having the same (or almost the same) configs for your development and production environment. Otherwise, situations like "Hey, it works on my machine" will happen from time to time.

    So in order to automate your deployment and eliminate those WOMM issues, just use Docker.

    0 讨论(0)
提交回复
热议问题