Django博客开发教程封面
Django博客开发教程 · 第 8 / 20 节

用Admin管理后台管理数据

吴秀峰 55020 次阅读 32 条评论 ↓

上节我们我们把数据库迁移到数据库里去了,那么现在我们数据库里是个什么样的情况呢?我们点击Pycharm右上角的Database,然后在网站项目里选中我们的数据库文件db.sqlite3,把它拖到Database框里。

15.jpg

然后点击db,就可以查看到我们的网站数据库,我们可以对数据进行增、删、改、查操作。

16.jpg

更多相关方面的操作请查看文章:使用Pycharm里的Database对数据库进行可视化操作

Pycharm Batabase限制非常大,下面我们介绍如何使用Django自带的admin管理网站数据。django的admin后台管理它可以让我们快速便捷管理数据,我们可以在各个app目录下的admin.py文件中对其进行控制。想要对APP应用进行管理,最基本的前提是要先在settings里对其进行注册,就是在INSTALLED_APPS里把APP名添加进去,我们在前面的文章基础配置有提到过。

注册APP应用之后,我们想要在admin后台里对数据库表进行操作,我们还得在应用APP下的admin.py文件里对数据库表先进行注册。我们的APP应用是blog,所以我们需要在blog/admin.py文件里进行注册:

blog/admin.py

from django.contrib import admin
from .models import Banner, Category, Tag, Tui, Article, Link 
#导入需要管理的数据库表

@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    list_display = ('id', 'category', 'title', 'tui', 'user', 'views', 'created_time')
    # 文章列表里显示想要显示的字段
    list_per_page = 50
    # 满50条数据就自动分页
    ordering = ('-created_time',)
    #后台数据列表排序方式
    list_display_links = ('id', 'title')
    # 设置哪些字段可以点击进入编辑界面



@admin.register(Banner)
class BannerAdmin(admin.ModelAdmin):
    list_display = ('id', 'text_info', 'img', 'link_url', 'is_active')

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'index')

@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
    list_display = ('id', 'name')

@admin.register(Tui)
class TuiAdmin(admin.ModelAdmin):
    list_display = ('id', 'name')

@admin.register(Link)
class LinkAdmin(admin.ModelAdmin):
    list_display = ('id', 'name','linkurl')

关于admin定制和数据库表注册管理方法,在文章定制Admin管理后台有详细介绍。

登录管理后台http://127.0.0.1:8000/admin/

注册之前的后台:

17.jpg

注册之后,启动项目,刷新页面:

18.jpg

多出了之前我们在models里创建的表。我们可以在后台里面对这些表进行增、删、改方面的操作。


提示:如果复制上面的代码运行提示出错,请检查models.py里代码的格式,有个地方直接复制代码可能导致格式不对,请自行检查一下。留言里出现的问题大多是这个问题。

把学习连起来

登录后可保存阅读位置、标记章节完成,并在其他设备继续学习。

登录并保存学习进度 →
联系站长

微信:VIPdjango。技术问题建议先在 社区讨论 中交流,方便更多人参与和查阅。

章节讨论 32

  • 社区成员1843

    <class 'App.admin.ArticleAdmin'>: (admin.E033) The value of 'ordering[0]' refers to 'created_time', which is not an attribute of 'App.Article'. <class 'App.admin.ArticleAdmin'>: (admin.E108) The value of 'list_display[3]' refers to 'tui', which is not a callable, an attribute of 'ArticleAdmin', or an attribute or method on 'App.Article'. <class 'App.admin.ArticleAdmin'>: (admin.E108) The value of 'list_display[4]' refers to 'user', which is not a callable, an attribute of 'ArticleAdmin', or an attribute or method on 'App.Article'. <class 'App.admin.ArticleAdmin'>: (admin.E108) The value of 'list_display[5]' refers to 'views', which is not a callable, an attribute of 'ArticleAdmin', or an attribute or method on 'App.Article'. <class 'App.admin.ArticleAdmin'>: (admin.E108) The value of 'list_display[6]' refers to 'created_time', which is not a callable, an attribute of 'ArticleAdmin', or an attribute or method on 'App.Article'.

  • 社区成员1966

    配好admin.py 后打开网站 点击文章报错(其他都行) 1054, "Unknown column 'blog_article.user_id' in 'field list'" 请问这个怎么解决,是外键哪儿出问题了吗

  • 社区成员1984

    我将db.sqlite3拉到database里面,出现了一个db,但是点开没有内容,什么都没有,这是为什么啊?

  • 社区成员2256

    Performing system checks...

    Unhandled exception in thread started by <function check_errors..wrapper at 0x107fab7a0> Traceback (most recent call last): File "/Users/pinfguo/python/pyweb/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper fn(*args, **kwargs) File "/Users/pinfguo/python/pyweb/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run self.check(display_num_errors=True) File "/Users/pinfguo/python/pyweb/lib/python3.7/site-packages/django/core/management/base.py", line 425, in check raise SystemCheckError(msg) django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

    ERRORS: <class 'blog.admin.ArticleAdmin'>: (admin.E033) The value of 'ordering[0]' refers to 'created_time', which is not an attribute of 'blog.Article'. <class 'blog.admin.ArticleAdmin'>: (admin.E108) The value of 'list_display[3]' refers to 'tui', which is not a callable, an attribute of 'ArticleAdmin', or an attribute or method on 'blog.Article'. <class 'blog.admin.ArticleAdmin'>: (admin.E108) The value of 'list_display[4]' refers to 'user', which is not a callable, an attribute of 'ArticleAdmin', or an attribute or method on 'blog.Article'. <class 'blog.admin.ArticleAdmin'>: (admin.E108) The value of 'list_display[5]' refers to 'views', which is not a callable, an attribute of 'ArticleAdmin', or an attribute or method on 'blog.Article'. <class 'blog.admin.ArticleAdmin'>: (admin.E108) The value of 'list_display[6]' refers to 'created_time', which is not a callable, an attribute of 'ArticleAdmin', or an attribute or method on 'blog.Article'.

    System check identified 5 issues (0 silenced).

  • 社区成员2502

    请问大佬,根据教程在blog/admin.py文件里进行注册后, 提示如下报错: 请问是什么原因,不胜感激! django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues: ERRORS: <class 'blog.admin.ArticleAdmin'>: (admin.E033) The value of 'ordering[0]' refers to 'created_time', which is not an attribute of 'blog.Article'. <class 'blog.admin.ArticleAdmin'>: (admin.E108) The value of 'list_display[3]' refers to 'tui', which is not a callable, an attribute of 'ArticleAdmin', or an attribute or method on 'blog.Article'. <class 'blog.admin.ArticleAdmin'>: (admin.E108) The value of 'list_display[4]' refers to 'user', which is not a callable, an attribute of 'ArticleAdmin', or an attribute or method on 'blog.Article'. <class 'blog.admin.ArticleAdmin'>: (admin.E108) The value of 'list_display[5]' refers to 'views', which is not a callable, an attribute of 'ArticleAdmin', or an attribute or method on 'blog.Article'. <class 'blog.admin.ArticleAdmin'>: (admin.E108) The value of 'list_display[6]' refers to 'created_time', which is not a callable, an attribute of 'ArticleAdmin', or an attribute or method on 'blog.Article'.

  • 社区成员2534

    教程非常好,看着易懂。只是 banner后台管理上传图片时,图片链接地址 要手工输入,该怎么填?如何自动取得图片地址?

  • 橘子果汁
    橘子果汁

    有一个问题是,上传图片了,而且在media文件夹下面的了。但是打开的时候无法查看。