3(form home/admin_form)

  1. 创建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)

  1. 修改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)

  1. 修改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)

{{ title }}

{{ form.as_p }}

你可能感兴趣的:(3(form home/admin_form))