Django — backend development made simple

Bartek Pacia
4 min readDec 4, 2018

--

If you are developing web apps for quite a while, you’ve probably heard of Django. Maybe you even got interested and checked their website, but that’s it? Well, it has never been a better time to check it out!

So, to start, let’s answer the most basic question: what is Django?

No, not this Django

It’s a server-side framework using Python as its main language.

It’s advertised as The web framework for perfectionists with deadlines. Is it actually true? Well, it’s not for me to judge this as I have very little experience with it, but it has very good opinions on the web. Still not convinced? Spotify and Instagram, one of the most popular web services in the world use it as their backend.

Okay, enough of this flattering, how do I start?

I’ll do a list so it’s more readable.

  • As said above, Django’s language is Python so you obviously need Python installed, preferably in version 3.
  • When you get Python, then you have to download Django. The easiest way to do this is by using pip (something like npm for Node). pip is bundled with newer Python releases so you don’t have to worry about installing it. All you have to do is type pip install django .
  • When download finishes, go to the directory where you want your project to be created and django-admin startproject <project-name> . Aaaand…that’s it!

Your project is set up. Before diving into the files, you may want to make sure that everything is in place and working. Type python manage.py runserver and head over localhost:8000 in your browser.

Your eyes should see the following page.

Yay!

Now let’s take a closer look at the generated files.

discover-rudy/
discover-rudy/
__init__.py
settings.py
urls.py
wsgi.py
manage.py

I have a plan to create a website which will help tourists/visitors to learn more about my village (I live in Rudy), so called it Discover Rudy.

The top-level folder (in my case discover-rudy) is a container the project, you can rename it freely.

The inner directory (discover-rudy) is the name of the app. You shouldn’t change its name or edit it (at least as of now).

  • __init__.py indicates that this directory should be treated as Python package. Also see: Packages in python
  • settings.py is pretty self-explanatory.
  • urls.py is where routing happens. You may have a look at this article.
  • wsgi.py stands for Web Server Gateway Interface. It’s also a thing we shouldn’t care at the beggining.

manage.py is a convenience script that allows you to run administrative tasks like Django's included django-admin and muuch more.

Applications in Django

Maybe you got interested in why I bolded the app keyword. Let me explain. The Django project consists of apps. An app is a single functionality of your website. I’ll use the explanation I heard from Bucky Roberts: Each app should do one thing. You should be able to explain the purpose of the app in one simple sentence.

Again, it’s much easier for me to teach by examples. Think once again about my Discover Rudy touristic website. We all know functionalities usually offered in such type of websites, like Featured, Nearby, Tourist Info, Map.

Should these features be an app?

Featured displays the most popular locations.

Tourist Info lets user check news and events in the Rudy

Map is a map of interesting locations and user’s position.

As you can see, I’m able to describe the purpose of each feature in one, quite simple sentence. So each of these functionalities should be an app.

How to create a new app?

As many others things in Django, it’s also very simple.

python manage.py startapp featured

That’ it. We’ve just created a Featured app. You’ll notice that Django has created a new directory with a bunch of new files.

discover-rudy/
featured/
migrations/
settings.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
discover-rudy/
...
manage.py

Now let’s create a simple subpage for our new feature. I’ll open urls.py and type in the following code to add a redirection to our newly created app.

discover-rudy/urls.pyfrom django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('featured/', include('featured.urls'))

Let’s assume that Discover Rudy is available at www.discoverrudy.pl. This code simply says that when the if the url is www.discoverrudy.pl/featured, the routing will be handled by urls.py .

Now, create a urls.py file in the featured app directory. Type there the following code.

featured/urls.pyfrom django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index")
]

Explanation: when the url is www.discoverrudy.pl/featured (as we specified in the previous step), run the method index from the views.py file.

And now the final step :D

In views.py , write the index() method which simply responses with a single HTML header.

from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>Featured section of Discover Rudy.</h1>")

Now it’s time to type python manage.py runserver in the terminal and head over localhost:8000/featured to see the output.

That’s it.

It was very interesting experience for me to do research, try to learn the basics of Django and write this blog post. I hope you appreciate it, if you have any suggestions or want to point out the mistakes I’ve surely made, write them down in comments and I’ll be very grateful!

Bye! ;)

--

--

Bartek Pacia
Bartek Pacia

Written by Bartek Pacia

Interested in everything related to technology and programming.

No responses yet