Upgrading Django from version 1.9 to version 2.0

    Hello, today I would like to tell you about how to upgrade the project from version 1.9 to 2.0. What are the main nuances to consider and rewrite in order for the project to start on the new version of Django.

    First step


    This is a Django update to version 2.0, as well as an update of all of your related packages that are used in the project, because I use virtual environment and requirements.txt, then for me this is one way, for you there may be another.

    After you have updated all the packages, you should not start the project, it will not start anyway, so we will immediately start fixing all the main points so that the project can start.

    The second step. Update all urls.py of your project


    In your main urls.py, in which you include urls from other aplicas, we connect:
    from django.urls import re_path, path.

    And we change from url to path, as well as remove regular expressions in these connections.

    url(r'^ some/', include('some.urls')), # старый вариант
    path('some/', include('some.urls')),

    If you use views directly from this aplicashin, which are required by regulars, then we use:

    re_path(r'^app/$', App.as_view(), name='app')

    In connected aplikashinas (for example, some / urls.py), in the urls.py file, we use:

    re_path(r'^create/$', Create.as_view(), name='create')

    If you use the namespace in urls during the installation, then remove them from there and transfer them directly to the connected application. Go to urls.py of this application and write down above urlpatterns = []

    app_name = ‘app-application’

    This line serves as a replacement for the namespace and is designed to make the main urls.py cleaner and more readable, as well as for the convenience of changing names in one place.

    Third step


    We use the search for the entire project, depending on your code editor, which you use when developing, these may be different hot keys, I think you know them, so I will not stop here.

    We drive in:

    is_authenticated()

    And change to is_authenticated. Now this is not a method, but a property. This error will raise an exception.

    Next in the project we are looking for:

    from django.core.urlresolvers import reverse 

    And change to:

    from django.urls import reverse

    Fourth step


    Now in all models.ForeignKey, the “on_delete” argument must be positionally required, for example:

    on_delete=models.CASCADE
    on_delete=models.DO_NOTHING
    on_delete=models.SET_NULL

    Next, do:

    python manage.py makemigrations
    python manage.py migrate

    Fifth step


    If you try to start a project, then it should already start, but immediately it will give you an error as soon as you log in at 127.0.0.1:8000.

    The error will be as follows:

    AttributeError at /
    ‘WSGIRequest’ object has no attribute ‘user’

    It happens because you need to rename MIDDLEWARE_CLASSES to MIDDLEWARE.
    Then you will get the following error in the console:

    django.core.exceptions.ImproperlyConfigured: WSGI application 'application' could not be loaded; Error importing module: 'application doesn't look like a module path

    This error is due to the fact that you have old middleware and you need to update them to:

    'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
        'django.middleware.security.SecurityMiddleware'

    Sixth step


    If you use your own middleware in the project, then they should be inherited from MiddlewareMixin, and not from object (you can also use object, but then you need to write additional required methods).

    Import:

    from django.utils.deprecation import MiddlewareMixin 

    That's all! :)

    Of course, if you have a very large project and you use a large number of packages, then you will have more problems, but the chain of errors in the console will help you solve them and run the project in the proper mode. This guide describes the main errors and ways to solve them that are relevant to all projects with Django 1.9 (some points are not relevant to the version of Django 1.11), and will help to quickly transfer the project to Django 2.0, as well as to avoid unnecessary loss of time to parse and search making common mistakes.

    Also popular now: