I am writing test for my Django app. I want to start Django with different settings value and run tests for each case. (eg one test for settings.ALLOW_ROBOTS=True and another test for settings.ALLOW_ROBOTS=False). I am not interested in overriding the value at run time. I want to start the server with different settings value each time. Does Django provide a way to accomplish this?
You can choose which settings.py file to use with DJANGO_SETTINGS_MODULE; you can have many that import * from a main one, and then change what needs to be changed.
Alternatively, settings.py is just a Python file. You can get values of settings from environment variables, if you want:
import os
ALLOW_ROBOTS = bool(os.getenv('ALLOW_ROBOTS', False))
And then change that environment variable from Travis.
If I understand your question correctly, you would like to be able to use multiple settings modules. If so, setting the DJANGO_SETTINGS_MODULE environment variable before starting the server is likely what you need.
Related
Seems like a really simple issue I am having, but for whatever reason I cannot get an arbitrary file i create within a Django app to access my models/compile.
analytics/
analytics/
settings.py
etc...
mapper/
models.py
views.py
filetoDothings.py
I would like "fileToDoThings.py" do things with my models, but when i put even a few simple lines of code in filetodothings, and try to import the models i get the following error: "Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure()"
A lot of searching has led me to find answers similar to this:
sys.path.append("path/toproject")
os.environ["DJANGO_SETTINGS_MODULE"] = "myApp.settings"
from django.contrib.auth.models import ModelName
This worked, but only when i moved the arbitrary file to the outermost "analytics" directory. Even though I got this to work, it never quite seemed to make sense to me/seemed like a lot of work to get a file to access models in the same directory.
So what I am trying to do now is the following, in filetoDothings.py:
from models import Addresses
usaMapPoints = Addresses.objects.filter(countryCode='US').order_by("-frequency")[:25]
print(usaMapPoints)
For "messing around" purposes, I want to call "python filetodothings.py" and have it print the results in the console. But ultimately Im just trying ot get my head around how to create a file that can be automated later on.
I have seen the code for another Django app and someone was able to do exactly this.
Am I missing something really simple?
I would like "fileToDoThings.py" do things with my models
To do arbitrary operations from the command line, write a new Django management command.
You write your ‘lorem_ipsum.py’ module within a particular app, placing it a fooapp/management/commands/lorem_ipsum.py. Write it to define a command as per the documentation above.
Then you can invoke that as a management command: python3 -m manage lorem_ipsum.
Am I missing something really simple?
Simple, yes; obvious, no.
Django is a framework, not a library. The difference has been characterised by saying that with a library, you write code that calls the library; with a framework, you write code that gets called by the framework.
In other words, the way to get things done in a framework is to find the places where your code fits, and write it to fit those places.
looks like you need create command
https://docs.djangoproject.com/en/1.10/howto/custom-management-commands/
Despite googling I can't find any documentation for the Django HOST_DOMAIN setting in the settings.py.
I am going through a settings.py file I have been given and this is the only part of the file I am not 100% clear on.
You can find out what are all the django specific settings at the settings reference which lists all the settings variables (and their defaults) that are set by django.
Since settings.py is just any other Python module, you are free to define your own variables and import them in your code with:
from django.conf import settings
settings.MY_CUSTOM_SETTING
Third party applications can also define their own settings, which you can modify by entering the specific value in settings.py.
It sounds like HOST_DOMAIN is one such custom setting.
That is not a Django setting.
It's perfectly good practice to define your own project-specific settings inside settings.py, and that is presumably what the original developer did here.
I'm using Django and I'm wondering whether it's proper to use myapp.models, opposed to myproject.myapp.models, or in INSTALLED_APPS, should the FULL NAME be used myproject.myapp or is it alright to just use myapp for it's name? I am wondering because if I were to change the project name using the latter method it would break my app, but I'm not sure that just because the former method works that it is correct. Could someone clear this up for me.
Thank you!
I would say that it is not encouraged to reference your apps using your project name. I say this because as of Django 1.4 this will not work with a default Django project. You can read more about this here:
https://docs.djangoproject.com/en/dev/releases/1.4/#updated-default-project-layout-and-manage-py
A quote from that:
"If settings, URLconfs and apps within the project are imported or referenced using the project name prefix (e.g. myproject.settings, ROOT_URLCONF = "myproject.urls", etc), the new manage.py will need to be moved one directory up, so it is outside the project package rather than adjacent to settings.py and urls.py."
I would recommend against it since it would mean messing with the default project structure, not a huge deal, but increased unnecessary work.
I would also recommend against it since it couples your apps to your project, which imho goes against the philosophy of Django which advocates reusable, decoupled apps.
I want to keep some global settings for my project in Django. I need to have access to these settings from the code. For example, I need to set a current theme for my site that I can set using admin console or from the code. Or I need to set a tagline that will show in the header of all pages. I suppose I should use models for keeping the settings but I can't realize how I should better do it.
There are quite some packages that store settings in models, pick the one that works best for you:
http://pypi.python.org/pypi?:action=search&term=django+settings&submit=search
If you are okay with changing these setting programmatically via settings.py you should do that. However, if you want to change these settings via the Admin Console, you should use models.
It is sometimes beneficial to share certain constants between various code files in a django application.
Examples:
- Name or location of dump file used in various modules\commands etc
- Debug mode on\off for the entire app
- Site specific configuration
What would be the elegant\pythonic way of doing this?
There's already a project-wide settings.py file. This is the perfect place to put your own custom setttings.
you can provide settings in your settings.py like
MY_SETTING = 'value'
and in any module you can fetch it like
from django.conf import settings
settings.MY_SETTING
Create a configuration module.
Configuration.py: (in your project/app source directory)
MYCONST1 = 1
MYCONST2 = "rabbit"
Import it from other source files:
from Configuration import MYCONST1,MYCONST2
...
Django apps are meant to be (more or less) pluggable. Therefore, you are not supposed to hack into the code of an app in order to parametrize what you want (it would be quite a mess if you had to do this ! Imagine you want to upgrade an app you downloaded on internet... you would have to re-hack into the code of the new version ?!?).
For this reason you shouldn't add app-level settings at the app level, but rather put them together somewhere in your project-wide settings.py.