Separating the settings.py data out - python

In my Django project.
I have a requirement of settings.py:
in my settings.py, there is a CORS_ORIGIN_WHITELIST variable:
CORS_ORIGIN_WHITELIST = (
'http://10.10.10.102',
'http://10.10.10.102:8000',
'http://10.10.10.102:8080',
'http://10.10.10.102:8081',
'http://10.10.10.102:8888',
'http://10.10.10.103',
'http://10.10.10.103:8000',
'http://10.10.10.103:8080',
'http://10.10.10.103:8081',
'http://10.10.10.103:8888',
.....
I want to set it flexibly, I mean I can in my user interface for add/update/delete the items.
but it is settings up in settings.py, how can I implements my requirement?

Out-of-the-box django does not support changing settings dynamically, but you can achieve it by using django constance plugin.

Related

Django project on two domains - limiting access to urls/views

I am working on django project.
It utilizes multiple small apps - where one of them is used for common things (common models, forms, etc).
I want to separate whole for project to two domains, i.g.:
corporatedomain.com and userportal.com
I want corporatedomain.com to use different urls, same for userportal.com.
Is that possible? If so, how can I do this? How should I configure my urls?
Maybe you can look at the Django Site Framework. From Django official documentation:
Django comes with an optional “sites” framework. It’s a hook for associating objects and functionality to particular Web sites, and it’s a holding place for the domain names and “verbose” names of your Django-powered sites.
You can use then this approach
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.contrib.sites.models import Site
current_site = Site.objects.get_current()
if 'userportal' in current_site.domain:
urlpatterns = patterns('',
url(r'', include('userapp.urls')),
url(r'^admin/', include(admin.site.urls)),
)
else:
urlpatterns = patterns('',
url(r'', include('corporateapp.urls')),
url(r'^admin/', include(admin.site.urls)),
)
You should add as many entries as you need to Site Table and add django.contrib.sites app in your INSTALLED_APP and also a SITE_ID variable to your settings bound with the correct site ID. Use SITE_ID = 1 when no domain info are available (for example in your development session). More info about SITE_ID in this post).
In my settings I use the following approach:
SITE_ID = os.environ.get('SITE_ID', 1)
where I have set the right SITE_ID variable in each of my enrivorments.
You will have separate settings file anyway so define different ROOT_URLCONF for each domain.
UPDATE: If you don't want to use different settings then you have to write the middleware which will change the request.urlconf attribute using the HTTP_HOST header. Here is the example of such middleware.

Django serving each app separately in each its port

I've got a very simple project running on Django (no models yet) and I need to do the following:
I have created 2 apps, 'Ebony' and 'Ivory' that need to communicate with each other through JSON messages (originally designed to run on different machines but for now one is good enough).
The problem is that the Django Debug server is just one process which runs in a specific port. What I want to do is make each 'App' listen to its own port on the same server and if possible under the same Django project. Is such a scenario possible? And if yes, how should I go about it?
Thanks in advance
This is possible, but not the way you're conceptualizing it. A Django app is one part of what runs on a given web server. Thus a Django project, which has one or more apps, runs as a part of one web server.
The solution is to run multiple instances of Django. Not sure how well this is going to work for you with the debug servers. You can run each server on its own port by giving it a parameter telling it where to open the port, for example:
./manage.py runserver 8000
runs a debug server on 127.0.0.1:8000, and
./manage.py runserver 8080
runs another debug server on 127.0.0.1:8080. Usually this is done in separate shells.
You will need to make sure that the INSTALLED_APPS setting on one of these has 'Ebony' in it, and the other has 'Ivory'. You will also need to figure out some way to tell each instance how to connect to the other (usually by specifying a root URL).
That said, later on you will need to figure out if your two apps will be sharing the same database. If so, make sure that both machines can get to it. If not, make sure the DATABASES value in settings.py is different for each one. If you're sharing the database, Django's sites framework can help you keep things straight in your models.
To have both running from the same project, you have to tell Django which one to run. I prefer to use an environment variable. This changes the above runserver commands to:
SHARD=Ebony ./manage.py runserver 8000
and
SHARD=Ivory ./manage.py runserver 8080
In your settings.py file, this variable can be accessed through os.environ. So, for example, for the INSTALLED_APPS setting to have different values for each shard, you write something like:
SHARD = os.environ["SHARD"]
# Apps common to all shards go here.
LOCAL_APPS = [
commonApp,
]
# Add apps specific to each shard.
if SHARD == "Ebony":
LOCAL_APPS += [
Ebony,
]
elif SHARD == "Ivory":
LOCAL_APPS += [
Ivory,
]
# Add them to the apps that aren't mine.
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.admin',
# ... omitted for brevity ...
'django_extensions',
'south',
'debug_toolbar',
) + LOCAL_APPS
By defining SHARD as a setting in this file, you avoid having to have all your code access the environment variable, and you confine the logic for setting SHARD to settings.py, in case you want to change it later. Your other Python files, if needed, can get the setting with from django.conf.settings import SHARD.
A similar mechanism can be used to give each shard its own DATABASES setting, too. And anything else in settings.py.
Then later in your urls.py file, you use that to pull in your apps' URLs:
from django.conf.urls import *
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'commonApp.views.get_homepage', name='home'),
url(r'^login$', 'django.contrib.auth.views.login', name="login"),
url(r'^logout$', 'django.contrib.auth.views.logout',
{"next_page": "/"}, name="logout"),
# Admin
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
# Auto-add the applications.
for app in settings.LOCAL_APPS:
urlpatterns += patterns('',
url(r'^{0}/'.format(app), include(app + '.urls', namespace=app)),
)
This means your apps need their own urls.py files, and your app URL names get prefixed with your app names. So if the app Ebony defines a URL pattern with name="index", you would get that URL in a template with {% url 'Ebony:index' %}.

