Skip to main content

Django version 2.0 // A Few Key Features


   

Hi, In this post from time to time as I uncover new things about Django version 2.0. This post will not be exhaustive since the release notes are.
Something else that's important to note: this post is mainly for our content. If you're randomly finding this, you might be wondering why we left out a few things. It's simple: the things below I know will have an immediate impact for our students looking to move to Django 2.0 (but remember always follow the version in the videos!) Enjoy.

New Starter page!

The image above, that's the new default starter page. Ahhh much better.

is_authenticated() is now is_authenticated

Example:
# < 2.0 
request.user.is_authenticated()

# 2.0

request.user.is_authenticated

reverse moved

# < 2.0 
from django.core.urlresolvers import reverse

# 2.0+
from django.urls import reverse
django.core.urlresolvers is now simply django.urls

on_delete is now required on Foreign Keys

#models.py

user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, Blank=True, related_name='update_user')

MIDDLEWARE_CLASSES in settings is just MIDDLEWARE now

Simplified URLs without REGEX

Both of the below accomplish the same result (and keyword argument):
from django.urls import re_path, path

from profiles.views import ProfileDetailView

urlpatterns = [
    ...
    re_path(r'^user/(?P<pk>\d+)/$', ProfileDetailView.as_view()),
    path('user/<int:pk>/', ProfileDetailView.as_view()),
    ...
]
Both of the below almost accomplish the same result (and keyword argument) as stated in the release notes:
from django.urls import re_path, path

from posts.views import YearArchive
urlpatterns = [
    ...
    re_path(r'^user/(?P<year>[0-9]{4})/$', YearArchive.as_view()),
    path('user/<int:year>/', YearArchive.as_view()),
]
The biggest difference between the year keyword arguments are the regex limits the allowed digits to 4, the other, will match any int

Namespacing

The reverse("<namespace>:<url-name>", kwargs={"<kwarg>": "<val>"}) method is great for building our urls.
In Django <= 1.11, we'd simply do:
# cfehome/urls.py 
urlpatterns = [
    url(r'^posts/', include('posts.urls', namespace='posts')) 
]
Notice the namespace keyword argument.
In Django 2.0+, we can just do:
# cfehome/urls.py
urlpatterns = [
    re_url(r'^posts/', include(('posts.urls', 'posts'))) 
]
Same thing is accomplished. We can even elminate that namespace variable all together by adding a new variable to urls.py in any given app. Such as:
# cfehome/urls.py
urlpatterns = [
    re_path(r'^posts/', include('posts.urls')) 
]
# posts/urls.py
from django.urls import re_path, path
from posts.views import YearArchive, ArchiveListView

app_name = 'posts'

urlpatterns = [
    ...
    re_path(r'^$', ArchiveListView.as_view(), name='archive-list'),
    re_path(r'^user/(?P<year>[0-9]{4})/$', YearArchive.as_view(), name='year-archive'),
    path('user/<int:year>/', YearArchive.as_view(), name='year-archive-path'),
]
Now, try to implement this using reverse and the url template tag
reverse("posts:archive-list")


reverse("posts:archive-list", kwargs={"year": 2017})



{% url "posts:archive-list" %}

{% url "posts:year-archive" year=2017 %}

Popular posts from this blog

How to read or extract text data from passport using python utility.

Hi ,  Lets get start with some utility which can be really helpful in extracting the text data from passport documents which can be images, pdf.  So instead of jumping to code directly lets understand the MRZ, & how it works basically. MRZ Parser :                 A machine-readable passport (MRP) is a machine-readable travel document (MRTD) with the data on the identity page encoded in optical character recognition format Most travel passports worldwide are MRPs.  It can have 2 lines or 3 lines of machine-readable data. This method allows to process MRZ written in accordance with ICAO Document 9303 (endorsed by the International Organization for Standardization and the International Electrotechnical Commission as ISO/IEC 7501-1)). Some applications will need to be able to scan such data of someway, so one of the easiest methods is to recognize it from an image file. I 'll show you how to retrieve the MRZ infor...

How to generate class diagrams pictures in a Django/Open-edX project from console

A class diagram in the Unified Modeling Language ( UML ) is a type of static structure diagram that describes the structure of a system by showing the system’s classes, their attributes, operations (or methods), and the relationships among objects. https://github.com/django-extensions/django-extensions Step 1:   Install django extensions Command:  pip install django-extensions Step 2:  Add to installed apps INSTALLED_APPS = ( ... 'django_extensions' , ... ) Step 3:  Install diagrams generators You have to choose between two diagram generators: Graphviz or Dotplus before using the command or you will get: python manage.py graph_models -a -o myapp_models.png Note:  I prefer to use   pydotplus   as it easier to install than Graphviz and its dependencies so we use   pip install pydotplus . Command:  pip install pydotplus Step 4:  Generate diagrams Now we have everything installed...

Open edX Step-By-Step Production Installation Guide

Get your first Open edX platform up and running with this detailed step-by-step how-to guide that provides detailed instructions on how to build your AWS EC2 Ubuntu Linux server, execute the Open edX native build scripts, and configure your new platform. The first time I installed an instance of Open edX, in many ways I felt like I was on the outside looking in. The project documentation that exists is very good, but, it assumes an extensive knowledge base that, at the time, I lacked. This article attempts to fill in some of the gaps in that knowledge base, and hopefully, make the Open edX platform more accessible. Note: this article references Bash scripts that are located in this Github repository: https://github.com/lpm0073/edx.ginkgo This is a step-by-step fully automated script to stand up a single-server full-stack production-ready instance of Open edX running on an Amazon Web Services (AWS) EC2 (Elastic Compute Cloud — aka virtual server) R3.Large instance (aka 2-cpu ser...