- 创建forms.py
在BASE_DIR/appname下新建forms.py
from django import forms
from .models import AppName
class AppNameForm(forms.ModelForm):
class Meta:
model = AppName
fields = ["attri1","attri2","attri3"]
可以更改admin中添加属性的次序和有无(依据fields)
- 修改admin.py
from django.contrib import admin# Register your models here.
from .models import APPName
from .forms import AppNameForm #modified
class AppNameAdmin(admin.ModelAdmin):
list_display = ["attri1","attri2","attri3"]
form = AppNameForm #modified
#class meta:
#model = AppName
admin.site.register(AppName, AppNameAdmin)
可以更改记录显示的次序(依据list_display)
- 修改views.py以及home.html
from django.shortcuts import render
from .forms import AppNameForm
# Create your views here.
def home(request):
title = "test_title"
context = {
"title" : title,
"form" : AppNameForm,
}
return render(request,"home.html",context)
<h1>{{ title }}</h1>
<form method="POST" action="">
{{ form.as_p }}<!-- as_p的作用是让他按行显示,而不是显示在一行 -->
<input type="submit" value="submit">
</form>