English 中文(简体)
为什么Django行政界面没有出现?
原标题:Why the Django admin interface didn t appear?

我是一个新来的对阵队。

我试图根据一本关于Jjango的书 做一个样本

我在我的模范课程中加上了“阶级管理者”, 然而,在对角行政界面中, 我只能看到“用户”、“群体”和“特选人”, 我的模范课程都没有出现。

没有任何错误或警告信息,所以我不知道发生了什么,我下一步该怎么办。

有帮助吗?

from django.db import models

# Create your models here.
class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address=models.CharField(max_length=50)
    city=models.CharField(max_length=60)
    state_province=models.CharField(max_length=30)
    country=models.CharField(max_length=50)
    website=models.URLField()

    def __str__(self):
        return self.name

    class Admin:
        pass

class Author(models.Model):
    salutation=models.CharField(max_length=10)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email=models.EmailField()
    headshot=models.ImageField(upload_to= /tmp )

    def __str__(self):
        return  %s %s  % (self.first_name, self.last_name)

    class Admin:
        pass

class Book(models.Model):
    title=models.CharField(max_length=10)
    authors=models.ManyToManyField(Author)
    publisher=models.ForeignKey(Publisher)
    publication_date = models.DateField()

    def __str__(self):
        return self.title
最佳回答

您需要做三件事才能通过管理网站编辑您的模型 :

  1. 在您的应用程序目录中以代码创建文件管理员. py (参见 < a href="https://docs.djangoproject.com/en/dev/ref/contrib/admin/#modadmin-objects" rel="nofollow" >https://docs.djangoproject.com/en/dev/ref/contrib/admin/#modadmin-objects ) 创建文件管理员:

    from django.contrib import admin
    from your_app.models import Publisher, Author, Book
    
    admin.site.register(Author)
    admin.site.register(Publisher)
    admin.site.register(Book)
    
  2. 在您的 urls.py 中添加以下内容 (请看< a href="https://docs.djangoproject.com/en/dev/ref/contrib/admin/#hooking-adminsite-instations-into-your-urlconf" rel=“nofollow" >https://docs.djangoproject.com/en/dev/ref/contrib/admin/#hooking-adminsite-adminsite-intances-inst-into-your-urconf < /a > :

    from django.conf.urls import patterns, url, include
    from django.contrib import admin
    
    admin.autodiscover()
    
    urlpatterns = patterns(  ,
        (r ^admin/ , include(admin.site.urls)),
        # your urls goes here
    )
    
  3. 请确定您设置的. py 符合以下条件 (请看< a href="https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overview>https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overview ):

    TEMPLATE_CONTEXT_PROCESSORS = (
         django.contrib.messages.context_processors.messages ,
        #other context processors
    )
    
    MIDDLEWARE_CLASSES = (
         django.contrib.messages.middleware.MessageMiddleware ,
        # other middleware
    )
    
    INSTALLED_APPS = (
         django.contrib.admin ,
         django.contrib.auth ,
         django.contrib.contenttypes ,
         django.contrib.messages ,
         django.contrib.sessions ,
        # other apps
    )
    
问题回答

您应该创建管理员. py, 并遵循以下代码

from xxxxx.models import Publisher
from django.contrib import admin

admin.site.register(Publisher)
admin.site.register(Author)
admin.site.register(Book)

您没有这样做。 正确的方法是在 < code> models. py 和 admin. py 中设置模型。

models. py :

from django.db import models

# Create your models here.
class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address=models.CharField(max_length=50)
    city=models.CharField(max_length=60)
    state_province=models.CharField(max_length=30)
    country=models.CharField(max_length=50)
    website=models.URLField()

    def __str__(self):
        return self.name


class Author(models.Model):
    salutation=models.CharField(max_length=10)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email=models.EmailField()
    headshot=models.ImageField(upload_to= /tmp )

    def __str__(self):
        return  %s %s  % (self.first_name, self.last_name)


class Book(models.Model):
    title=models.CharField(max_length=10)
    authors=models.ManyToManyField(Author)
    publisher=models.ForeignKey(Publisher)
    publication_date = models.DateField()

    def __str__(self):
        return self.title

admin.py :

from django.contrib import admin
from myproject.myapp.models import Author, Publisher


class AuthorAdmin(admin.ModelAdmin):
    pass


class PublisherAdmin(admin.ModelAdmin):
    pass


admin.site.register(Author, AuthorAdmin)
admin.site.register(Publisher, PublisherAdmin)

此外,如果您的行政课程空空如也,只要有 pass ,您就可以一起省略,所以最终会变成这样:

admin.py :

from django.contrib import admin
from myproject.myapp.models import Author, Publisher


admin.site.register(Author)
admin.site.register(Publisher)

You re using old methods to declare models which should appear in admin interface. This methods are obsolete now. Please consider to manuals and make yourself clear of which version of django you use. And how to get what you want.





相关问题
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 ]="...

热门标签