English 中文(简体)
Django admin. site. register 不添加我的应用程序管理员
原标题:Django admin.site.register doesn t add my app admin

我试图自动为我的应用程序模式创建行政管理管理模式。

在主 URLS.py 中,我有:

< 强度 > 编辑 :

from django.contrib import admin
admin.autodiscover()

在此之后:

urlpatterns = patterns(  ,
                       url(r ^appname/ ,include( appname.urls )),
                       url(r ^admin/ ,include(admin.site.urls)) 

在主 urls.py 中, 而不是在 App urspy 中, 注意此选项。

跟随教程( 在教程中为我工作过... ) 我创建了一个 admin. py 文件, 在 apname 文件夹中, 并在那里 :

from appname.models import Appname
from django.contrib import admin


class appnameAdmin(admin.ModelAdmin):
        fieldsets = [various field sets and fields etc ]

admin.site.register(Appname,AppnameAdmin)

................ 我............................ 我...................

 django.contrib.admin 

命令窗口没有出错, 基本管理员屏幕也出现( 简和网站) 。

我检查了管理器.py shell的进口品, 一切似乎都正常, 我还尝试将AppnameAdmin 课程评为“AppnameAdmin”课,

admin.site.register(Appname) 

但却不起作用,

我猜我错过了一件显而易见的事情 我会很乐意帮忙的

使用对角1.4+python 2.72

问题回答

检查"https://docs.djangoproject.com/en/dev/ref/contrib/admin/" rel="noreferr">这些:

启动Django行政网站有七个步骤:

  1. Add django.contrib.admin to your INSTALLED_APPS setting.
  2. The admin has four dependencies - django.contrib.auth, django.contrib.contenttypes, django.contrib.messages and django.contrib.sessions. If these applications are not in your INSTALLED_APPS list, add them.
  3. Add django.contrib.messages.context_processors.messages to TEMPLATE_CONTEXT_PROCESSORS and MessageMiddleware to MIDDLEWARE_CLASSES. (These are both active by default, so you only need to do this if you’ve manually tweaked the settings.)
  4. Determine which of your application’s models should be editable in the admin interface.
  5. For each of those models, optionally create a ModelAdmin class that encapsulates the customized admin functionality and options for that particular model.
  6. Instantiate an AdminSite and tell it about each of your models and ModelAdmin classes.
  7. Hook the AdminSite instance into your URLconf.
  • Do you have all the other admin dependencies in your installed apps?
  • Do you have admin.autodiscover() in your URLS.py?

我认为你的代码应该更像这个

from projectname.appname.models import Appname
from django.contrib import admin


class AppnameAdmin(admin.ModelAdmin):
        fieldsets = [various field sets and fields etc ]

admin.site.register(Appname,AppnameAdmin)

<% 1> 您重新启动服务器进程了吗?

也许这对某人有帮助:在我的案例中,问题是通过停止和启动服务器程序来解决的,因为当你添加一个新的 admin.py 文件时,它不会自动重新装入。

aaargghh - 我发现问题了。 我保存了在模板/ apname/ 文件夹中的管理员. py, 而不是在 apname/ 文件夹中。 我太蠢了。 很抱歉打断了 。

在您的型号管理员中设置此选项 :

def has_add_permission(self, request, obj=None):
    return True
def has_change_permission(self, request, obj=None):
    return True
def has_delete_permission(self, request, obj=None):
    return True

我假设您在应用程序中已经适当添加了管理. py 和 mods. py 的代码。 请确定您的应用程序已被添加到设置中 。 py :

INSTALLED_APPS = [
    . . .
     your_app_name.apps.Your_app_nameConfig 
]

在您将应用程序添加到设置. py 后, 在终端中运行以下命令 :

python manage.py makemigrations
python manage.py migrate

重新启动您的应用程序, 看看它现在是否有效!

检查所有这些 :

  1. < 加强> 重新启动服务器 并再次检查

  2. 添加 < 坚固 > 模型。 模式 作为您在模型文件中的类中的一个参数。 py 文件


    class Classname(models.Model):

  1. Add your app in the INSTALLED_APPS in settings.py, In my case it s travello.apps.TravelloConfig
    INSTALLED_APPS = [
         travello.apps.TravelloConfig ,
         django.contrib.admin ,
         django.contrib.auth ,
         django.contrib.contenttypes ,
         django.contrib.sessions ,
         django.contrib.messages ,
         django.contrib.staticfiles ,
    ]
  1. Add admin.autodiscover() in main urls.py

    from django.contrib import admin
    from django.urls import path, include
    admin.autodiscover()

    urlpatterns = [
        path(  , include( travello.urls )),
        path( admin/ , admin.site.urls),
    ]

它为我工作!

上面没有任何东西能为我工作

Then I went to Settings, and under Project:MyApp Project Interpreter I switched the Project Interpreter from Python 3.8(venv) to Python 3.8(MyApp)

然后是我所有注册的模型(可以在http://localhost:800/admin/ 中列出这些模型)

奇怪的是,仍然在 admin.py 中,在“admin.site.site”之后。注册的方法将不会被列出为可用 。 但不管怎样它还是有效的 。

转到 model.py 文件并添加 model。 在您的类中, 模式作为参数 。

示例:

class className(models.Model)

在您的情况中, 使用下面的类名, 它会100%地为您服务 。

class AppnameAdmin(models.Model):
from django.contrib import admin
from .models import modelname
admin.site.register(modelname)

以这种方式导入模型

Please consider that some models can be seen only from a superuser. Try to create one and log-in the admin with that user.

python3 manage.py createsuperuser




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签