官术网_书友最值得收藏!

  • Django 2 by Example
  • Antonio Melé
  • 195字
  • 2021-06-25 21:19:00

Adding your models to the administration site

Let's add your blog models to the administration site. Edit the admin.py file of your blog application and make it look like this:

from django.contrib import admin
from .models import Post

admin.site.register(Post)

Now, reload the admin site in your browser. You should see your Post model on the admin site, as follows:

That was easy, right? When you register a model in the Django admin site, you get a user-friendly interface generated by introspecting your models that allows you to list, edit, create, and delete objects in a simple way.

Click on the Add link beside Posts to add a new post. You will note the create form that Django has generated dynamically for your model, as shown in the following screenshot:

Django uses different form widgets for each type of field. Even complex fields, such as DateTimeField, are displayed with an easy interface, such as a JavaScript date picker.

Fill in the form and click on the SAVE button. You should be redirected to the post list page with a successful message and the post you just created, as shown in the following screenshot:

主站蜘蛛池模板: 泉州市| 贵南县| 新邵县| 华安县| 耒阳市| 昌宁县| 顺义区| 永年县| 逊克县| 潜山县| 会同县| 齐河县| 龙岩市| 垣曲县| 磴口县| 富宁县| 昌宁县| 稻城县| 沂水县| 廊坊市| 雅安市| 邓州市| 仙居县| 肥乡县| 南宁市| 疏附县| 漳州市| 开阳县| 辽阳县| 彰化市| 沅江市| 沛县| 灵山县| 邻水| 务川| 屏南县| 罗田县| 都匀市| 凤翔县| 宁晋县| 麻阳|