neromancer_1982
neromancer_1982
采纳率0%
2019-01-18 22:08

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

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条回答

  • weixin_43324767 文竹. 2年前

    a = Topic.object.order_by('date_added') ,object后面加个s就好了 我也被这个问题卡了好久刚解决就来告诉你了

    点赞 6 评论 复制链接分享
  • keegopo by豪 2年前

    我也是被s卡了大半天,用p ython manage.py shell查了一下,object要加s

    点赞 1 评论 复制链接分享
  • berlinsaint berlinsaint 2年前

    Topic定义呢

    点赞 1 评论 复制链接分享

相关推荐