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

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

更多相关方面的操作请查看文章:使用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/
注册之前的后台:

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

多出了之前我们在models里创建的表。我们可以在后台里面对这些表进行增、删、改方面的操作。
提示:如果复制上面的代码运行提示出错,请检查models.py里代码的格式,有个地方直接复制代码可能导致格式不对,请自行检查一下。留言里出现的问题大多是这个问题。
登录后可保存阅读位置、标记章节完成,并在其他设备继续学习。
登录并保存学习进度 →联系站长
微信:VIPdjango。技术问题建议先在 社区讨论 中交流,方便更多人参与和查阅。
章节讨论 32
出现这个 refers to 'tui', which is not a callable, an attribute of 'ArticleAdmin', or an attribute or method on 'blog.Article',首先检查models.py中的Article类下的粘贴格式是否有问题,有的粘贴位置都没有对齐。第二就是检查引用的名称是否有问题
有没有人和我一样 复制上面的代码到 blog/admin.py 页面下注册,会出现报错 打不开网站,找不到什么原因,我的python是 3.8 Django是最新版的 pycharm也是最新版的2020.2
报错如下: class ArticleAdmin(admin.ModelAdmin): TypeError: 'NoneType' object is not callable D:\djangoProject1\blog\admin.py changed, reloading. INFO:django.utils.autoreload:D:\djangoProject1\blog\admin.py changed, reloading. Watching for file changes with StatReloader INFO:django.utils.autoreload:Watching for file changes with StatReloader Performing system checks...
Exception in thread django-main-thread: Traceback (most recent call last): File "D:\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner self.run() File "D:\Python\Python38-32\lib\threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "D:\Python\Python38-32\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper fn(*args, **kwargs) File "D:\Python\Python38-32\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run self.check(display_num_errors=True) File "D:\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 442, 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 attribut e 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 attribu te 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 attrib ute 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).
后面我发现是 @admin.register 这个改为 @admin.site.register 后 就报错如下
File "", line 1014, in _gcd_import File "", line 991, in _find_and_load File "", line 975, in _find_and_load_unlocked File "", line 671, in _load_unlocked File "", line 783, in exec_module File "", line 219, in _call_with_frames_removed File "D:\djangoProject1\blog\admin.py", line 8, in class ArticleAdmin(admin.ModelAdmin): TypeError: 'NoneType' object is not callable
有谁解决了吗? 请教下
按照大佬的源代码测试,“文章”这个链接点击去后报错。 (1054, "Unknown column 'blob_article.user_id' in 'field list'") ???什么情况?
<class 'blog.admin.LinkAdmin'>: (admin.E108) The value of 'list_display[2]' refers to 'linkurl', which is not a callable, an attribute of 'LinkAdmin', or an attribute or method on 'blog.Link'.