2. Upgrading to Django 1.8.7: sfvue.com site sample with virtualenv and Apache
Continued from 1. Introduction: sfvue site sample with virtualenv and Apache, in this chapter, we'll upgrade Django version from 1.6.11 to 1.8.7.
We need to modify dev-requirements.txt:
... Django==1.8.7 ...
To upgrade, we need couple of fixes.
We get the following error when we run manage.py:
raise AppRegistryNotReady("Models aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
This is caused by the following lines of code in registration/models.py:
import datetime import hashlib import random import re from django.conf import settings from django.contrib.auth.models import User from django.db import models from django.db import transaction from django.template.loader import render_to_string from django.utils.translation import ugettext_lazy as _ try: from django.contrib.auth import get_user_model User = get_user_model() except ImportError: from django.contrib.auth.models import User
We're trying to reference a model at the import time. It is no longer possible in Django >= 1.7. Actually, recent Django versions allow us to use our models only after all of the applications are loaded. So, we should move the line like this:
... try: from django.contrib.auth import get_user_model #User = get_user_model() except ImportError: from django.contrib.auth.models import User ... def create_inactive_user(self, username, email, password, site, send_email=True): ... User = get_user_model() # moved here new_user = User.objects.create_user(username, email, password) new_user.is_active = False new_user.save() ...
Also, we need to do one more thing:
#create_inactive_user = transaction.commit_on_success(create_inactive_user) create_inactive_user = transaction.atomic(create_inactive_user)
We replaced commit_on_success with atomic.
Another fix is needed when we upgrade Django from 1.6 to 1.8.
When we delete a data from Admin page (e.g., delete a user or a site), we get the DjangoDoc: Exceptions-NoReverseMatch Reverse for 'app_list' with arguments '()' and keyword arguments '{u'app_label': ''}' not found:
NoReverseMatch at /manage/auth/user/ Reverse for 'app_list' with arguments '()' and keyword arguments '{u'app_label': ''}' not found. 1 pattern(s) tried: [u'manage/(?P<app_label>registration|auth|sites|profiles|resources|djangoratings|flatpages)/$'] ... Error during template rendering In template /home/k/sfvue3/templates/admin/delete_selected_confirmation.html, error at line 9 Reverse for 'app_list' with arguments '()' and keyword arguments '{u'app_label': ''}' not found. 1 pattern(s) tried: [u'manage/(?P<app_label>registration|auth|sites|profiles|resources|djangoratings|flatpages)/$']
We can get the hint from /templates/admin/delete_selected_confirmation.html. So, let's go to Github delete_selected_confirmation.html, and copy the following line of code from it:
{% block breadcrumbs %} <div class="breadcrumbs"> <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a> › <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a> › <a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a> › {% trans 'Delete multiple objects' %} </div> {% endblock %}
then, copy it to our code, /templates/admin/delete_selected_confirmation.html:
{% extends "admin/base_site.html" %} {% load i18n l10n %} {% load url from future %} {% load admin_urls %} {% block breadcrumbs %} <div class="breadcrumbs"> <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a> › <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a> › <a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a> › {% trans 'Delete multiple objects' %} </div> {% endblock %} {% block content %} {% if perms_lacking or protected %} {% if perms_lacking %} <p>{% blocktrans %}Deleting the selected {{ objects_name }} would result in deleting related objects, but your account doesn't have permission to delete the following types of objects:{% endblocktrans %}</p> <ul> {% for obj in perms_lacking %} <li>{{ obj }}</li> {% endfor %} </ul> {% endif %} {% if protected %} <p>{% blocktrans %}Deleting the selected {{ objects_name }} would require deleting the following protected related objects:{% endblocktrans %}</p> <ul> {% for obj in protected %} <li>{{ obj }}</li> {% endfor %} </ul> {% endif %} {% else %}
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization