All Projects → maxpoletaev → Django Micro

maxpoletaev / Django Micro

Licence: bsd-2-clause
Django as a microframework

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Django Micro

Djng
Turtles all the way down
Stars: ✭ 127 (-48.16%)
Mutual labels:  django, microframework
Fosswebsite
A club management system that handles student details, progress, events, achievements, attendance, status updates, teams and workshop registrations. This is the official [email protected] website
Stars: ✭ 242 (-1.22%)
Mutual labels:  django
Celery Progress
Drop in, configurable, dependency-free progress bars for your Django/Celery applications.
Stars: ✭ 230 (-6.12%)
Mutual labels:  django
Relate
RELATE is an Environment for Learning And TEaching
Stars: ✭ 239 (-2.45%)
Mutual labels:  django
Receiver
Swift µframework implementing the Observer pattern 📡
Stars: ✭ 238 (-2.86%)
Mutual labels:  microframework
Django React Redux Base
Seedstars Labs Base Django React Redux Project
Stars: ✭ 2,629 (+973.06%)
Mutual labels:  django
Rest Api
Learn how to build your own REST API with Python, Django, and the Django Rest Framework.
Stars: ✭ 232 (-5.31%)
Mutual labels:  django
Cancan
cancan is a tiny permission controller base on ruby cancan library.
Stars: ✭ 244 (-0.41%)
Mutual labels:  django
Django Rest Framework Datatables
Seamless integration between Django REST framework and Datatables.
Stars: ✭ 241 (-1.63%)
Mutual labels:  django
Chain
链喵 CMDB 本项目已停止开发!因长时间未对代码进行维护,可能会造成项目在不同环境上无法部署、运行BUG等问题,请知晓!项目仅供参考!
Stars: ✭ 240 (-2.04%)
Mutual labels:  django
Tacticalrmm
A remote monitoring & management tool, built with Django, Vue and Go.
Stars: ✭ 231 (-5.71%)
Mutual labels:  django
Django Rest Passwordreset
An extension of django rest framework, providing a configurable password reset strategy
Stars: ✭ 238 (-2.86%)
Mutual labels:  django
Django Vuejs Tutorial
A tutorial to integrate Vue.js with django
Stars: ✭ 242 (-1.22%)
Mutual labels:  django
Zds Site
Cœur du projet technique de Zeste de Savoir
Stars: ✭ 236 (-3.67%)
Mutual labels:  django
Hackide
hackIDE is an online code editor, compiler and interpreter based on Django, powered by HackerEarth API! Go, hack it!
Stars: ✭ 242 (-1.22%)
Mutual labels:  django
Django Migration Linter
🚀 Detect backward incompatible migrations for your django project
Stars: ✭ 231 (-5.71%)
Mutual labels:  django
Django Colorfield
color field for django models with a nice color-picker in the admin. 🎨
Stars: ✭ 238 (-2.86%)
Mutual labels:  django
Pyinstrument
🚴 Call stack profiler for Python. Shows you why your code is slow!
Stars: ✭ 3,870 (+1479.59%)
Mutual labels:  django
Sh00t
Security Testing is not as simple as right click > Scan. It's messy, a tough game. What if you had missed to test just that one thing and had to regret later? Sh00t is a highly customizable, intelligent platform that understands the life of bug hunters and emphasizes on manual security testing.
Stars: ✭ 245 (+0%)
Mutual labels:  django
Django Rest Registration
User-related REST API based on the awesome Django REST Framework
Stars: ✭ 240 (-2.04%)
Mutual labels:  django

============ Django Micro

.. image:: https://img.shields.io/pypi/v/django-micro.svg :target: https://pypi.python.org/pypi/django-micro

.. image:: https://img.shields.io/badge/status-stable-brightgreen.svg

Django Micro is lightweight wrapper around Django that turns it to the microframework for writing small applications in a single file.

tl;dr: See the example_ of full-featured application.

What works

  • Configuration_
  • Views and routes_
  • Models and migrations_
  • Management commands_
  • Custom template tags_
  • Testing_
  • Admin interface_
  • Third party apps

Installation

.. code-block::

$ pip install django-micro

Quick start

Create app.py file with following content.

.. code-block:: python

from django_micro import configure, route, run
from django.http import HttpResponse

DEBUG = True
configure(locals())


@route('', name='homepage')
def homepage(request):
    name = request.GET.get('name', 'World')
    return HttpResponse('Hello, {}!'.format(name))


application = run()

Run the application.

.. code-block::

$ python app.py runserver

Note: Parent directory of the app.py file must have a valid python module name. Under the hood, Micro adds that directory to INSTALLED_APPS and uses it as a regular Django application.

Compatibility

The latest relase of django-micro supports only the latest stable release of Django. This is the only way to keep codebase of django-micro clean, without hacks for different versions of Django.

  • Django version: >=2.1
  • Python version: >=3.4

Run and deployment

