|
| 1 | +title: django.contrib.admin.helpers AdminForm code examples |
| 2 | +category: page |
| 3 | +slug: django-contrib-admin-helpers-adminform-examples |
| 4 | +sortorder: 500011021 |
| 5 | +toc: False |
| 6 | +sidebartitle: django.contrib.admin.helpers AdminForm |
| 7 | +meta: Python example code for the AdminForm class from the django.contrib.admin.helpers module of the Django project. |
| 8 | + |
| 9 | + |
| 10 | +AdminForm is a class within the django.contrib.admin.helpers module of the Django project. |
| 11 | + |
| 12 | + |
| 13 | +## Example 1 from django-cms |
| 14 | +[django-cms](https://github.com/divio/django-cms) |
| 15 | +([project website](https://www.django-cms.org/en/)) is a Python-based |
| 16 | +content management system (CMS) [library](https://pypi.org/project/django-cms/) |
| 17 | +for use with Django web apps that is open sourced under the |
| 18 | +[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE) |
| 19 | +license. |
| 20 | + |
| 21 | +[**django-cms / cms / admin / placeholderadmin.py**](https://github.com/divio/django-cms/blob/develop/cms/admin/placeholderadmin.py) |
| 22 | + |
| 23 | +```python |
| 24 | +# placeholderadmin.py |
| 25 | +import uuid |
| 26 | +import warnings |
| 27 | + |
| 28 | +from django.conf.urls import url |
| 29 | +~~from django.contrib.admin.helpers import AdminForm |
| 30 | +from django.contrib.admin.utils import get_deleted_objects |
| 31 | +from django.core.exceptions import PermissionDenied |
| 32 | +from django.db import router, transaction |
| 33 | +from django.http import ( |
| 34 | + HttpResponse, |
| 35 | + HttpResponseBadRequest, |
| 36 | + HttpResponseForbidden, |
| 37 | + HttpResponseNotFound, |
| 38 | + HttpResponseRedirect, |
| 39 | +) |
| 40 | +from django.shortcuts import get_list_or_404, get_object_or_404, render |
| 41 | +from django.template.response import TemplateResponse |
| 42 | +from django.utils.decorators import method_decorator |
| 43 | +from django.utils.encoding import force_text |
| 44 | +from django.utils import translation |
| 45 | +from django.utils.translation import ugettext as _ |
| 46 | +from django.views.decorators.clickjacking import xframe_options_sameorigin |
| 47 | +from django.views.decorators.http import require_POST |
| 48 | + |
| 49 | +from six.moves.urllib.parse import parse_qsl, urlparse |
| 50 | + |
| 51 | +from six import get_unbound_function, get_method_function |
| 52 | + |
| 53 | +from cms import operations |
| 54 | + |
| 55 | + |
| 56 | +## ... source file abbreviated to get to AdminForm examples ... |
| 57 | + |
| 58 | + |
| 59 | + context = { |
| 60 | + 'opts': opts, |
| 61 | + 'message': force_text(_("Field %s not found")) % raw_fields |
| 62 | + } |
| 63 | + return render(request, 'admin/cms/page/plugin/error_form.html', context) |
| 64 | + if not request.user.has_perm("{0}.change_{1}".format(self.model._meta.app_label, |
| 65 | + self.model._meta.model_name)): |
| 66 | + context = { |
| 67 | + 'opts': opts, |
| 68 | + 'message': force_text(_("You do not have permission to edit this item")) |
| 69 | + } |
| 70 | + return render(request, 'admin/cms/page/plugin/error_form.html', context) |
| 71 | + form_class = self.get_form(request, obj, fields=fields) |
| 72 | + if not cancel_clicked and request.method == 'POST': |
| 73 | + form = form_class(instance=obj, data=request.POST) |
| 74 | + if form.is_valid(): |
| 75 | + form.save() |
| 76 | + saved_successfully = True |
| 77 | + else: |
| 78 | + form = form_class(instance=obj) |
| 79 | +~~ admin_form = AdminForm(form, fieldsets=[(None, {'fields': fields})], prepopulated_fields={}, |
| 80 | + model_admin=self) |
| 81 | + media = self.media + admin_form.media |
| 82 | + context = { |
| 83 | + 'CMS_MEDIA_URL': get_cms_setting('MEDIA_URL'), |
| 84 | + 'title': opts.verbose_name, |
| 85 | + 'plugin': None, |
| 86 | + 'plugin_id': None, |
| 87 | + 'adminform': admin_form, |
| 88 | + 'add': False, |
| 89 | + 'is_popup': True, |
| 90 | + 'media': media, |
| 91 | + 'opts': opts, |
| 92 | + 'change': True, |
| 93 | + 'save_as': False, |
| 94 | + 'has_add_permission': False, |
| 95 | + 'window_close_timeout': 10, |
| 96 | + } |
| 97 | + if cancel_clicked: |
| 98 | + context.update({ |
| 99 | + 'cancel': True, |
| 100 | + }) |
| 101 | + return render(request, 'admin/cms/page/plugin/confirm_form.html', context) |
| 102 | + if not cancel_clicked and request.method == 'POST' and saved_successfully: |
| 103 | + return render(request, 'admin/cms/page/plugin/confirm_form.html', context) |
| 104 | + |
| 105 | + |
| 106 | +## ... source file continues with no further AdminForm examples... |
| 107 | + |
| 108 | +``` |
| 109 | + |
0 commit comments