|
| 1 | +title: django.shortcuts get_list_or_404 Example Code |
| 2 | +category: page |
| 3 | +slug: django-shortcuts-get-list-or-404-examples |
| 4 | +sortorder: 500011345 |
| 5 | +toc: False |
| 6 | +sidebartitle: django.shortcuts get_list_or_404 |
| 7 | +meta: Python example code for the get_list_or_404 callable from the django.shortcuts module of the Django project. |
| 8 | + |
| 9 | + |
| 10 | +get_list_or_404 is a callable within the django.shortcuts 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 | +from cms.admin.forms import PluginAddValidationForm |
| 55 | +from cms.constants import SLUG_REGEXP |
| 56 | +from cms.exceptions import PluginLimitReached |
| 57 | +from cms.models.placeholdermodel import Placeholder |
| 58 | +from cms.models.placeholderpluginmodel import PlaceholderReference |
| 59 | +from cms.models.pluginmodel import CMSPlugin |
| 60 | +from cms.plugin_pool import plugin_pool |
| 61 | +from cms.signals import pre_placeholder_operation, post_placeholder_operation |
| 62 | +from cms.toolbar.utils import get_plugin_tree_as_json |
| 63 | +from cms.utils import copy_plugins, get_current_site |
| 64 | +from cms.utils.compat import DJANGO_2_0 |
| 65 | + |
| 66 | + |
| 67 | +## ... source file abbreviated to get to get_list_or_404 examples ... |
| 68 | + |
| 69 | + |
| 70 | + operation=operation, |
| 71 | + request=request, |
| 72 | + language=self._get_operation_language(request), |
| 73 | + token=token, |
| 74 | + origin=self._get_operation_origin(request), |
| 75 | + **kwargs |
| 76 | + ) |
| 77 | + return token |
| 78 | + |
| 79 | + def _send_post_placeholder_operation(self, request, operation, token, **kwargs): |
| 80 | + if not request.GET.get('cms_path'): |
| 81 | + return |
| 82 | + |
| 83 | + post_placeholder_operation.send( |
| 84 | + sender=self.__class__, |
| 85 | + operation=operation, |
| 86 | + request=request, |
| 87 | + language=self._get_operation_language(request), |
| 88 | + token=token, |
| 89 | + origin=self._get_operation_origin(request), |
| 90 | + **kwargs |
| 91 | + ) |
| 92 | + |
| 93 | + def _get_plugin_from_id(self, plugin_id): |
| 94 | + queryset = CMSPlugin.objects.values_list('plugin_type', flat=True) |
| 95 | +~~ plugin_type = get_list_or_404(queryset, pk=plugin_id)[0] |
| 96 | + plugin_class = plugin_pool.get_plugin(plugin_type) |
| 97 | + real_queryset = plugin_class.get_render_queryset().select_related('parent', 'placeholder') |
| 98 | + return get_object_or_404(real_queryset, pk=plugin_id) |
| 99 | + |
| 100 | + def get_urls(self): |
| 101 | + info = "%s_%s" % (self.model._meta.app_label, self.model._meta.model_name) |
| 102 | + pat = lambda regex, fn: url(regex, self.admin_site.admin_view(fn), name='%s_%s' % (info, fn.__name__)) |
| 103 | + url_patterns = [ |
| 104 | + pat(r'copy-plugins/$', self.copy_plugins), |
| 105 | + pat(r'add-plugin/$', self.add_plugin), |
| 106 | + pat(r'edit-plugin/(%s)/$' % SLUG_REGEXP, self.edit_plugin), |
| 107 | + pat(r'delete-plugin/(%s)/$' % SLUG_REGEXP, self.delete_plugin), |
| 108 | + pat(r'clear-placeholder/(%s)/$' % SLUG_REGEXP, self.clear_placeholder), |
| 109 | + pat(r'move-plugin/$', self.move_plugin), |
| 110 | + ] |
| 111 | + return url_patterns + super(PlaceholderAdminMixin, self).get_urls() |
| 112 | + |
| 113 | + def has_add_plugin_permission(self, request, placeholder, plugin_type): |
| 114 | + return placeholder.has_add_plugin_permission(request.user, plugin_type) |
| 115 | + |
| 116 | + def has_change_plugin_permission(self, request, plugin): |
| 117 | + placeholder = plugin.placeholder |
| 118 | + return placeholder.has_change_plugin_permission(request.user, plugin) |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +## ... source file continues with no further get_list_or_404 examples... |
| 123 | + |
| 124 | +``` |
| 125 | + |
0 commit comments