Django博客开发教程:用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里代码的格式,有个地方直接复制代码可能导致格式不对,请自行检查一下。留言里出现的问题大多是这个问题。
文章评论 32
良心教程,受用
> 我是用是mysql,添加可视化管理的时候报错
你的models.py中,created_time,tui等几个字段创建的那段代码缩进错了
老哥稳 百度了一天也没找出来原因 怎么也没想到c+v代码还能把缩减v没了
为什么我登陆管理后台http://127.0.0.1:8000/admin/显示无法访问此网站
问题暂时性已解决,pycharm需要运行项目,有更好的办法推荐吗
请问怎么解决的,我也遇到了
终端 重新运行:python manage.py runserver
服务没起来,执行 python manage.py runserver
<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'.
我也是卡到这个错误了,老哥你解决了吗?
应该有拼写错误。教程里models.py是create_time这里是created_time两者改其一应该就行了。
<class“App.admin.ArticleAdmin”>:(admin.E033)“ordering[0]”的值指的是“created_time”,它不是“App.Article”的属性。<class“App.admin.AArticleAdmin”>:(admin.E108)“list_display[3]”的值是指“tui”,它是不可调用的,是“ArticleAdmin”的属性,或“App.Article”上的属性或方法。<class“App.admin.ArticleAdmin”>:(admin.E108)“list_display[4]”的值指的是“user”,它不是可调用的,是“ArticleAdmin”的属性或“App.Article”的属性。<class”App.admin.ArticleAdmin“>:(admin.E108),或“App.Article”上的属性或方法。<class“App.admin.ArticleAdmin”>:(admin.E108)“list_display[6]”的值指的是“created_time”,它不是可调用的、“ArticleAdmin”的属性或“App.Article”的属性。
配好admin.py 后打开网站 点击文章报错(其他都行) 1054, "Unknown column 'blog_article.user_id' in 'field list'" 请问这个怎么解决,是外键哪儿出问题了吗
把数据库和migrations缓存删掉从新迁移就好了
我将db.sqlite3拉到database里面,出现了一个db,但是点开没有内容,什么都没有,这是为什么啊?
1、创建数据库的代码,鼠标右键复制后,在pycharm 邮件,选择无格式黏贴(解决楼上“models.py中,created_time,tui等几个字段创建的那段代码缩进错了”的这个问题 2、按照步骤 迁移数据库 3、在pycharm 右上角 Database 视图窗口,点击bd 的设置,进入界面,看看这里。可以测试一下数据库链接,有可能缺少链接的工具包,他会提示你下载,我就是这么解决的。
因为你的pycharm没有装sqlite的驱动,在data source中选择sqlite,在dirver中点开安装即可。
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.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).
正在执行系统检查。。。
<function check_errors启动的线程中出现未处理的异常<locals>.wapper at 0x107fab7a0>Traceback(最近一次调用):文件“/Users/pinfguo/python/pyweb/lib/python3.7/site packages/django/utils/autoreload.py”,第225行,在包装器fn(args,*kwargs)中,在inner_run self.check(display_num_errors=True)文件“/Users/pinfguo/python/pyweb/lib/python3.7/site packages/django/core/management/base.py”的第425行,在检查中引发SystemCheckError(msg)django.core.management.base.SystemCheckError:SystemCheckError:系统检查发现了一些问题:
错误:<class“blog.admin.ArticleAdmin”>:(admin.E033)“ordering[0]”的值指的是“created_time”,它不是“blog.Article”的属性。<class“blog.admin.AArticleAdmin”>:(admin.E108)“list_display[3]”的值是指“tui”,它是不可调用的,或“blog.Article”上的属性或方法。<class“blog.admin.ArticleAdmin”>:(admin.E108)“list_display[4]”的值指的是“user”,它不是可调用的,是“ArticleAdmin”的属性,或是“blog.Article”上的特性或方法。<class”blog.admin.ArticleAdmin“>:(admin.E108),或“blog.Article”上的属性或方法。<class“blog.admin.ArticleAdmin”>:(admin.E108)“list_display[6]”的值指的是“created_time”,它不是可调用的、“ArticleAdmin”的属性或“blog.Article”的属性。
系统检查发现了5个问题(0个静音)。 你按这个步骤去做
请问大佬,根据教程在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'.
复制粘贴的时候看看class里面代码缩进有没有问题
现在解决了没?我也遇到了
教程非常好,看着易懂。只是 banner后台管理上传图片时,图片链接地址 要手工输入,该怎么填?如何自动取得图片地址?
有一个问题是,上传图片了,而且在media文件夹下面的了。但是打开的时候无法查看。
出现这个 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 "<frozen importlib._bootstrap>", line 1014, in _gcd_import File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 671, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 783, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "D:\djangoProject1\blog\admin.py", line 8, in <module> class ArticleAdmin(admin.ModelAdmin): TypeError: 'NoneType' object is not callable
有谁解决了吗? 请教下
<class 'web.admin.ArticleAdmin'>: (admin.E033) The value of 'ordering[0]' refers to 'created_time', which is not an attribute of 'web.Article'. <class 'web.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 'web.Article'. <class 'web.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 'web.Article'. <class 'web.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 'web.Article'. <class 'web.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 'web.Article'.
相信我,你的models里面的Artical里面缩进有问题,多打一些空格就好了
都是复制的吧?没看格式。复制的时候格式乱了!
按照大佬的源代码测试,“文章”这个链接点击去后报错。 (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'.