neromancer_1982
neromancer_1982
2019-01-19 10:58

求助:AttributeError: type object 'Topic' has no attribute 'object'

  • python
  • django

python新手,求助个有关django的问题

django版本:2.1.5
python版本:3.6.0

问题如下:
项目路径\ learning_log
应用路径\learning_logs

\learning_log\urls代码:

from django.contrib import admin

from django.urls import path,include

urlpatterns = [

    path('admin/', admin.site.urls),

    path('',include('learning_logs.urls'))

]

\learning_logs.urls代码:

from django.urls import path

from . import views

app_name = 'learning_logs'

urlpatterns = [

path('',views.index,name='index'),

path('topics/',views.topics,name='topics'),

]

\learning_logs.views代码:

from django.shortcuts import render

from .models import Topic,Entry

def index(request):

return render(request,'learning_logs/index.html')

def topics(request):

a = Topic.object.order_by('date_added')
context = {'topics':a}
return render(request,'learning_logs/topics.html',context)

\learning_logs.models代码:

from django.db import models

class Topic(models.Model):

text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)

def __str__(self):
    return self.text

class Entry(models.Model):

topic = models.ForeignKey(Topic,on_delete=models.CASCADE)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)

class Meta:
    verbose_name_plural = 'entries'

def __str__(self):
    if len(self.text) < 50:
        return self.text
    else:
        return self.text[:50] + '...'

运行网页后:
http://127.0.0.1:8000/ 显示如下,正常

Learning Log - Topics

Learning Log helps you keep track of your learning, for any topic you're learning about.

运行 http://127.0.0.1:8000/Topic/ 后报错如下:

AttributeError at /topics/

type object 'Topic' has no attribute 'object'

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答