简体   繁体   English

模型未显示在 django admin 上

[英]models not showing on django admin

I'm just starting my first project using Django 1.11.我刚刚使用 Django 1.11 开始我的第一个项目。 I followed the same steps I've used on multiple Django 1.10 projects, but for some reason my models are not showing up on my localhost/admin site.我遵循了在多个 Django 1.10 项目中使用的相同步骤,但由于某种原因,我的模型没有出现在我的 localhost/admin 站点上。

My INSTALLED_APPS from settings.py:我的 INSTALLED_APPS 来自 settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home.apps.HomeConfig',
]

My admin.py:我的管理员.py:

from django.contrib import admin

from home.models import Home

# begin Admin Class Definitions
class HomeAdmin(models.ModelAdmin):

    fieldsets = [
        ('Title', {'fields': ['title']}),
        ('Publication Date', {'fields': ['pub_date']}),
        ('Home Page Text', {'fields': ['header', 'sub_header', 
         'link_text']}),
    ]
    list_display = ('title', 'pub_date')
    list_filter = ['pub_date']

admin.site.register(Home, HomeAdmin)

My [main_app]/urls.py:我的 [main_app]/urls.py:

from django.conf.urls import url, include
from django.contrib import admin

admin.autodiscover()

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('home.urls'))
]

But when I go to localhost:8000/admin the only things present are Groups and Users as if I hadn't registered any models at all.但是当我访问localhost:8000/admin时,唯一存在的就是组和用户,就好像我根本没有注册任何模型一样。

I have run makemigrations and migrate, and I have tried putting admin.py within the app directory instead of the project directory.我已经运行了 makemigrations 和 migrate,我尝试将 admin.py 放在 app 目录而不是项目目录中。 It's currently in the [project_name]/[project_name] directory (the one with settings.py, urls.py, and wsgi.py files).它当前位于 [project_name]/[project_name] 目录(包含 settings.py、urls.py 和 wsgi.py 文件的目录)。

Any suggestions?有什么建议么?

I think you have to register your models to the admin.我认为您必须将模型注册到管理员。 I normally do it like this if it's a simple model.如果它是一个简单的模型,我通常会这样做。

    from django.contrib import admin
    from .models import *

    # Register your models here.
    @admin.register(Model)
    class ModelAdmin(admin.ModelAdmin): pass

I gave up trying to diagnose my problem, and nuked the project and started over.我放弃了尝试诊断我的问题,并取消了该项目并重新开始。 It works fine now.它现在工作正常。 I'm honestly not sure what the problem was.老实说,我不确定问题出在哪里。 If anyone is curious, here are my new files:如果有人好奇,这是我的新文件:

from settings.py:来自settings.py:

INSTALLED_APPS = [
    'home',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

from urls.py:来自 urls.py:

from django.conf.urls import url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

from admin.py:来自 admin.py:

from django.contrib import admin

# Register your models here.
from .models import HomePageText

admin.site.register(HomePageText)

I honestly don't understand why it wasn't working before.老实说,我不明白为什么它以前不起作用。 If anyone with more insight can spot some difference that I can't please let me know.如果有更多洞察力的人可以发现一些我无法做到的差异,请告诉我。

When I use the Django admin always import in this form:当我使用 Django 管理员时,总是以这种形式导入:

from django.contrib import admin

And inherint in the class:并在类中继承:

class HomeAdmin(admin.ModelAdmin):

So your admin.py file would looke like this:所以你的 admin.py 文件看起来像这样:

from django.contrib import admin

from home.models import Home

# begin Admin Class Definitions
class HomeAdmin(admin.ModelAdmin):

    fieldsets = [
        ('Title', {'fields': ['title']}),
        ('Publication Date', {'fields': ['pub_date']}),
        ('Home Page Text', {'fields': ['header', 'sub_header', 
         'link_text']}),
    ]
    list_display = ('title', 'pub_date')
    list_filter = ['pub_date']

admin.site.register(Home, HomeAdmin)

Stupid and obvious suggestion, but Django admin changes don't always appear to be picked up immediately.愚蠢而明显的建议,但 Django 管理员更改似乎并不总是立即被采纳。 If everything appears to be configured correctly (models and admins registered, apps installed etc), try a server restart - after all the suggestions above, ultimately, this did it for me.如果一切似乎都配置正确(模型和管理员注册,应用程序安装等),请尝试重新启动服务器 - 在上述所有建议之后,最终,这对我来说是成功的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM