def object_detail(request, req_context, content_object, extra_context=None, view_name='detail', template_name=None, template_object_name=None): """ Generic detail of an object. """ obj = content_object if not template_object_name: template_object_name = get_object_name(obj) if not template_name: template_name = "%s/%s_%s.html" % (get_app_label(obj), get_object_name(obj), view_name) t = loader.get_template(template_name) req_context.update({template_object_name: obj}) if extra_context: for key, value in extra_context.items(): if callable(value): req_context[key] = value() else: req_context[key] = value return t.render(req_context)
def get_urls(self): urlpatterns = patterns( '', url(r'^$', self.index, name='index'), #JSON views for AJAX updating of admin fields url(r'^collection_categories_json$', self.collection_categories_json), url(r'^layout_regions_json$', self.layout_regions_json), url(r'^registered_region_views_json$', self.registered_region_views_json), url(r'^registered_standard_views_json$', self.registered_standard_views_json), url(r'^objects_for_ctype_json$', self.objects_for_ctype_json), url(r'^menu_items_for_menu_json$', self.menu_items_for_menu_json), url(r'^options_view_widget_html$', self.options_view_widget_html), url(r'^api/create/$', self.api_create_content), ) # url patterns for registered views for model, model_views in self._registry.items(): model_name = get_object_name(model) for view in model_views: url_pattern = self.get_url_pattern_for_view(view, model_name) url_name = self.get_url_name_for_view(view, model_name) urlpatterns += patterns('', url(url_pattern, view, name=url_name)) if view.is_default: urlpatterns += patterns( '', url(url_pattern, view, name=model_name)) # url patterns for menu items urlpatterns.extend(self.get_menuitem_urls()) return urlpatterns
def render(self, name, value, attrs=None): # if it is inline build the proper ct_field name ct_field = self.ct_field if "__prefix__" in name: ct_field = re.sub('__prefix__.*', "__prefix__-" + ct_field, name) elif re.match('.*\-(\d+\-).*', name): ct_field = re.sub('(\d+\-).*', "\g<1>" + ct_field , name) self._test_ct_field = ct_field # for testing purposes only actual_object = self.get_actual_object(value) if attrs is None: attrs = {} params = self.url_parameters() d = { 'url': ('?' + '&'.join(['%s=%s' % (k, v) for k, v in params.iteritems()])) if params else "", 'obj_repr': do_render_object(actual_object) if actual_object else "", 'value': value, 'name': name, 'ct_field': ct_field, 'static_admin': settings.STATIC_URL + "admin/", 'content_types': [(ContentType.objects.get_for_model(ct).pk, get_app_label(ct), get_object_name(ct)) \ for ct in self.cts], } return render_to_string(self.template, d)
def inline_template(obj, inline_view_name): """ Returns the name of the requested inline template for the object obj. Eg: {{ article|inline_template:"teaser" }} -> "articles/article_teaser.html" """ return "%s/%s_%s.html" % (get_app_label(obj), get_object_name(obj), inline_view_name)
def object_list(request, req_context, queryset, view_name='list', paginate_by=None, page=None, allow_empty=True, template_name=None, template_loader=loader, extra_context=None, template_object_name=None, mimetype=None): """ Generic list of objects. Templates: ``<app_label>/<model_name>_list.html`` Context: object_list list of objects is_paginated are the results paginated? results_per_page number of objects per page (if paginated) has_next is there a next page? has_previous is there a prev page? page the current page next the next page previous the previous page pages number of pages, total hits number of objects, total last_on_page the result number of the last of object in the object_list (1-indexed) first_on_page the result number of the first object in the object_list (1-indexed) page_range: A list of the page numbers (1-indexed). """ if extra_context is None: extra_context = {} queryset = queryset._clone() if not template_object_name: template_object_name = get_object_name(queryset.model) if paginate_by: paginator = Paginator(queryset, paginate_by, allow_empty_first_page=allow_empty) if not page: page = request.GET.get('page', 1) try: page_number = int(page) except ValueError: if page == 'last': page_number = paginator.num_pages else: # Page is not 'last', nor can it be converted to an int. raise Http404 try: page_obj = paginator.page(page_number) except InvalidPage: raise Http404 req_context.update({ '%s_%s' % (template_object_name, view_name): page_obj.object_list, 'paginator': paginator, 'page_obj': page_obj, # Legacy template context stuff. New templates should use page_obj # to access this instead. 'is_paginated': page_obj.has_other_pages(), 'results_per_page': paginator.per_page, 'has_next': page_obj.has_next(), 'has_previous': page_obj.has_previous(), 'page': page_obj.number, 'next': page_obj.next_page_number(), 'previous': page_obj.previous_page_number(), 'first_on_page': page_obj.start_index(), 'last_on_page': page_obj.end_index(), 'pages': paginator.num_pages, 'hits': paginator.count, 'page_range': paginator.page_range, }) else: req_context.update({ '%s_list' % template_object_name: queryset, 'paginator': None, 'page_obj': None, 'is_paginated': False, }) if not allow_empty and len(queryset) == 0: raise Http404 for key, value in extra_context.items(): if callable(value): req_context[key] = value() else: req_context[key] = value if not template_name: model = queryset.model template_name = "%s/%s_%s.html" % (get_app_label(model), get_object_name(model), view_name) t = template_loader.get_template(template_name) return t.render(req_context)
def get_object_name(cls): return get_object_name(cls)
def get_absolute_url(self): return '/%s/%s/' % (get_object_name(self), self.slug)