模型未显示在 django admin 中
为了自己的乐趣,我已经创建了几个 django 应用程序和东西,到目前为止一切都工作正常。
现在我刚刚创建了新项目(django 1.2.1),从一开始就遇到了麻烦。
我创建了新的应用程序 - 游戏和新模型游戏。我创建了admin.py并将相关内容放入其中。跑syncdb并去检查管理员。模型没有出现在那里。
我继续检查并仔细检查并阅读以前的类似线程: 注册模型不会显示在管理中 Django 应用程序未显示在管理界面中
我看得出来,他们也帮不了我。也许其他人可以为我指出这一点。
游戏应用程序中的 models.py:
# -*- coding: utf-8 -*-
from django.db import models
class Game(models.Model):
type = models.IntegerField(blank=False, null=False, default=1)
teamone = models.CharField(max_length=100, blank=False, null=False)
teamtwo = models.CharField(max_length=100, blank=False, null=False)
gametime = models.DateTimeField(blank=False, null=False)
游戏应用程序中的 admin.py:
# -*- coding: utf-8 -*-
from jalka.game.models import Game
from django.contrib import admin
class GameAdmin(admin.ModelAdmin):
list_display = ['type', 'teamone', 'teamtwo', 'gametime']
admin.site.register(Game, GameAdmin)
项目 settings.py:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
ROOT_URLCONF = 'jalka.urls'
TEMPLATE_DIRS = (
"/home/projects/jalka/templates/"
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'game',
)
urls.py:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^jalka/', include('jalka.foo.urls')),
(r'^admin/', include(admin.site.urls)),
)
I have ceated several django apps and stuffs for my own fun and so far everything has been working fine.
Now I just created new project (django 1.2.1) and have run into trouble from 1st moments.
I created new app - game and new model Game. I created admin.py and put related stuff into it. Ran syncdb and went to check into admin. Model did not show up there.
I proceeded to check and doublecheck and read through previous similar threads:
Registered models do not show up in admin
Django App Not Showing up in Admin Interface
But as far as I can tell, they don't help me either. Perhaps someone else can point this out for me.
models.py in game app:
# -*- coding: utf-8 -*-
from django.db import models
class Game(models.Model):
type = models.IntegerField(blank=False, null=False, default=1)
teamone = models.CharField(max_length=100, blank=False, null=False)
teamtwo = models.CharField(max_length=100, blank=False, null=False)
gametime = models.DateTimeField(blank=False, null=False)
admin.py in game app:
# -*- coding: utf-8 -*-
from jalka.game.models import Game
from django.contrib import admin
class GameAdmin(admin.ModelAdmin):
list_display = ['type', 'teamone', 'teamtwo', 'gametime']
admin.site.register(Game, GameAdmin)
project settings.py:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
ROOT_URLCONF = 'jalka.urls'
TEMPLATE_DIRS = (
"/home/projects/jalka/templates/"
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'game',
)
urls.py:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^jalka/', include('jalka.foo.urls')),
(r'^admin/', include(admin.site.urls)),
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
报告的问题可能是因为您跳过了为管理站点注册模型。这可以通过在您的应用程序下创建一个
admin.py
文件来完成,然后使用以下命令注册模型:The problem reported can be because you skipped registering the models for the admin site. This can be done, creating an
admin.py
file under your app, and there registering the models with:嗯...尝试在 settings.py 中更改您的应用程序的包含内容:
发件人:
收件人:
Hmmmm...Try change include of your app in settings.py:
From:
To:
Django 2.0 也有同样的问题。
以下代码不起作用:
事实证明该行
应该是
因为我没有收到任何警告,只是想在这里指出这一点。
Had the same issue with Django 2.0.
The following code didn't work:
It turns out the line
should have been
Since I didn't get any warnings, just wanted to point out this thing here too.
我知道这已经得到回答和接受,但我想分享我对这个问题的解决方案,也许它会帮助其他人。
我的
INSTALLED_APPS
看起来像这样:看,我把我的应用程序放在 Django 的管理应用程序之前,显然它按这个顺序加载它们。我只是将我的应用程序移至管理员下方,它就开始显示:)
I know this has already been answered and accepted, but I felt like sharing what my solution to this problem was, maybe it will help someone else.
My
INSTALLED_APPS
looked like this:See, I put my app before Django's admin app, and apparently it loads them in that order. I simply moved my app right below the admin, and it started showing up :)
我有同样的问题。一切都做对了。但我没有查看模型的权限。
只需授予所需的权限(查看/添加/编辑/等),然后再次检查。
使用管理面板授予权限:
选择并添加所需的权限
参考此通过管理面板添加/更改权限
替代方案:
使用代码
参考 Django 官方文档
I had the same issue. Done everything right. But I didn't have permission to view the model.
Just give the required permission (view/add/edit/etc) and then check again.
Give Permission using admin panel:
Select and add the required permission
refer this to add/change permission through the admin panel
Alternative:
Using code
refer Official Django Doc
对于 Django 1.10 帮助我按照最后使用 (admin.ModelAdmin) 的方式注册模型
For Django 1.10 helped to me to register the Model following way with (admin.ModelAdmin) at the end
这可能非常罕见,但我今天遇到了一个问题,我创建的 admin.py 文件的权限已损坏,因此 django 无法读取它。我删除了该文件并重新创建它,成功了。
希望这可以拯救某人,如果他们遇到我的问题。
It's probably very rare but I had an issue today where the permissions on the admin.py file I had created were corrupt and thus made it unreadable by django. I deleted the file and recreated it with success.
Hope that saves someone, should they stumble here with my problem.
我也很难注册我的模型(尝试了上面的所有建议),直到我做了一件非常简单的事情。我将 admin.py 从目录移至项目目录 - 刷新管理屏幕并将其移回,瞧,进入模型应用程序目录 - 它们都立即出现。我正在使用 PyCharm 所以不确定这是否导致了一些问题。
我的设置正是 Django 手册所说的 -
models.py
admin.py
I struggled with registering my models (tried all the suggestions above) too until I did a very simple thing. I moved my admin.py out of the directory up to the project directory - refreshed the admin screen and moved it back and voila into the models app directory - they all appeared instantly. I'm using PyCharm so not sure if that was causing some problems.
My setup is exactly what the Django manual says -
models.py
admin.py
我正在使用数字海洋,也遇到了这个问题。解决方案是重新启动服务。我使用了
服务gunicorn restart
,模型就显示出来了
I am using digital ocean and also ran into this issue. The solution was a service restart. I used
service gunicorn restart
and that got the model to show up
我只需要重新启动 apache 服务即可。服务 apache2 重新启动。然后新的模型出现了。
I just had to restart apache service. service apache2 restart. Then new models shown up.
我有同样的问题。我的几个模型没有显示在 django admin dashbaord 中,因为我没有在 admin.py 文件中注册这些模型。
这为我解决了这个问题。只需注册并刷新您的网页,模型就会在管理仪表板中可见
I had the same issue. Few of my models were not showing in the django admin dashbaord because I had not registered those models in my admin.py file.
This fixed it for me. Just register and refresh your webpage and the models will be visible in the admin dashboard
我有同样的问题。我解决了这个问题,将 admin 寄存器添加到
admin.py
中。而且我不需要添加额外的课程。
喜欢:
环境:Win10、Django 1.8
django
I have the same problem. I solve this to add register of admin to
admin.py
.And I don't need to add extra class.
Like:
Enviroment: Win10、Django 1.8
django
我有同样的问题,我解决如下
首先转到您的 app.py 文件并复制类名。
现在转到setting.py并将其放置在应用程序定义中,如下所示:
在终端中保存并运行命令
运行服务器并转到/admin
你的问题将会得到解决
I had the same issue and i resolved it as follow
first go to your app.py file and copy the class name.
now go to setting.py and place it in application definition as follow:
save and run the commands in the terminal
run your server and go to /admin
your problem will be solved
你只需要注册你的模型。
到同一应用程序的 admin.py 文件并写入,
you just need to register your model.
got to admin.py file of the same app and write,
我还想补充一点,我做了这些答案所说的一切,除了我没有在命令行上输入
sudo service apache2 restart
以使更改生效。 (因为我正在亚马逊网络服务的端口 80 上的实时服务器上进行测试。我还使用 Ubuntu 操作系统。)这为我解决了这个问题。我希望这可以帮助某人。I would also like to add, that I did everything these answers said, except I did not enter on the command line
sudo service apache2 restart
which I needed to make the changes take effect. (Because I was testing on a live server on port 80 on amazon web services. I was also using an Ubuntu operating system.) That solved it for me. I hope this might help somebody.我遇到了同样的问题,
重新启动后,我的开发服务器模型显示在管理页面中。
I got the same issue,
After restart my development server model is showing in admin page.
将您的管理代码更改为:
还要确保该应用程序包含在 settings.py 文件中,并确保为您的模型导入正确的名称。导致此问题的另一个原因是在模型字段末尾放置一个逗号 (,) ,就好像它是一个 Python 列表一样。
Change your admin code to this:
Also make sure the app is included in settings.py file and make sure you're importing the right name for your model. Another reason that can bring about this issue is putting a comma (,) at the end of your model fields as though it a python list.
由于我使用 ADMIN_REORDER(https://pypi. org/project/django-modeladmin-reorder/)在settings.py中控制模型顺序,不久前添加并忘记了。将新模型添加到settings.py并显示出来。
My new model not showing up on Admin page due to me using ADMIN_REORDER(https://pypi.org/project/django-modeladmin-reorder/) in settings.py to control model order, added a while back and forgotten. Added the new model to settings.py and it shows up.
添加到 Saff 所说的内容中,您的
settings.py
应该如下所示:Adding to what Saff said, your
settings.py
should be like this:错误可能是在你的“def”中的views.py,你应该检查你是否得到了
我会给你一小部分我的代码,这样你就会明白
“UserAsking是一种形式”。
我希望我确实帮助了你
The mistake might be at
views.py
in your "def" you should check if you gotI will give you a piee of my code so you would understand it
`UserAsking is a form.
i hope i did help you
可能的原因之一是您没有关闭旧的 python 进程。
One of the possible reasons is that you do not close old python processes.
我在安装在 docker 容器中的 django 实例上遇到了类似的问题。代码是正确的,但对 admin.py 的更改没有出现在 django admin 中。
退出 django 进程并使用 docker-compose up 重新挂载容器后,添加的模型在 django admin 中可用。
I had a similar issue on an instance of django mounted in a docker container. The code was correct but changes to admin.py were not appearing in django admin.
After quitting the django process and remounting the container with
docker-compose up
, the added models became available in django admin.我将所有管理类放在一个文件夹
./sites/
中,该文件夹仅因我的循环依赖而起作用。我修好了它们,其他一切都停止工作了。当我将该目录从
./sites/
重命名为./admin/
时,它起作用了。I had all my Admin classes in a folder
./sites/
which only worked because of cercular dependencies I had. I fixed them, and everything else stopped working.When I renamed that directory from
./sites/
to./admin/
it worked.另一件需要指出的事情是,您应该确保您是使用超级用户登录的。
可能是您登录的用户对查看容量的权限有限,因此请尝试这样做。
我在 Django 2.0 中仍然仅使用应用程序的名称(即“游戏”)来注册我的应用程序。但问题出在登录用户权限上。
Another thing to point out is that you should make sure you're signed in with a superuser.
It could be that you are signed in with a user who has limited permission on view capacity so try that.
I in Django 2.0 I still register my app using just the name of the app i.e 'game'. But the issue was in the logged in user permissions.
我正在使用本地代码更新远程服务器,但远程服务器仍在管理屏幕中使用旧模型。我的本地服务器和远程服务器之间的一切看起来都完全相同 - 数据库表匹配并且代码也相同。
问题原来是远程服务器正在使用 venv 环境运行。运行
source venv/bin/activate
并进行迁移后,管理员显示了所有新模型。I was updating a remote server with code from local, but the remote server was still using the old models in the admin screen. Everything looked exactly the same between my local and the remote server - the database tables matched and the code was also identical.
The problem turned out the remote server was running using the venv environment. After running
source venv/bin/activate
and then doing the migrations, the admin showed all the new models.如果您有多个模型要注册,请使用。
admin.site.register((Model1 , Model2 , Model3))
或
admin.site.register([Model1 , Model2 , Model3])
。admin.py 文件中的
If you have multiple models to register then use.
admin.site.register((Model1 , Model2 , Model3))
or
admin.site.register([Model1 , Model2 , Model3])
in your admin.py file.
我有同样的错误,但我没有在 INSALLED_APPS 中提及我的应用程序
我已经注册了该模型,但忘记在 INSTALLED_APPS 中提及我的应用程序,在提及它对我有用后
I have the same error but I have not mentioned my app in INSALLED_APPS
I have already registered the model but forgot to mention my app in INSTALLED_APPS and after mentioning it works for me
对我来说,移民是一个问题。如果您创建了这个新应用,请尝试执行以下操作:
您不需要将所有
admin.site.register
包含在单个应用中,您可以在admin 的相应应用中指定它们.py 文件。
For me, migration was an issue. If you created this new app, try doing the following:
You do not need to include all
admin.site.register
in a single app, you could specify them in their corresponding app at theadmin.py
file.我遇到了同样的问题,
django=4.0
和docker-compose
我没有使用ModelAdmin
,结果在一次调用中注册了多个模型 <运行迁移时 code>admin.site.register 出错。所以这样做
并没有成功。相反,你做了类似的事情,
我希望它可以帮助任何人 dockerizing 他们的 Django 应用程序
I had this same issue,
django=4.0
withdocker-compose
i was not usingModelAdmin
, turns out registering multiple Models in one call toadmin.site.register
errors out when running migration.So doing
did not work. instead, you I did something like
I hope it helps anyone dockerizing their Django App
通过管理面板中的编辑用户功能为您的用户授予“超级用户身份”。
您登录的用户很可能不是超级用户,并且不具备新创建模型所需的权限。
在Django中,创建并迁移新模型后,现有用户的权限不会更新。默认情况下,只有具有超级用户状态的用户才能查看、添加、更改和删除这些模型的记录。
为了解决该问题并避免将来再次出现该问题,请为您的开发用户提供“超级用户身份”。
Give your user "Superuser status" through the edit user functionality in the admin panel.
Most probably the user you are signed in is not a superuser and doesn't have the required permissions for the newly created models.
In Django, after new models are created and migrated, the permissions of the existing users are not updated. Only the users with Superuser status can by default view, add, change and delete records for these models.
To fix the problem and avoid it in the future give "Superuser status" to your development user.