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
Built Distribution
Hashes for django_flasky-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 429377b90b81014f10b80b388f1995368d2abffafeca3651591d7c29c6b49f89 |
|
MD5 | 3e7f0b2817d8b098253f2708f59abc2f |
|
BLAKE2b-256 | 128c72118c3bce57361aa5c3e8e69c4415f8b43ba69e71e3ee44f940b9716084 |