Installing a Django Script Application - How do I add an application to the INSTALLED_APPS setting?

I wrote a Django application, and now I want to simplify deployment on multiple servers.

Basic setting:

  • Copy application folder to Django project folder
  • Add it INSTALLED_APPStosettings.py
  • Run ./manage.py collectstatic

In this particular application, there is no need to use a database, but if that were the case, I would use the south and run ./manage.py migrate, but that's a different story.

I have a problem with number 2. I do not want to manually edit this file each time. What is the easiest / most reliable way to update it?

I thought I could use a module inspectto find a variable and then add it somehow, but I was out of luck. inspect.getsourcelineswill not find variables.

+5
source share
2 answers

Here are my reasons why I think this would be wrong:

  • this is an additional code complexity without any great need, adding one line to the settings each time is not so bad, especially if you do steps 1 and 3.
  • it will not explicitly state which applications your project uses. When another developer will work on your project, he may not know that your application is installed.
  • you need to take step number 1 and step number 2 in the version control system of the code, test the entire system, and then commit the changes and just deploy it.

, - ( ) /, ​​ "". , INSTALLED_APPS.

- , , , . python . :

pip install my-app-name

№1 №3! №1 pip, №2 №3 , .

script - :

# Install paste script:
pip install pastescript

# install django templates for pastescript:
pip install fez.djangoskel

# now paste script knows about fez.djangoskel because of entry-points

# start a new django project from fez templates:
paste create -t django_buildout

setup.py fez.djangoskel:

...
entry_points="""
[paste.paster_create_template]
django_buildout=fez.djangoskel.pastertemplates:DjangoBuildoutTemplate
django_app=fez.djangoskel.pastertemplates:DjangoAppTemplate
...

zc.buildout - , . Python buildout.

+2

settings.py bash.

#set $SETTINGS_FILE variable to full path of the your django project settings.py file
SETTINGS_FILE="/path/to/your/django/project/settings.py"
# checks that app $1 is in the django project settings file
is_app_in_django_settings() {
    # checking that django project settings file exists
    if [ ! -f $SETTINGS_FILE ]; then
        echo "Error: The django project settings file '$SETTINGS_FILE' does not exist"
        exit 1
    fi
    cat $SETTINGS_FILE | grep -Pzo "INSTALLED_APPS\s?=\s?\[[\s\w\.,']*$1[\s\w\.,']*\]\n?" > /dev/null 2>&1
    # now $?=0 if app is in settings file
    # $? not 0 otherwise
}

# adds app $1 to the django project settings
add_app2django_settings() {
    is_app_in_django_settings $1
    if [ $? -ne 0 ]; then
        echo "Info. The app '$1' is not in the django project settings file '$SETTINGS_FILE'. Adding."
        sed -i -e '1h;2,$H;$!d;g' -re "s/(INSTALLED_APPS\s?=\s?\[[\n '._a-zA-Z,]*)/\1    '$1',\n/g" $SETTINGS_FILE
        # checking that app $1 successfully added to django project settings file
        is_app_in_django_settings $1
        if [ $? -ne 0 ]; then
            echo "Error. Could not add the app '$1' to the django project settings file '$SETTINGS_FILE'. Add it manually, then run this script again."
            exit 1
        else
            echo "Info. The app '$1' was successfully added to the django settings file '$SETTINGS_FILE'."
        fi
    else
        echo "Info. The app '$1' is already in the django project settings file '$SETTINGS_FILE'"
    fi
}

:

add_app2django_settings "my_app"
0

All Articles