Git Product home page Git Product logo

django-microframework's Introduction

Django as a Microframework

A single-page Django website as demoed by Django Fellow Carlton Gibson at DjangoCon US 2019 (video).

Set Up

In a directory of your choice install Django.

$ pipenv install django==2.2.6
$ pipenv shell
(env) $

Option 1

Create a new file hello_django.py and update it as follows:

# hello_django.py
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.http import HttpResponse
from django.urls import path

settings.configure(
    ROOT_URLCONF=__name__,
)

def hello_world(request):
    return HttpResponse("Hello, Django!")

urlpatterns = [
    path('', hello_world)
]

application = WSGIHandler()

Install Gunicorn to run the local server.

(env) $ pipenv install gunicorn==19.9.0

Then start the server:

(env) $ gunicorn hello_django:application

And navigate to http://127.0.0.1:8000.

Option 2

As pointed out by Peter Baumgartner, if we import execute_from_command_line then it's possible to make python hello_django1.py the equivalent of running Django's manage.py command.

To demo with runserver rather than Gunicorn we will set DEBUG to TRUE.

from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.core.management import execute_from_command_line # new
from django.http import HttpResponse
from django.urls import path

settings.configure(
    ROOT_URLCONF=__name__,
    DEBUG=True, # new
)

def hello_world(request):
    return HttpResponse("Hello, Django!")

urlpatterns = [
    path('', hello_world)
]

application = WSGIHandler()

if __name__ == "__main__": # new
    execute_from_command_line()

Then start the server with Django's runserver command.

(env) $ python hello_django1.py runserver

And navigate to http://127.0.0.1:8000.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.