flango

Flask-like interface for Django


Keywords
django, flask
License
MIT
Install
pip install flango==0.0.2

Documentation

flango!

Do you like the niceties of Flask, but have to use Django for some reason? Are you a lunatic?

If you answered yes twice, this library is for you!

Supports Python 2.6 to 3.7, and Django 1.8+

Quickstart

Install package.

pip install flango

Create Flango app, add it to your urls.

# urls.py
from flango import Flango

app = Flango(__name__)

@app.route('/<int:pk>')
def index(pk):
    from django.contrib.auth.models import User
    return User.objects.get(pk=pk).first_name

urlpatterns = app.urlpatterns

Benefits

  • Access current request anywhere

    from django import forms
    from flango import request
    
    class MyForm(forms.Form):
       def save(self):
           instance = super(MyForm, self).save(commit=False)
           instance.user = request.user
           instance.save()
           return instance
  • Declare your routes alongside your views

    from flango import render_template
    from myapp import app
    
    @app.route('/about')
    def about():
       return render_template('about.html')
  • Typed variable parts

    from flango import render_template
    from myapp import app
    
    @app.route('/map/<float:lat>/<float:long>')
    def map(lat, long):
       assert isinstance(lat, float)
       assert isinstance(long, float)
       return render_template('map.html', lat=lat, long=long)
  • Return response content, status code, and headers directly from views

    from myapp import app
    
    @app.route('/ok')
    def ok():
       return 'ok'
    
    @app.route('/idk')
    def idk():
       return 'idk', 404
    
    @app.route('/wat')
    def wat():
       return 'wat', 400, {'Content-Type': 'text/wat'}
  • Save precious PEP-8 space when building URLs

    from django.db import models
    from flango import url_for
    
    class MyModel(models.Model):
       def get_invitation_link(self, friend_name):
           return url_for('my-model-invite', id=self.id, friend_name=friend_name)

Using flango.request with regular Django views

Flango wraps your view functions to provide the request object globally. When your form or other code which accesses flango.request is called from a regular Django view, it will fail.

To fix this, add Flango's global_request_middleware to your settings.MIDDLEWARE:

# settings.py
MIDDLEWARE = (
    'flango.global_request_middleware',
    # ...
)

For best results, place it at the top of the list. This allows you to use flango.request in other middlewares.