From a9f5652113f0721a7066e359ae28d14692ea3c47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vlastimil=20Z=C3=ADma?= Date: Fri, 25 Aug 2017 11:47:59 +0200 Subject: [PATCH] Fixed #28529 -- Fixed VariableDoesNotExist warnings in admin templates. --- django/contrib/admin/sites.py | 3 +++ django/contrib/admin/templates/admin/change_list.html | 6 +++--- .../contrib/admin/templates/admin/change_list_results.html | 2 +- .../contrib/admin/templates/admin/edit_inline/stacked.html | 2 +- .../contrib/admin/templates/admin/edit_inline/tabular.html | 6 +++--- django/contrib/admin/templates/admin/submit_line.html | 2 +- django/contrib/admin/templatetags/admin_list.py | 1 + django/contrib/admin/views/main.py | 1 + django/contrib/admin/widgets.py | 4 ++++ docs/ref/templates/api.txt | 6 +++--- 10 files changed, 21 insertions(+), 12 deletions(-) diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py index 0dafe9766b..6842f49684 100644 --- a/django/contrib/admin/sites.py +++ b/django/contrib/admin/sites.py @@ -300,6 +300,7 @@ class AdminSite: 'site_url': site_url, 'has_permission': self.has_permission(request), 'available_apps': self.get_app_list(request), + 'is_popup': False, } def password_change(self, request, extra_context=None): @@ -431,6 +432,8 @@ class AdminSite: 'name': capfirst(model._meta.verbose_name_plural), 'object_name': model._meta.object_name, 'perms': perms, + 'admin_url': None, + 'add_url': None, } if perms.get('change') or perms.get('view'): model_dict['view_only'] = not perms.get('change') diff --git a/django/contrib/admin/templates/admin/change_list.html b/django/contrib/admin/templates/admin/change_list.html index 0ffca672c0..768e581d1e 100644 --- a/django/contrib/admin/templates/admin/change_list.html +++ b/django/contrib/admin/templates/admin/change_list.html @@ -46,7 +46,7 @@ {% endblock %} {% endblock %} - {% if cl.formset.errors %} + {% if cl.formset and cl.formset.errors %}

{% if cl.formset.total_error_count == 1 %}{% trans "Please correct the error below." %}{% else %}{% trans "Please correct the errors below." %}{% endif %}

@@ -54,7 +54,7 @@ {% endif %}
{% block search %}{% search_form cl %}{% endblock %} - {% block date_hierarchy %}{% date_hierarchy cl %}{% endblock %} + {% block date_hierarchy %}{% if cl.date_hierarchy %}{% date_hierarchy cl %}{% endif %}{% endblock %} {% block filters %} {% if cl.has_filters %} @@ -65,7 +65,7 @@ {% endif %} {% endblock %} -
{% csrf_token %} + {% csrf_token %} {% if cl.formset %}
{{ cl.formset.management_form }}
{% endif %} diff --git a/django/contrib/admin/templates/admin/change_list_results.html b/django/contrib/admin/templates/admin/change_list_results.html index b3d7dd01d3..9b97b5b4f7 100644 --- a/django/contrib/admin/templates/admin/change_list_results.html +++ b/django/contrib/admin/templates/admin/change_list_results.html @@ -27,7 +27,7 @@ {% for result in results %} -{% if result.form.non_field_errors %} +{% if result.form and result.form.non_field_errors %} {{ result.form.non_field_errors }} {% endif %} {% for item in result %}{{ item }}{% endfor %} diff --git a/django/contrib/admin/templates/admin/edit_inline/stacked.html b/django/contrib/admin/templates/admin/edit_inline/stacked.html index 507f69bc56..8af4d54791 100644 --- a/django/contrib/admin/templates/admin/edit_inline/stacked.html +++ b/django/contrib/admin/templates/admin/edit_inline/stacked.html @@ -19,7 +19,7 @@ {% include "admin/includes/fieldset.html" %} {% endfor %} {% if inline_admin_form.needs_explicit_pk_field %}{{ inline_admin_form.pk_field.field }}{% endif %} - {{ inline_admin_form.fk_field.field }} + {% if inline_admin_form.fk_field %}{{ inline_admin_form.fk_field.field }}{% endif %}
{% endfor %} diff --git a/django/contrib/admin/templates/admin/edit_inline/tabular.html b/django/contrib/admin/templates/admin/edit_inline/tabular.html index 2584745ef9..2395d1cb6d 100644 --- a/django/contrib/admin/templates/admin/edit_inline/tabular.html +++ b/django/contrib/admin/templates/admin/edit_inline/tabular.html @@ -36,12 +36,12 @@ {% if inline_admin_form.show_url %}{% trans "View on site" %}{% endif %}

