django-etc documentation¶
https://github.com/idlesign/django-etc
Description¶
Tiny stuff for Django that won’t fit into separate apps.
Note
Add the etc application to INSTALLED_APPS in your settings file (usually ‘settings.py’).
Requirements¶
- Python 3.6+
- Django 2.0+
Table of Contents¶
Tools for Django Admin¶
CustomModelPage¶
etc.admin.CustomModelPage allows easy construction of custom admin pages processing user input.
Use it if you need to perform some action in admin requiring user input.
from django.db import models
from django.http import HttpResponse
from etc.admin import CustomModelPage, admins
from etc.tests.testapp.models import MyChildModel1
class MyPageModelAdmin(admins.CustomPageModelAdmin):
fields = (
'my_field', 'moy_relation'
)
autocomplete_fields = (
'my_relation',
)
class MyPage(CustomModelPage):
title = 'Test page 1' # set page title
# Define some fields.
my_field = models.CharField('some title', max_length=10)
my_relation = models.ForeignKey(MyChildModel1, null=True, on_delete=models.CASCADE)
admin_cls = MyPageModelAdmin # set admin class for this page
def save(self):
... # Implement data handling from self attributes here.
# self.bound_admin has some useful methods.
# self.bound_request allows you to access current HTTP request.
self.bound_admin.message_success(self.bound_request, f'Hey, done!')
super().save()
# to return a custom response you can assign self.bound_response
# this can be useful, e.g. for file downloads
self.bound_response = HttpResponse(b'%)')
# Register my page within Django admin.
MyPage.register()
Various utils¶
import_app_module¶
etc.toolbox.import_app_module imports and returns a module from a specific app by its name.
If your application provides some kind of tooling for others and you know that configuration for this tooling could be found in a certain module within a thirdparty app you can use this function to load such a module by its name.
from etc.toolbox import import_app_module
module = import_app_module('someapp', 'mymodule') # Get `mymodule` module from `someapp` application.
import_project_modules¶
etc.toolbox.import_project_modules imports modules from registered apps using given module name and returns them as a list.
This is an automation for import_app_module() described above to load all modules from every app in a project.
from etc.toolbox import import_project_modules
all_modules = import_project_modules('mymodule') # Get `mymodule` module from every app in a project.
get_site_url¶
etc.toolbox.get_site_url does its best to provide you with a site URL where request object is unavailable.
On occasions when you do not have a request object to get current site URL from (e.g. background tasks) this function tries to get it from environment and settings, using the following order:
- (SITE_PROTO or SITE_SCHEME) + SITE_DOMAIN
- SITE_URL
- Django Sites contrib
- Request object (if available)
from etc.toolbox import get_site_url
my_url = get_site_url()
etc_misc Template Tags¶
site_url tag.
Does its best to provide you with a site URL whether request object is unavailable or not. See
get_site_url
description above.{% load etc_misc %} {% site_url %}
include_ tag.
Similar to built-in
include
template tag, but allowing template variables to be used in template name and a fallback template, thus making the tag more dynamic.Warning
Requires Django 1.8+
{% load etc_misc %} {% include_ "sub_{{ postfix_var }}.html" fallback "default.html" %}
Get involved into django-etc¶
Submit issues. If you spotted something weird in application behavior or want to propose a feature you can do that at https://github.com/idlesign/django-etc/issues
Write code. If you are eager to participate in application development, fork it at https://github.com/idlesign/django-etc, write your code, whether it should be a bugfix or a feature implementation, and make a pull request right from the forked project page.
Spread the word. If you have some tips and tricks or any other words in mind that you think might be of interest for the others — publish it.