Summary:
Say I have a project in Django called "devsite" which will be deployed first to a staging project (also called "devsite") and finally to the live codebase (where the project is called "livesite"). During live deployments, I'd have to make manual changes to urls.py in order to import views from the right project. Which means urls.py in "devsite" would use something like:
from devsite import views
And urls.py for "livesite" would be changed to:
from livesite import views
My Solution:
The following seems to work (with limited testing so far). What I've done is create a variable in settings.py to get the project name from the directory, like so:
settings.py
# /settings.py import os.path PROJECT_NAME = os.path.basename(os.path.dirname(__file__))
And then use this to import the correct views in urls.py:
urls.py
# /urls.py from django.conf import settings website = __import__('%s' % settings.PROJECT_NAME, fromlist=['views']) ... urlpatterns = patterns('', (r'^monty/$', website.views.monty), )
My Question:
What I'd like to know is:
- Is this a good way of doing what I want to do, or is there a better way to code this?
- Or do I need to rethink my whole deployment workflow?
Thanks in advance.
score:0
I would question why you have different projects for your live and dev sites. Why not keep all differences down to settings/configuration (as you do with PROJECT_NAME
, for example) but keep your projects common? It seems you only increase any chances for error between dev and live the more you make each site diffferent.
Other than that, I think what you're doing is more or less fine. The other pattern I've seen is something like:
try:
from livesite import views
except ImportError:
from devsite import views
score:1
Have a look at zc.buildout and djangorecipe - which, as Jacob Kaplan-Moss puts it,
...is an exceedingly civilized way to develop an app
http://jacobian.org/writing/django-apps-with-buildout/
Apart from general 'civilizedness", djangorecipe allows you to have more than one settings file, which can import from each other. Then you can specify which settings file to use in a particular installation.
development.py
from settings import *
DEBUG = True
live.py
from settings import *
DEBUG = False
... and that's only the tip of the iceberg
Credit To: stackoverflow.com
Related Query
- With Django, is there a better way to dynamically import views in urls.py?
- Is there a way to auto-increment a Django field with respect to a foreign key?
- Is there a way to generate pdf containing non-ascii symbols with pisa from django template?
- Is there a way to integrate Django with Next.js?
- A better way to import AUTH_USER_MODEL in Django 1.5
- Is there a way to dynamically render a hidden version of a django field in a template?
- In Django is there a way to aggregate over relationships with a condition on the related object
- Is there a way to augment django QuerySets with extra attributes?
- Is there a way to get graphene to work with django GenericRelation field?
- Is there a way to install django with pip to point to a specific version of python in virtualenv
- Is it better to use GenericAPIView or basic APIView while writing class based views (CBV) with Django Rest Framework (DRF)
- Is there any way to get the list of names of views having a decorator, Python, Django
- Django ManyToMany relationship with abstract base - not possible, but is there a better way?
- Isn't there any way to select attributes from multiple tables by non-pk-column with hitting DB once in Django model?
- Is there a way to configure Apache to auto-recognize URLs for Django vs React without hard-coding each endpoint?
- Is there a way of creation sessions in django via class based views after posting data?
- Is there a way for Django to accept naive datetimes with USE_TZ=True, or to suppress warnings?
- With all the easy Django utilities, tools, configs, settings, etc. is there REALLY no easy way to define the name of a custom App in Admin?
- Is there a way in Django to make it so that when a form with initial text is clicked the text disappears?
- Is there a better way to create a Django REST Web Service?
- Using Django with Python - Views is not defined when trying to import
- Is there a way to block django views from serving multiple requests concurrently?
- Is there any way django does not save the file uploaded with form
- Is there a way to capture URL parameters with django in urls.py?
- Django Import Error with URLS and ROOT_URLCONF confusion
- Is there a better way to load Django static directories for multiple apps than this? Thank you
- How do I dynamically load data from my django views to my django template with jquery without refreshing the page?
- (Django) Reverse dynamic urls with multiple arguments using Django Views
- Writing your first Django app, error with from . import views
- Is there a way to get an error using setattr which would I get without using setattr with django models?
More Query from same tag
- Fields clash in case of inheritance
- Mocking pyodbc module calls for django unit tests
- Structure for Django methods that span different models
- Django unique URL redirection issue
- Is the {% csrf_token %} CSRF protection tag still necessary in Django 1.2?
- Setup postgres in Github Actions for Django
- Get a List of Project Specific Apps in Django
- S3 Hosting/Query String Authentication broken with extra parameters
- virtualenv not using own packages
- How to iterate over a formset in django?
- django when updating pass the required field
- How do I speed up iteration of large datasets in Django
- "Must provide query string." graphene-python
- Django: create object from CSV that has a ForeignKey
- Using MongoDB as backend for Django: Error running "python manage.py migrate"