{% endif %} {% if inline_admin_form.needs_explicit_pk_field %}{{ inline_admin_form.pk_field.field }}{% endif %} - {{ inline_admin_form.fk_field.field }} + {% if inline_admin_form.fk_field %}{{ inline_admin_form.fk_field.field }}{% endif %} {% spaceless %} {% for fieldset in inline_admin_form %} {% for line in fieldset %} {% for field in line %} - {% if field.field.is_hidden %} {{ field.field }} {% endif %} + {% if not field.is_readonly and field.field.is_hidden %}{{ field.field }}{% endif %} {% endfor %} {% endfor %} {% endfor %} @@ -50,7 +50,7 @@ {% for fieldset in inline_admin_form %} {% for line in fieldset %} {% for field in line %} - {% if not field.field.is_hidden %} + {% if field.is_readonly or not field.field.is_hidden %} {% if field.is_readonly %}

{{ field.contents }}

diff --git a/django/contrib/admin/templates/admin/submit_line.html b/django/contrib/admin/templates/admin/submit_line.html index b9467e82b7..bb283fe9e0 100644 --- a/django/contrib/admin/templates/admin/submit_line.html +++ b/django/contrib/admin/templates/admin/submit_line.html @@ -2,7 +2,7 @@
{% block submit-row %} {% if show_save %}{% endif %} -{% if show_delete_link %} +{% if show_delete_link and original %} {% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %} {% endif %} diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py index 22dec6994f..0211452d17 100644 --- a/django/contrib/admin/templatetags/admin_list.py +++ b/django/contrib/admin/templatetags/admin_list.py @@ -418,6 +418,7 @@ def date_hierarchy(cl): years = getattr(cl.queryset, 'dates')(field_name, 'year') return { 'show': True, + 'back': None, 'choices': [{ 'link': link({year_field: str(year.year)}), 'title': str(year.year), diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py index 3febd42fb9..2f5616f4bd 100644 --- a/django/contrib/admin/views/main.py +++ b/django/contrib/admin/views/main.py @@ -46,6 +46,7 @@ class ChangeList: self.list_display = list_display self.list_display_links = list_display_links self.list_filter = list_filter + self.has_filters = None self.date_hierarchy = date_hierarchy self.search_fields = search_fields self.list_select_related = list_select_related diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index 5bf54da7d6..835e653ba6 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -154,8 +154,12 @@ class ForeignKeyRawIdWidget(forms.TextInput): context['link_title'] = _('Lookup') # The JavaScript code looks for this class. context['widget']['attrs'].setdefault('class', 'vForeignKeyRawIdAdminField') + else: + context['related_url'] = None if context['widget']['value']: context['link_label'], context['link_url'] = self.label_and_url_for_value(value) + else: + context['link_label'] = None return context def base_url_parameters(self): diff --git a/docs/ref/templates/api.txt b/docs/ref/templates/api.txt index c1a1754b29..f37c0a65c2 100644 --- a/docs/ref/templates/api.txt +++ b/docs/ref/templates/api.txt @@ -371,9 +371,9 @@ replaced with the name of the invalid variable. While ``string_if_invalid`` can be a useful debugging tool, it is a bad idea to turn it on as a 'development default'. - Many templates, including those in the Admin site, rely upon the silence - of the template system when a nonexistent variable is encountered. If you - assign a value other than ``''`` to ``string_if_invalid``, you will + Many templates, including some of the Django's templates, rely upon the + silence of the template system when a nonexistent variable is encountered. + If you assign a value other than ``''`` to ``string_if_invalid``, you will experience rendering problems with these templates and sites. Generally, ``string_if_invalid`` should only be enabled in order to debug