{"id":13936743,"url":"https://github.com/maxpoletaev/django-micro","last_synced_at":"2025-12-13T18:42:57.885Z","repository":{"id":54988165,"uuid":"48293531","full_name":"maxpoletaev/django-micro","owner":"maxpoletaev","description":"Django as a microframework","archived":false,"fork":false,"pushed_at":"2021-01-17T13:47:28.000Z","size":68,"stargazers_count":279,"open_issues_count":3,"forks_count":21,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-04-02T11:49:32.951Z","etag":null,"topics":["django","microframework","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maxpoletaev.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGES.rst","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-19T18:52:23.000Z","updated_at":"2025-03-12T07:22:08.000Z","dependencies_parsed_at":"2022-08-14T08:20:13.758Z","dependency_job_id":null,"html_url":"https://github.com/maxpoletaev/django-micro","commit_stats":null,"previous_names":["zenwalker/django-micro"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxpoletaev%2Fdjango-micro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxpoletaev%2Fdjango-micro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxpoletaev%2Fdjango-micro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxpoletaev%2Fdjango-micro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxpoletaev","download_url":"https://codeload.github.com/maxpoletaev/django-micro/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248075054,"owners_count":21043518,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","microframework","python"],"created_at":"2024-08-07T23:02:57.364Z","updated_at":"2025-12-13T18:42:52.829Z","avatar_url":"https://github.com/maxpoletaev.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"============\nDjango Micro\n============\n\n.. image::\n    https://img.shields.io/pypi/v/django-micro.svg\n    :target: https://pypi.python.org/pypi/django-micro\n\n.. image::\n    https://img.shields.io/badge/status-stable-brightgreen.svg\n\nDjango Micro is lightweight wrapper around Django that turns it to the microframework for writing small applications in a single file.\n\n**tl;dr:** See the example_ of full-featured application.\n\n\nWhat works\n==========\n\n- `Configuration`_\n- `Views and routes`_\n- `Models and migrations`_\n- `Management commands`_\n- `Custom template tags`_\n- `Testing`_\n- `Admin interface`_\n- Third party apps\n\n\nInstallation\n============\n\n.. code-block::\n\n    $ pip install django-micro\n\n\nQuick start\n===========\n\nCreate ``app.py`` file with following content.\n\n.. code-block:: python\n\n    from django_micro import configure, route, run\n    from django.http import HttpResponse\n\n    DEBUG = True\n    configure(locals())\n\n\n    @route('', name='homepage')\n    def homepage(request):\n        name = request.GET.get('name', 'World')\n        return HttpResponse('Hello, {}!'.format(name))\n\n\n    application = run()\n\nRun the application.\n\n.. code-block::\n\n    $ python app.py runserver\n\n**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.\n\n\nCompatibility\n=============\n\nThe 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.\n\n- **Django version:** \u003e=2.1\n- **Python version:** \u003e=3.4\n\n\nRun and deployment\n==================\n\nOn the localhost the application runs with the built-in ``runserver`` command and deploys as a standard WSGI application.\n\n.. code-block::\n\n    $ python app.py runserver\n    $ gunicorn example.app --bind localhost:8000\n    $ uwsgi --module example.app --http localhost:8000\n\nThis behaviour is provided by the single string: ``application = run()`` which actually just a shortcut for the following code.\n\n.. code-block:: python\n\n    if __name__ == '__main__':\n        from django.core.management import execute_from_command_line\n        execute_from_command_line(sys.argv)\n    else:\n        from django.core.wsgi import get_wsgi_application\n        application = get_wsgi_application()\n\n\nConfiguration\n=============\n\nThe 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.\n\nI recommend to define all the configuration in the global namespace and call ``configure`` with ``locals()`` argument. Don’t worry, configure takes only *UPPERCASE* variables.\n\n.. code-block:: python\n\n    from django_micro import configure\n\n    DEBUG = True\n\n    configure(locals())\n\n\nViews and routes\n================\n\nRouting is wrapped in a single function ``route``. You can use it as a decorator.\n\n.. code-block:: python\n\n    from django_micro import route\n\n    @route('blog/\u003cint:year\u003e/', name='year_archive')\n    def year_archive(request, year):\n        return HttpResponse('hello')\n\nOr as a regular function.\n\n.. code-block:: python\n\n    def year_archive(request):\n        return HttpResponse('hello')\n\n    route('blog/\u003cint:year\u003e/', year_archive, name='year_archive')\n\nAlso ``route`` may be used with class-based views.\n\n.. code-block:: python\n\n    @route('blog/\u003cint:year\u003e/', name='year_archive')\n    class YearArchiveView(View):\n        def get(request, year):\n            return HttpResponse('hello')\n\n    # or directly\n    route('blog/\u003cint:year\u003e/', YearArchiveView.as_view(), name='year_archive')\n\nMicro 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.\n\n.. code-block:: python\n\n    @route(r'^articles/(?P\u003cyear\u003e[0-9]{4})/$', regex=True)\n    def year_archive(request, year):\n        return HttpResponse('hello')\n\nYou always can access the ``urlpatterns`` for the use low-level API.\n\n.. code-block:: python\n\n    from django.urls import path\n    import django_micro as micro\n\n    micro.urlpatterns += [\n        path('', homepage, name='homepage'),\n    ]\n\n\n**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.\n\n\nModels and migrations\n=====================\n\nMicro 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``.\n\n.. code-block::\n\n    blog\n    ├── __init__.py\n    ├── app.py\n    └── migrations\n        ├── __init__.py\n        └── 0001_initial.py\n\n.. code-block:: python\n\n    from django.db import models\n\n    class Post(models.Model):\n        title = models.CharField(max_length=255)\n\n        class Meta:\n            app_label = 'blog'\n\n**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``.\n\nFor getting ``app_label`` you can use ``get_app_label`` shortcut.\n\n.. code-block:: python\n\n    from django_micro import get_app_label\n\n    class Meta:\n        app_label = get_app_label()\n\nYou also can place models separately in ``models.py`` file. In this case ``app_label`` is not required, but this is not a micro-way ;)\n\n\nManagement commands\n===================\n\nNow 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.\n\n.. code-block:: python\n\n    from django.core.management.base import BaseCommand\n    from django_micro import command\n\n    @command('print_hello')\n    class PrintHelloCommand(BaseCommand):\n        def handle(self, *args, **options):\n            self.stdout.write('Hello, Django!')\n\nYou also can create function-based commands.\n\n.. code-block:: python\n\n    from django_micro import command\n\n    @command\n    def print_hello(cmd, **options):\n        cmd.stdout.write('Hello, Django!')\n\nUnfortunately, the ``command`` decorator uses a few dirty hacks for command registration. But everything works fine if you don’t think about it ;)\n\n\nCustom template tags\n====================\n\nUse ``template`` for register template tags. It works same as a ``register`` object in tag library file.\n\n.. code-block:: python\n\n    from django_micro import template\n\n    @template.simple_tag\n    def print_hello(name):\n        return 'Hello, {}!'\n\n    @template.filter\n    def remove_spaces(value):\n        return value.replace(' ', '')\n\n\nYou don’t need to use the ``load`` tag. All template tags are global.\n\n\nTesting\n=======\n\nNo magick. Use built-in test cases.\n\n.. code-block:: python\n\n    from django.test import TestCase\n\n    class TestIndexView(TestCase):\n        def test_success(self):\n            response = self.client.get('/')\n            self.assertEqual(response.status_code, 200)\n\nTo run tests which defined in app.py use the following command:\n\n.. code-block::\n\n    $ python app.py test __main__\n\n\nAdmin interface\n===============\n\nDjango-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.\n\n.. code-block:: python\n\n    from django.contrib import admin\n    from django_micro import configure\n\n    configure(locals(), django_admin=True)\n\n\n    class Post(models.Model):\n        title = models.CharField(max_length=255)\n        content = models.TextField(blank=True)\n        create_date = models.DateTimeField(auto_now_add=True)\n\n        class Meta:\n            app_label = get_app_label()\n            ordering = ('-create_date',)\n\n\n    @admin.register(Post)\n    class PostAdmin(admin.ModelAdmin):\n        pass\n\n\n    route('admin/', admin.site.urls)\n\n\nCredits\n=======\n\nDjango Micro is based on ideas from the following projects:\n\n- https://softwaremaniacs.org/blog/2011/01/07/django-micro-framework/en/\n- https://github.com/apendleton/djmicro\n- https://github.com/amitu/importd\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxpoletaev%2Fdjango-micro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxpoletaev%2Fdjango-micro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxpoletaev%2Fdjango-micro/lists"}