在将我的模型注册到Django admin时获取AttributeError?

时间:2017-02-14 14:19:20

标签: django django-models django-admin

我想做什么:

尝试将我的模型注册到django admin

我会遇到什么问题:

我收到以下错误:

Attribute Error

我的代码:

admin.py:

from django.contrib import admin
from .models import UserProfile, Post

admin.site.register(UserProfile,Post)

model.py:

from django.contrib.auth.models import User
from django.db import models

class UserProfile(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    avatar = models.ImageField(upload_to='static/media/',max_length=100)

class Post(models.Model):
    title = models.CharField(max_length=120)
    content = models.TextField()

urls.py:

from django.conf.urls import url
from .import views

urlpatterns = [
    url(r'^register/$',views.registerUser),
    url(r'^$', views.index, name="Index"),
    url(r'^validateRegisterForm/$',views.validateRegisterForm),
    url(r'^validateLoginForm/$',views.validateLoginForm),
    url(r'^article/$', views.article, name="Article"),
    url(r'^Login/$',views.loginUser, name="Login"),
    url(r'^Logout/$',views.logoutUser, name="Logout"),
]

注意:我是django的新手,所以不太了解它。

1 个答案:

答案 0 :(得分:2)

这应该是

admin.site.register(UserProfile)
admin.site.register(Post)

Admin.site.register有两个参数,第一个是模型类,可选的第二个参数是管理类。在您的代码中,您将另一个模型作为管理类传递。

有关详细信息,请参阅:https://docs.djangoproject.com/en/1.10/ref/contrib/admin/