Blog

    Upgrade from Django 1.3 to Django 1.4

    Published on August 12, 2014

    Recently I had the pleasure to update from Django 1.3 to Django 1.4. Due to security reasons, it is recommended to update to newer Django releases, furthermore, a Django Version older than 1.3 is not supported anymore.

    For those upgrading to a new version of Django, you will need to check all the backwards-incompatible changes and deprecated features for each ‘final’ release from the one after your current Django version, up to and including the new version.

    I recommend to upgrade from one release step to another step and not to jump from version 1.3 to e.g. 1.6. I will write my experiences during a Django update from 1.4 to 1.5 in a separate blog entry.

    Now in this blog entry I will show those backwards-incompatibilities I had to face during my update process from Django 1.3 to 1.4. I hope you will find some hints that will help you to successfully update your project as well:

    Updated default project layout and manage.py

    If you try to start your localhost server by doing python manage.py runserver after you have downloaded and installed Django 1.4, you will get the follwing error:

    Error: Can't find the file 'settings.py' in the directory containing 'manage.py'. It appears you've customized things. You'll have to run django-admin.py, passing it your settings module. (If the file settings.py does indeed exist, it's causing an ImportError somehow.)

    This is because Djangos project structure has changed. Until Django 1.3 the manage.py file was located within the project folder like this:

    mysite/
        manage.py
        __init__.py
        settings.py
        urls.py
        myapp/
            __init__.py
            models.py
    

     Place manage.py outside the mysite/ directory and edit it like this:

    #!/usr/bin/env python
    import os, sys
    
    if __name__ == "__main__":
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")
    
        from django.core.management import execute_from_command_line
    
        execute_from_command_line(sys.argv)
    

    {{ project_name }} should be replaced with the Python package name of the actual project.

    If you leave myapp/ folder as described above, you should import mysite.settings, mysite.urls, and mysite.myapp, but not settings, urls, or myapp as top-level modules.

    If you want to import your apps as top-level modules you must replace your apps folders adjacent to the new manage.py like this:

    manage.py
    myapp/
        __init__.py
        models.py
    mysite/
        __init__.py
        settings.py
        urls.py
    

    Otherwise you will get "ImportError: no module named <myapp> in *.py".

    Module “django.core.context_processors” does not define a “auth” callable request processor

    django.core.context_processors.auth was completely removed in Django 1.4, so you need to make the following change to TEMPLATE_CONTEXT_PROCESSORS in your settings.py file:

    # old
    TEMPLATE_CONTEXT_PROCESSORS = ("django.core.context_processors.auth",
                                   ...
    )
    # new
    TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth",
                                   ...
    )

    Loading template Error

    The ability to specify a callable template loader rather than a Loader class will be removed, as will the load_template_source functions that are included with the built in template loaders for backwards compatibility. So after an upgrade from Django 1.3 to Django 1.4 you will get the following error:

    django.template.loaders.filesystem.load_template_source: "'module' object has no attribute 'load_template_source'"

    So you need to make the following change to TEMPLATE_LOADERS in your settings.py file:

    #old
    
    TEMPLATE_LOADERS = (
    
        'django.template.loaders.filesystem.load_template_source',
    
        'django.template.loaders.app_directories.load_template_source',
    
    )
    
    #new
    
    TEMPLATE_LOADERS = (
    
        'django.template.loaders.filesystem.Loader',
    
        'django.template.loaders.app_directories.Loader',
    
    )

    Some third party apps incompatibilities with Django 1.4

    ckeditor

    The recent widgets.py of ckeditor-updated version 4.4.0 throws the following error after the Django 1.4 upgrade:

    ImportError: cannot import name force_text

    So you need to upgrade to the latest Django 1.4 (as of 07-08-2014 - the "1.4.13").

    contact-form

    If you want to use a Cross Site Request Forgery protection you need to use the csrf_token tag inside the <form> element in any template that uses a POST form, e.g.:

    <form action="." method="post">{% csrf_token %}
    

    and make the following change to MIDDLEWARE_CLASSES in your settings.py file:

    #'django.contrib.csrf.middleware.CsrfMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',

    Comments

    So far, this entry has 0 comments.

    Post a comment

Buchempfehlungen pix pix
Kalender
December 2018
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930
31      
Über pix

Ich bin Diplom Informatiker und das ist meine persönliche Internet sowie weblog Seite.mehr Informationen...