Skip to content

Instantly share code, notes, and snippets.

@daimon99
Last active September 5, 2019 04:30
Show Gist options
  • Save daimon99/2061514448c31cc30f3371a99d473ad8 to your computer and use it in GitHub Desktop.
Save daimon99/2061514448c31cc30f3371a99d473ad8 to your computer and use it in GitHub Desktop.
django默认显示只读页面,点击编辑之后再进入编辑页面
@admin.register(m.Article)
class ArticleAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
if 'edit' in request.GET or '/add/' == request.path[-5:]:
return self.readonly_fields
else:
return self.get_fields(request, obj)
{% extends "admin/change_form.html" %}
{% load i18n admin_urls static admin_modify %}
{% block object-tools-items %}
{% if not request.GET.edit %}
<li><a href="?edit=1">修改</a></li>
{% endif %}
{{ block.super }}
{% endblock %}
{% if save_on_top %}{% block submit_buttons_top %}{% if add or request.GET.edit %}{% submit_row %}{% endif %}{% endblock %}{% endif %}
{% block submit_buttons_bottom %}
{% if add or request.GET.edit %}{% submit_row %}{% endif %}
{% endblock %}
@daimon99
Copy link
Author

daimon99 commented Sep 5, 2019

效果演示:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment