[ Pobierz całość w formacie PDF ]
.slideshare.net/jacobian/the-best-and-worst-of-django).Instead of having one settings.py le, with this setup you have a settings/ directory containing yoursettings les.is directory will typically contain something like the following:E.settings/__init__.pybase.pylocal.pystaging.pytest.pyproduction.py29 Chapter 5: Settings and Requirements FilesWARNING: Requirements + SettingsEach settings module should have its own corresponding requirements le.We ll cover thisat the end of this chapter in section 5.5,  Using Multiple Requirements Files.Settings file Purposebase.py Settings common to all instances of the project.This is the settings file that you use when you're working on the project locally.Locallocal.py development-specific settings includeDEBUGmode, log level, and activation of developertools like django-debug-toolbar.Developers sometimes name this file dev.py.Staging version for running a semi-private version of the site on a production server.This isstaging.pywhere managers and clients should be looking before your work is moved to production.Settings for running tests including test runners, in-memory database definitions, and logtest.pysettings.This is the settings file used by your live production server(s).That is, the server(s) that hostproduction.py the real live website.This file contains production-level settings only.It is sometimes calledprod.py.Table 5.1: Settings les and their purposeTIP: Multiple Files with Continuous Integration ServersYou ll also want to have a ci.py module containing that server s settings.Similarly, if it s a largeproject and you have other special-purpose servers, you might have custom settings les foreach of them.Let s take a look at how to use the shell and runserver management commands with this setup.You ll have to use the--settingscommand line option, so you ll be entering the following at thecommand-line.To start the Python interactive interpreter with Django, using your settings/local.py settings le:30 5.2: Using Multiple Settings FilesE.python manage.py shell --settings=twoscoops.settings.localTo run the local development server with your settings/local.py settings le:E.python manage.py runserver --settings=twoscoops.settings.localTIP: DJANGO SETTINGS MODULEA great alternative to using the--settingscommand line option everywhere is to settheDJANGOSETTINGSMODULEenvironment variable to your desired settings module path.You d have to setDJANGOSETTINGSMODULEto the corresponding settings module for eachenvironment, of course.For the settings setup that we just described, here are the values to use with the--settingscom-mand line option or theDJANGOSETTINGSMODULEenvironment variable:Option To Use With--settings(orEnvironmentDJANGOSETTINGSMODULEvalue)Your local development server twoscoops.settings.localYour staging server twoscoops.settings.stagingtwoscoops.settings.testYour test serverYour production server twoscoops.settings.productionTable 5.2: Setting DJANGO SETTINGS MODULE per location31 Chapter 5: Settings and Requirements FilesTIP: Using django-admin.py instead of manage.pye official Django documentation says that you should use django-admin.py rather than manage.py when working with multiple settings les:https://docs.djangoproject.com/en/1.5/ref/django-admin/at being said, if you re struggling with django-admin.py, it s perfectly okay to develop andlaunch your site running it with manage.py.5.2.1 A Development Settings ExampleAs mentioned earlier, we need settings con gured for development, such as setting the email hostto localhost, setting the project to run inDEBUGmode, and setting other con guration options thatare used solely for development purposes.We place development settings like the following intosettings/local.py :E.# settings/local.pyfrom.base import *DEBUG = TrueTEMPLATE_DEBUG = DEBUGEMAIL_HOST = "localhost"EMAIL_PORT = 1025DATABASES = {"default": {"ENGINE": "django.db.backends.postgresql_psycopg2","NAME": "twoscoops","USER": "","PASSWORD": "","HOST": "localhost","PORT": "",}32 5.2: Using Multiple Settings Files}INSTALLED_APPS += ("debug_toolbar", )INTERNAL_IPS = ("127.1",)MIDDLEWARE_CLASSES += \("debug_toolbar.middleware.DebugToolbarMiddleware", )Now try it out at the command line with:E.python manage.py runserver --settings=twoscoops.settings.localOpenhttp://127.1:8000and enjoy your development settings, ready to go into versioncontrol! You and other developers will be sharing the same development settings les, which forshared projects, is awesome.Yet there s another advantage: No more  if DEBUG or  if not DEBUG logic to copy/paste aroundbetween projects.Settings just got a whole lot simpler!At this point we want to take a moment to note that Django settings les are the single, solitaryplace we advocate usingimport *.e reason is that for the singular case of Django setting moduleswe want to override all the namespaces.5.2.2 Multiple Development SettingsSometimes we re working on a large project where different developers need different settings, andsharing the same dev.py settings le with teammates won t do.Well, it s still better tracking these settings in version control than relying on everyone customizingthe same dev.py module to their own tastes.A nice way to do this is with multiple dev settings les,e.g.dev audreyr.py and dev pydanny.py:33 Chapter 5: Settings and Requirements FilesE.# settings/dev_pydanny.pyfrom.local import *# Set short cache timeoutCACHE_TIMEOUT = 30Why? It s not only good to keep all your own settings les in version control, but it s also good to beable to see your teammates dev settings les [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • drakonia.opx.pl
  • Linki