Skip to content
forked from csev/mini_django

A very small web server in Python inspired by Django

Notifications You must be signed in to change notification settings

gctaa/mini_django

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mini Django Web Server

This is a very small Python web server that takes inspiration from Django.

To install this source code, you can either

  • Use git to checkout this repository into a folder

      cd  (into some folder on your computer where you do programming things)
      git clone https://github.com/csev/mini_django/
    
  • Download a zip file from this repository and unzip it into a folder

After you have a copy of the code in a folder on your computer, to run this (assuming you have Python installed), you navigate into the folder and run the server.

cd mini_django
python runserver.py

And navigate to http://localhost:9000

If you want the server to listen on a port other than 9000, add it as an additional parameter on the runserver command.

python runserver.py 9001

And navigate to http://localhost:9001

Interesting URLs and Views

Looking at Code

There is a PowerPoint presenation describing mini_django on the DJ4E web site at https://www.dj4e.com/lectures/mini_django.pptx.

There is a video code walkthrough of this application available at: at https://youtu.be/q6LGpiVPwaA.

mini_django.py

This file is about 200 lines of Python and approximates the entire Django library in a very simple way. It handles many aspects of the HTTP Network Protocol like parsing incoming requests, calling your "application router", and sending a correctly formatted the HTTP response back to the browser to complete the request/response cycle. This library also defines HttpRequest and HttpResponse data classes which are passed into and returned from the application views.

runserver.py

This is a very short file. It loads the mini_django library and calls the httpServer method to start listening for connections on the specified port.

urls.py

The purpose of this file is to look at the path value from the HttpRequest object and decide which view to call. In "real Django", the paths are stored in an array, but to make easier to understand, this file just uses a series if if-then-else tests to pick the correct view for the path.

views.py

This is very similar to the views.py in "real Django". Each view is a function that takes an HttpRequest as its parameter and returns an HttpResponse as its return value. In each view, the code creates an HttpResponse and sets headers in the response and adds the body text to the response and then returns it to mini_django which then correctly formats the response and sends it back to the browser to complete the request / response cycle.

AutoGrader Support

As this code is part of the Django for Everybody course, it has the ability to a small JavaScript library to enable this code to be autograded as an assignment for the course. Take a look at the patchAutograder() method in mini_django for details.

The autograder library is only included in text/html responses.

If you want include the autograder library in your application start the web server as follows:

python ruserver.py 9000 autograder

Assuming you have DJ4E running on your computer, you can do development with a local copy of the autograder JavaScript endpoint as follows:

python runserver.py 9000 http://localhost:8888/dj4e/tools/jsauto/autograder.js

About

A very small web server in Python inspired by Django

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%