Skip to main content

Write a Django app which looks like Flask.

Project description

Write a Django site which looks like Flask, then turn it into a proper Django site when it starts to get complicated.

Quickstart

Write a Django app in the same style as you would a Flask app:

from django_flasky import Django


app = Django(__name__)


@app.route("/")
def hello_world(request):
    return "<p>Hello, World!</p>"

This is almost exactly a simplistic as Flask, but the request object is passed into the view instead of being a global.

Just save the above file as hello_world.py and run it with:

django-flasky run hello.py

Your site will now be running on http://localhost:8000.

But I want a database

No problem, this is Django, just not how you know it:

from django.db import models
from django_flasky import Django

app = Django()

class CountLog(models.Model):
    timestamp = models.DateTimeField(auto_now_add=True)

@app.route("/")
def count(request):
    CountLog.objects.create()
    return f"<p>Number of page loads: {CountLog.objects.count()}</p>"

Save that as counter.py and run it with:

django-flasky run counter.py migrate
django-flasky run counter.py

It will create your database in a db.sqlite3 file next to your counter.py.

Why would you do this? Why?

Developers often begin projects with Flask because it looks easier to get started with than Django, but as the project grows it’s easy for it to become an unmaintainable mashup of third party libraries and hand-rolled bodges just to begin to get close to what Django offers out of the box.

As someone who has often been brought in to try to rescue these projects, I decided that enough is enough - it is time to eliminate that excuse for picking Flask over Django.

Django-Flasky makes it as easy to start a Django project as it is to start a Flask project, but because it’s using Django from the start you’ll be able to take advantage of everything that Django has to offer - models, admin, forms, and the rest - and then switch to a normal Django site structure when you’re ready to do things properly.

Using django-flasky

Settings

Override settings by passing them into your Django(..) object constructor, eg:

app = Django(SECRET_KEY="some-secret", ALLOWED_HOSTS=["lol.example.com"])

Templates and static files

Place your templates and static assets next to hello_world.py, under a templates and static directory respectively.

Limitations

Django really doesn’t like running from a single file, so measures were taken during the development of Django-Flasky which may lead to problems as your project grows.

It is strongly recommended that this project is not used for anything serious.

Converting to a sensible Django project

Once you’ve got a couple of models and views, you’ll start thinking “Hey, maybe I should start splitting this project into apps”. You are correct, and now is the time to turn your project into an actual Django project.

One day you will be able to run:

django-flasky upgrade hello.py

This will do its best to break up your hello_world.py into a proper Django project under hello_world.

Right now though, this is not implemented, so you’ll just need to do it yourself.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-flasky-0.1.1.tar.gz (9.7 kB view hashes)

Uploaded Source

Built Distribution

django_flasky-0.1.1-py3-none-any.whl (10.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page