Package: django-micro
Django Micro is lightweight wrapper around Django that turns it to the microframework for writing small applications in a single file.tl;dr:See theexampleof full-featured application.What worksConfigurationViews and routesModels and migrationsManagement commandsCustom template tagsTestingAdmin interfaceThird party appsInstallation$ pip install django-microQuick startCreateapp.pyfile with following content.fromdjango_microimportconfigure,route,runfromdjango.httpimportHttpResponseDEBUG=Trueconfigure(locals())@route('',name='homepage')defhomepage(request):name=request.GET.get('name','World')returnHttpResponse('Hello,{}!'.format(name))application=run()Run the application.$ python app.py runserverNote:Parent directory of theapp.pyfile must have a valid python module name. Under the hood, Micro adds that directory toINSTALLED_APPSand uses it as a regular Django application.CompatibilityThe 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.0, <2.1Python version:>=3.4Run and deploymentOn the localhost the application runs with the built-inrunservercommand and deploys as a standard WSGI application.$ python app.py runserver
$ gunicorn example.app --bind localhost:8000
$ uwsgi --module example.app --http localhost:8000This behaviour is provided by the single string:application = run()which actually just a shortcut for the following code.if__name__=='__main__':fromdjango.core.managementimportexecute_from_command_lineexecute_from_command_line(sys.argv)else:fromdjango.core.wsgiimportget_wsgi_applicationapplication=get_wsgi_application()ConfigurationThe call of theconfigurefunction 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 callconfigurewithlocals()argument. Don’t worry, configure takes onlyUPPERCASEvariables.fromdjango_microimportconfigureDEBUG=Trueconfigure(locals())Views and routesRouting is wrapped in a single functionroute. You can use it as a decorator.fromdjango_microimportroute@route('blog/<int:year>/',name='year_archive')defyear_archive(request,year):returnHttpResponse('hello')Or as a regular function.defyear_archive(request):returnHttpResponse('hello')route('blog/<int:year>/',year_archive,name='year_archive')Alsoroutemay be used with class-based views.@route('blog/<int:year>/',name='year_archive')classYearArchiveView(View):defget(request,year):returnHttpResponse('hello')# or directlyroute('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 addregex=Trueto the decorator.@route(r'^articles/(?P<year>[0-9]{4})/$',regex=True)defyear_archive(request,year):returnHttpResponse('hello')You always can access theurlpatternsfor the use low-level API.fromdjango.urlsimportpathimportdjango_microasmicromicro.urlpatterns+=[path('',homepage,name='homepage'),]Note:You can include third-party apps into Micro’surlpatterns, 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 migrationsMicro works well with models and migrations. Just define model in yourapp.pyfile. If you need migrations, createmigrationsdirectory next to theapp.pyand callpython app.py makemigrations.blog
├── __init__.py
├── app.py
└── migrations
    ├── __init__.py
    └── 0001_initial.pyfromdjango.dbimportmodelsclassPost(models.Model):title=models.CharField(max_length=255)classMeta:app_label='blog'Note:You always need to setapp_labelattribute inMetaof your models. For example, if application placed inblog/app.py, app_label should beblog.For gettingapp_labelyou can useget_app_labelshortcut.fromdjango_microimportget_app_labelclassMeta:app_label=get_app_label()You also can place models separately inmodels.pyfile. In this caseapp_labelis not required, but this is not a micro-way ;)Management commandsNow you can create any management command without creating a file inyourapp/management/commands. Just defne command class in yourapp.pyand wrap it to@commanddecorator.fromdjango.core.management.baseimportBaseCommandfromdjango_microimportcommand@command('print_hello')classPrintHelloCommand(BaseCommand):defhandle(self,*args,**options):self.stdout.write('Hello, Django!')You also can create function-based commands.fromdjango_microimportcommand@commanddefprint_hello(cmd,**options):cmd.stdout.write('Hello, Django!')Unfortunately, thecommanddecorator uses a few dirty hacks for command registration. But everything works fine if you don’t think about it ;)Custom template tagsUsetemplatefor register template tags. It works same as aregisterobject in tag library file.fromdjango_microimporttemplate@template.simple_tagdefprint_hello(name):return'Hello,{}!'@template.filterdefremove_spaces(value):returnvalue.replace(' ','')You don’t need to use theloadtag. All template tags are global.TestingNo magick. Use built-in test cases.fromdjango.testimportTestCaseclassTestIndexView(TestCase):deftest_success(self):response=self.client.get('/')self.assertEqual(response.status_code,200)To run tests which defined in app.py use the following command:$ python app.py test __main__Admin interfaceDjango-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 shortcutdjango_admin=Trueto theconfigurefunction that automatically includes all the needed dependencies.fromdjango.contribimportadminfromdjango_microimportconfigureconfigure(locals(),django_admin=True)classPost(models.Model):title=models.CharField(max_length=255)content=models.TextField(blank=True)create_date=models.DateTimeField(auto_now_add=True)classMeta:app_label=get_app_label()ordering=('-create_date',)@admin.register(Post)classPostAdmin(admin.ModelAdmin):passroute('admin/',admin.site.urls)Who uses django-microstoragl— simple storage for screenshots and other shared files with short direct linksRelated projectsimportd— Popular implementation of django-as-microframework idea, but too  magical and over-engineered in my opinion.djmicro— Good and lightweight wrapper. I’ve took a few ideas from there. But it’s an experimental, undocumented and doesn’t develop anymore.