On the localhost the application runs with the built-in runserver command and deploys as a standard WSGI application.

.. code-block::

$ python app.py runserver
$ gunicorn example.app --bind localhost:8000
$ uwsgi --module example.app --http localhost:8000

This behaviour is provided by the single string: application = run() which actually just a shortcut for the following code.

.. code-block:: python

if __name__ == '__main__':
    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)
else:
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()

Configuration

The call of the configure function must be placed at the top of your application above the definition of views, models, and imports of other modules. It may violate PEP8, but this is the only way to make it works. You can’t define models or import models from another application until Django is configured.

I recommend to define all the configuration in the global namespace and call configure with locals() argument. Don’t worry, configure takes only UPPERCASE variables.

.. code-block:: python

from django_micro import configure

DEBUG = True

configure(locals())

Views and routes

Routing is wrapped in a single function route. You can use it as a decorator.

.. code-block:: python

from django_micro import route

@route('blog/<int:year>/', name='year_archive')
def year_archive(request, year):
    return HttpResponse('hello')

Or as a regular function.

.. code-block:: python

def year_archive(request):
    return HttpResponse('hello')

route('blog/<int:year>/', year_archive, name='year_archive')

Also route may be used with class-based views.

.. code-block:: python

@route('blog/<int:year>/', name='year_archive')
class YearArchiveView(View):
    def get(request, year):
        return HttpResponse('hello')

# or directly
route('blog/<int:year>/', YearArchiveView.as_view(), name='year_archive')

Micro uses the new simplified routing syntax which was introduced in Django 2.0. But if you’d like to use the regex-based routing syntax, just add regex=True to the decorator.

.. code-block:: python

@route(r'^articles/(?P<year>[0-9]{4})/$', regex=True)
def year_archive(request, year):
    return HttpResponse('hello')

You always can access the urlpatterns for the use low-level API.

.. code-block:: python

from django.urls import path
import django_micro as micro

micro.urlpatterns += [
    path('', homepage, name='homepage'),
]

Note: You can include third-party apps into Micro’s urlpatterns, but currently can’t use Micro as a third-party app. Micro is a singleton, and you can’t create more that one instance of it.

Models and migrations

Micro works well with models and migrations. Just define model in your app.py file. If you need migrations, create migrations directory next to the app.py and call python app.py makemigrations.

.. code-block::

blog
├── __init__.py
├── app.py
└── migrations
    ├── __init__.py
    └── 0001_initial.py

.. code-block:: python

from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=255)

    class Meta:
        app_label = 'blog'

Note: You always need to set app_label attribute in Meta of your models. For example, if application placed in blog/app.py, app_label should be blog.

For getting app_label you can use get_app_label shortcut.

.. code-block:: python

from django_micro import get_app_label

class Meta:
    app_label = get_app_label()

You also can place models separately in models.py file. In this case app_label is not required, but this is not a micro-way ;)

Management commands

Now you can create any management command without creating a file in yourapp/management/commands. Just defne command class in your app.py and wrap it to @command decorator.

.. code-block:: python

from django.core.management.base import BaseCommand
from django_micro import command

@command('print_hello')
class PrintHelloCommand(BaseCommand):
    def handle(self, *args, **options):
        self.stdout.write('Hello, Django!')

You also can create function-based commands.

.. code-block:: python

from django_micro import command

@command
def print_hello(cmd, **options):
    cmd.stdout.write('Hello, Django!')

Unfortunately, the command decorator uses a few dirty hacks for command registration. But everything works fine if you don’t think about it ;)

Custom template tags

Use template for register template tags. It works same as a register object in tag library file.

.. code-block:: python

from django_micro import template

@template.simple_tag
def print_hello(name):
    return 'Hello, {}!'

@template.filter
def remove_spaces(value):
    return value.replace(' ', '')

You don’t need to use the load tag. All template tags are global.

Testing

No magick. Use built-in test cases.

.. code-block:: python

from django.test import TestCase

class TestIndexView(TestCase):
    def test_success(self):
        response = self.client.get('/')
        self.assertEqual(response.status_code, 200)

To run tests which defined in app.py use the following command:

.. code-block::

$ python app.py test __main__

Admin interface

Django-admin requires lots of dependencies in apps and middlewares. We’ve realized that it’s not a simple way to add a huge list of apps to your config just to use the admin interface. So we added a shortcut django_admin=True to the configure function that automatically includes all the needed dependencies.

.. code-block:: python

from django.contrib import admin
from django_micro import configure

configure(locals(), django_admin=True)


class Post(models.Model):
    title = models.CharField(max_length=255)
    content = models.TextField(blank=True)
    create_date = models.DateTimeField(auto_now_add=True)

    class Meta:
        app_label = get_app_label()
        ordering = ('-create_date',)


@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    pass


route('admin/', admin.site.urls)

Credits

Django Micro is based on ideas from the following projects:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].