DEV Community

Cover image for Exploring Web Development: Python + Django
Alex Hebert
Alex Hebert

Posted on • Updated on

Exploring Web Development: Python + Django

The more I have learned about full-stack development with stacks like MERN, the more curious I have become about how other tech stacks compare, how they look, and how hard would it be for me to learn them now that I have a foundational understanding of full-stack development. So I decided to challenge myself over the next 6 weeks: to learn 3 new tech stacks and write a todo app with each one. After 2 weeks of learning the tech and writing the app I will log my experience here, give a brief overview of what I learned, and share my opinions on working with the technologies.

The first tech stack I chose is Django. I chose Django for 2 main reasons:

  1. I already know a decent amount of python from my college projects
  2. Python is one of the most popular languages right now and freshening up a bit on my Python knowledge seemed like a pretty good use of my time

Excited to dive back into python after being fully immersed in JavaScript, I dove in head first.

Django Basics

Welcome to Django

Django is a python web-app framework that focuses on development speed. Their homepage says: "Django was designed to help developers take their applications from concept to completion as quickly as possible," and I feel that they are definitely succeeding in that goal. Developing with Django felt incredibly straight-forward and it did not take much time at all for me to get from an empty directory to basic functionality.

Setting up the app

Django makes setting up your workspace super easy. Once you're in your project directory run django-admin startproject todo-project to start a new project called todo-project. Django will make a new directory called todo-app with this file structure:

files generated by startproject

manage.py is a key part of django, running it is how we will make changes to our database, start our server and do most other interactions we will need to do with our app. __init__.py is an empty file that's used by python to denote the directory as a python package. For the time being we wont worry about settings, asgi, or wsgi, instead we should start our first app. In Django, projects are collections of apps and apps are the functional components. Apps are like modules that can be implemented into many projects - like web app Legos!

To start our app we can use manage.py to set up our app skeleton with python manage.py startapp todos that will generate this directory:

files generated by startapp

After our app is set up by Django we need to add it to our project. To do this we need to edit the INSTALLED_APPS array in the settings.py in the todo_project directory.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'todo'
]
Enter fullscreen mode Exit fullscreen mode

Next we need to add a url for the project to use the todo app. In urls.py add a new path to end of the urlspatterns array.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('todos/', include('todo.urls'))
]
Enter fullscreen mode Exit fullscreen mode

And just like that our app is integrated into our project! We can now start working on our functionality.

Building out the App

To get a something rendering we need to add a view to our app. In views.py we need to from django.http import HttpResponse. This will allow us to render a string to our page. All views take in a request and return some response. This response could be
a simple string like return HttpResponse('This is where I would put my todo... If I had one!') or can respond with html with django's render shortcut. For now lets send back that string by adding this to views.py:

from django.http import HttpResponse

def todo_render(req):
  return HttpResponse('This is where I would put my todo... If I had one!')
Enter fullscreen mode Exit fullscreen mode

Now to actually access this view we need to set up an endpoint to use it. We can start by creating a new file in the todo directory called urls.py. This file is similar to the urls.py in the project directory but these endpoints will be added to the end of /todos. We can set up the view we just made as the default view for /todos by adding this to urls.py in todo:

from django.urls import path

from . import views

app_name = "todos"
urlpatterns = [
    path("", views.todo_render, name="index"),
]
Enter fullscreen mode Exit fullscreen mode

And just like that we have a incredibly simple but functional app with Django!. Now lets go checkout all our hard work.

Running the app

To start our server to look at our site in the browser all we need to do is run python manage.py runserver and navigate to localhost:8000/todos/. Thats where this little tutorial will end but if this at all interests you should definitely go to Django Docs and follow their tutorial where they walk through building a polls app.

My Thoughts

I found working with Django to be an extremely streamlined process. Django handled a lot of the tedium of set up and lets you get right into the coding. This is both a blessing and a curse. I like to know how things are working behind the scenes and the degree of abstraction Django uses to get the development as quick as possible feels very hand wavy. I walked away from making the app knowing how to get all the code in the right places but not sure why or how the code works, which is a feeling I am not a big fan of. It reminds me of this gem from 2015.



With that said, I can see myself learning more and using Django in the future to quickly get an idea into practice. One of the things I really like about Django is the included admin view that allows you to interact with your app in an easy to use GUI. I also like the html templating that I find to be very straight forward and easy to pick up. Django is a well-oiled machine and makes web development much more accessible by removing the most intimidating parts, setup.

Resources

I relied on the Django Docs to learn. The documentation is great and the tutorial covers everything you need for the basics.

I hope you try out Django, it is a powerful tool to have in your back pocket if you need an idea in code quickly. Have fun and good luck!

Top comments (0)