Djangoフレームワークのadmin高度カスタマイズ
2976 ワード
インテリジェントポイント
概要
リファレンスドキュメント
コードアドレス:SpareibsのGithub
じっけんステップ
単純テスト
まず自分のモデルを定義します【models.py】from django.db import models
# Create your models here.
class Poem(models.Model):
author = models.CharField(max_length=100)
title = models.CharField(max_length=200)
timestamp = models.DateField(auto_now_add=True, auto_now=False)
updated = models.DateField(auto_now_add=False, auto_now=True)
def __str__(self):
return "%s" % self.title
Poemのmodelsをモデルに登録すると【admin.py】Pomeの内容を削除して変更できますfrom django.contrib import admin
from .models import Poem
admin.site.register(Poem)
中国語表示【settings】
注意:googleブラウザはだめですが、firefoxブラウザはできます.MIDDLEWARE_CLASSES = [
'django.middleware.locale.LocaleMiddleware',
]
独自のUserAdminのカスタマイズ
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class MyUserAdmin(UserAdmin):
#
list_display = ('email', 'first_name', 'last_name', 'is_staff')
#
list_filter = ('is_staff',)
#
search_fields = ('last_name',)
# User , MyUserAdmin
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
カスタムPomeのカスタマイズ
class PoemModelAdmin(admin.ModelAdmin):
#
list_display = ['title', 'timestamp', 'author']
#
list_display_links = ['author']
# ,
search_fields = ['title']
# ( )
list_editable = ['title']
#
list_filter = ['author']
class Meta:
model = Poem
admin.site.register(Poem, PoemModelAdmin)
カスタムテンプレート表示
カスタムテンプレートを追加【cahnge_form.html】{% extends "admin/change_form.html" %}
{% block form_top %}
: !
{% endblock %}
adminで【admin.py】を使用class PoemModelAdmin(admin.ModelAdmin):
#
list_display = ['title', 'timestamp', 'author']
#
list_display_links = ['author']
# ,
search_fields = ['title']
# ( )
list_editable = ['title']
#
list_filter = ['author']
#
change_form_template = 'change_form.html'
class Meta:
model = Poem
admin.site.register(Poem, PoemModelAdmin)
カスタマイズ可能なテンプレート、ソースコード【option.py】を表示する
# Custom templates (designed to be over-ridden in subclasses)
add_form_template = None
change_form_template = None
change_list_template = None
delete_confirmation_template = None
delete_selected_confirmation_template = None
object_history_template = None
単純テスト
まず自分のモデルを定義します【models.py】
from django.db import models
# Create your models here.
class Poem(models.Model):
author = models.CharField(max_length=100)
title = models.CharField(max_length=200)
timestamp = models.DateField(auto_now_add=True, auto_now=False)
updated = models.DateField(auto_now_add=False, auto_now=True)
def __str__(self):
return "%s" % self.title
Poemのmodelsをモデルに登録すると【admin.py】Pomeの内容を削除して変更できます
from django.contrib import admin
from .models import Poem
admin.site.register(Poem)
中国語表示【settings】
注意:googleブラウザはだめですが、firefoxブラウザはできます.
MIDDLEWARE_CLASSES = [
'django.middleware.locale.LocaleMiddleware',
]
独自のUserAdminのカスタマイズ
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class MyUserAdmin(UserAdmin):
#
list_display = ('email', 'first_name', 'last_name', 'is_staff')
#
list_filter = ('is_staff',)
#
search_fields = ('last_name',)
# User , MyUserAdmin
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
カスタムPomeのカスタマイズ
class PoemModelAdmin(admin.ModelAdmin):
#
list_display = ['title', 'timestamp', 'author']
#
list_display_links = ['author']
# ,
search_fields = ['title']
# ( )
list_editable = ['title']
#
list_filter = ['author']
class Meta:
model = Poem
admin.site.register(Poem, PoemModelAdmin)
カスタムテンプレート表示
カスタムテンプレートを追加【cahnge_form.html】
{% extends "admin/change_form.html" %}
{% block form_top %}
: !
{% endblock %}
adminで【admin.py】を使用
class PoemModelAdmin(admin.ModelAdmin):
#
list_display = ['title', 'timestamp', 'author']
#
list_display_links = ['author']
# ,
search_fields = ['title']
# ( )
list_editable = ['title']
#
list_filter = ['author']
#
change_form_template = 'change_form.html'
class Meta:
model = Poem
admin.site.register(Poem, PoemModelAdmin)
カスタマイズ可能なテンプレート、ソースコード【option.py】を表示する
# Custom templates (designed to be over-ridden in subclasses)
add_form_template = None
change_form_template = None
change_list_template = None
delete_confirmation_template = None
delete_selected_confirmation_template = None
object_history_template = None