Skip to main content

Django is a Python based framework that takes care of a lot of web development hassle. Thank you Adrian Holovaty and Simon Willison.

“The web framework for perfectionists with deadlines”

It is the easiest back-end framework I have ever had to use so far because it basically does all the heavy-lifting for me; saving me a lot of time and frustration. cause who wouldn’t want a breezy time at work.

To properly understand its foundations, I went through the django documentation and followed the practical tutorial there. Another hands-on tutorial I found quite detailed is the Django-girls tutorial.  I highly recommend one of these first, or both if you are new to django.

Django is a full-stack framework meaning it covers everything in web development; the database, HTML templating and application logic. It’s also pretty easy to learn as you go along. You don’t have to know every nitty-gritty detail in order to work with it. Talk about being  warm and  beginner friendly

Disclaimer: This article assumes you know a little about Django or have began using it. It’s not a how-to beginner guide.

Here are my top 3 features:

1.Admin Interface

So Django creates an admin interface for your application and you don’t have to lift a single finger. I MEAN!!!.

Type the address http://localhost/admin/ in your browser and you should see a page like this.

Create admin user credentials:

You will obviously need user credentials to login as an admin, so create one using the command :

Windows: python manage.py createsuperuser in the root of your project.

 

Now to register your app with django’s admin, you have to register the app in the ‘your-app/admin.py’ file like so:

from django.contrib import admin

from .models import Question

admin.site.register(Question)

Remember to replace Question with your own model name.

 

To login, use the username and password created with the superuser command.

All the Create, Update, Delete actions are readily available in the admin interface. How convenient is that?!?

 

2. Decorators

Decorators can be used to restrict access to views. Django has popular in-built decorators like login_required, has_permission etc. These aren’t the only ones, but I will use login_required for an illustration.

Pretend we have an app called blog. For a user to be able to Write, Edit or Delete an article, they should have an account in the app.

The model file : models.py

from django.conf import settings
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import AbstractUser
class Post(models.Model):
author = models. ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
coverimage = models.ImageField(upload_to = 'images/', blank=True, null=True)
text = models.TextField()
created_date =models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
defpublish(self):
self.published_date = timezone.now()
self.save()
def__str__(self):
returnself.title

views.py:

from django.shortcuts import redirect
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from django.contrib.auth.decorators import login_required
from .models import Post
@login_required
def post_new(request):
if request.method =="POST":
form = PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
return redirect('post_detail', pk=post.pk)
else:
form = PostForm()
return render(request, 'articles/post_edit.html', {'form': form})
@login_required
def post_edit(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method =="POST":
form = PostForm(request.POST, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
return redirect('post_detail', pk=post.pk)
else:
form = PostForm(instance=post)
return render(request, 'articles/post_edit.html', {'form': form})

This app has 2 methods; create a new post and edit one. The login_required decorator at the top ensures that a User is logged in before the create and edit methods can be executed.

The entire background process automated in just one line. Bliss 🙂

I found this extremely helpful when I wanted more information on view decorators.

 

3. Django Crispy Forms

I mentioned how to use this in my July Favorites and it goes without saying, I love it, especially when I’m having a lazy coding day.

“Forms have never been this crispy”

 

Django automates many programming processes. For some, this is heaven sent, but for others who may prefer being in control of the development, Django may not be the best option.

Personally, I think it’s a great choice to use when building something complex in a tight deadline.

To learn more about Django, visit https://simpleisbetterthancomplex.com. He breaks down Django into bits and explains each concept in great detail and takes you from start to finish.

 

<Lulu>