Struggling with Django syncdb

This drives me mad... I'm reorganizing an existing Django project using the following structure:
[project_abc]
[app]
[core]
[app1]
admin.py
models.py
...
[app2]
admin.py
models.py
...
... etc ... there's a total of 9 apps
[rest]
... rest api stuff, non-db related ...
[mobile]
... mobile stuff, non-db related ...
[
south
tastypie
[project_abc]
settings.py
urls.py
manage.py
All apps with models that require database access have been added to settings.py:
INSTALLED_APPS = (
'django.contrib.admin',
'[app].[core].[app1]',
'[app].[core].[app2]',
...
'tastypie',
'south'
)
Each model class has a Meta class like:
class Meta:
app_label=[app] # this points to the top level above [core]
Directories [app], [core] and subsequent [app] directories have an __init__.py file in them.
When I run syncdb, it happily ignores my apps under [core] however the tables for other apps like tastypie and south get created properly.
When I run manage.py validate it returns 0 errors found
I've read probably all posts and hints on topics related to syncdb but unfortunately to no avail. I'm obviously missing something, but cannot figure out what it is....
I can't fully understand which [app] is set in models Meta, but note that django syncdb uses django.db.models.get_apps to find projects' applications. Latter interspects apps from INSTALLED_APPS, and explicetely tries to load apps' models module with
models = import_module('.models', app_name)
So applications outside INSTALLED_APPS won't have tables synced.
Second, django loads all the models with django.db.models.get_apps for each found application, and latter turn introspects AppCache.apps_models (that cache is, as far as I remember, populated with register_models by model constructor). So all the imported models corresponding to valid applications are processed.
I guess you have to ensure that [app] from models._Meta:
contains models.py (possibly empty) which will make it a django application;
is mentioned in INSTALLED_APPS, to be asseccible with get_apps function.

variables included in TEMPLATE_CONTEXT_PROCESSORS not in template with DEBUG=False

When I set DEBUG=False in my settings file in django 1.5, I no longer have access to the STATIC_URL or any of the other variables that should be loaded by my TEMPLATE_CONTEXT_PROCESSORS in my django templates. Oddly, everything works when DEBUG=True. For what its worth, I definitely have 'django.core.context_processors.static' in my TEMPLATE_CONTEXT_PROCESSORS so that is not the issue. I have also checked a few other variables in my template context and none of the other nothing seems to be there. MEDIA_URL? nope. request? nope. See this example on github (which has been updated with solution), but these are the important pieces that correctly work when DEBUG=True and throw a 500 error when DEBUG=False:
# settings.py
from django.conf.global_settings import *
# ...
TEMPLATE_CONTEXT_PROCESSORS += (
'django.core.context_processors.request',
)
# believe it or not, 'django.core.context_processors.static' is in there
print TEMPLATE_CONTEXT_PROCESSORS
# views.py
from django.template import RequestContext
from django.shortcuts import render_to_response
def wtf(request):
return render_to_response(
"wtf.html", {},
context_instance=RequestContext(request)
)
Does something special happen in django 1.5 when you turn off debug mode? Any suggestions for fixing and/or debugging the problem would be greatly appreciated!
Looks like there was a change between Django 1.2 and 1.3.
You now have to include django.core.context_processors.static in your TEMPLATE_CONTEXT_PROCESSORS if you want the STATIC_URL available to your template outside of debug mode.
You also need to ensure you're using a RequestContext instance when rendering the template.
This can be fixed by editing the ALLOWED_HOSTS variable in your settings.py. See this answer for more details.
To get this to work on localhost, for example, set ALLOWED_HOSTS = ['localhost'].

Django run admin inteface with custom template

In django how to run /admin interface as well as customized admin index page. My template dirs is followed below.
TEMPLATE_DIRS = (
PROJECT_PATH + '/templates/',
)
And...
ADMIN_MEDIA_PREFIX = '//admin/'
If i will comment this line my other functions would not work, if i put it uncommented then ma admin interface shows my specified file.
What should i do to run both simultaneously. Thanks in advance
Leave TEMPLATE_DIRS alone, that affects more than just the admin, and that's not your problem anyways.
The way to override any admin page is to include the associated template from the default Django admin templates in your own 'yourproject/templates/admin' directory, and make the necessary modifications.
See the documentation: https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#overriding-admin-templates

Categories

Resources