Metadata-Version: 2.1
Name: django-flasky
Version: 0.1.0
Summary: Write a Django app which looks like Flask.
Home-page: https://radiac.net/projects/django-flasky/
Author: Richard Terry
Author-email: code@radiac.net
License: BSD
Project-URL: Documentation, https://radiac.net/projects/django-flasky/documentation/
Project-URL: Source, https://github.com/radiac/django-flasky
Project-URL: Tracker, https://github.com/radiac/django-flasky/issues
Keywords: django flask
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Framework :: Django :: 3.2
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Requires-Python: >=3.8

=============
django-flasky
=============

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:

.. code-block:: python

    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:

.. code-block:: sh

    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:

.. code-block:: python

    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:

.. code-block:: sh

    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:

.. code-block:: python

    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:

.. code-block:: sh

    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.
