Django 类别';任务';没有';对象';成员
#我不知道我在哪里犯了错误,有一个错误说类'Task'没有'objects'成员Django 类别';任务';没有';对象';成员,django,django-models,django-views,Django,Django Models,Django Views,#我不知道我在哪里犯了错误,有一个错误说类'Task'没有'objects'成员 ''' views.py from django.shortcuts import render from django.http import HttpResponse from .models import * # Create your views here. def index(request): tasks = Task.objects.all() context =
'''
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
# Create your views here.
def index(request):
tasks = Task.objects.all()
context = {'tasks':tasks}
return render(request,'task/list.html',context)
'''
'''
models.py
from django.db import models
# Create your models here.
class Task(models.Model):
title = models.CharField(max_length=200)
complete =models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
'''
'''
list.html
<h3>To Do</h3>
{%for first in task %}
<div>
<p>{{first}}</p>
</div>
{% endfor %}
'''
上面说
Task
Task Object(2)
Task Object(1)
在views.py中测试此代码:
from django.shortcuts import render
from django.http import HttpResponse
from . import models
def index(request):
tasks = models.Task.objects.all()
context = {'tasks':tasks}
return render(request,'task/list.html',context)
您需要缩进
\uuuu str\uuuuu
方法,因此\uuuuu str\uuuu
方法应该是任务
类的成员。在视图中(在对象
之前)打印(任务.\uuuu mro\uuuuuu)时,您能提供结果吗?它只是打印到DoI,我已经试过了,正如你在下面所说的,但是当我打开任务时,它给出了一个操作错误
from django.shortcuts import render
from django.http import HttpResponse
from . import models
def index(request):
tasks = models.Task.objects.all()
context = {'tasks':tasks}
return render(request,'task/list.html',context)