ciphor
ciphor

Reputation: 8288

Why the Django admin interface didn't appear?

I'm a newbie for django.

I'm trying to run a sample according to a book about django.

I've added "class Admin" to my model classes, however, in the django admin interface, I can only see the "Users", "Groups" and "Sites", none of my model classes appear.

There is no error or warning information, so I don't know what happened and what shall I do next.

Any help?

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

Upvotes: 1

Views: 1251

Answers (5)

stalk
stalk

Reputation: 12054

You need to do three things to be able to edit your models via admin site:

  1. Create file 'admin.py' in your app directory with code (look https://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-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. In your urls.py add the following (look https://docs.djangoproject.com/en/dev/ref/contrib/admin/#hooking-adminsite-instances-into-your-urlconf):

    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. Be sure, that your settings.py satisfy following (look 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
    )
    

Upvotes: 3

Vasiliy Stavenko
Vasiliy Stavenko

Reputation: 1214

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.

Upvotes: 0

dmedvinsky
dmedvinsky

Reputation: 8356

What you did there is not how it's really done. The right way is to have models in models.py and admin classes in 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)

Also, if your admin classes are empty with just pass in them, you can omit them all together, so you end up like this:

admin.py:

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


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

Upvotes: 1

Peerapat A
Peerapat A

Reputation: 430

You should create admin.py and follow by below code

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

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

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799390

You missed the part about registering the models with the admin site.

Upvotes: 1

Related Questions