简单的Django程序导致我麻烦 [英] Simple Django program causing me trouble

查看:145
本文介绍了简单的Django程序导致我麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历了不少django教程,我终于准备好自己出发了。然而,我的第一个非教程程序是抛出一个错误,我一直在敲我的头几天。我期望它是一个非常noob的问题,因为,我是。



当我使用这个视图

  def todo(request) 
latest_list = Item.objects.all()
return HttpResponse(latest_list)

我得到

 征服djangocan我这样做?学习这个

哪些是填充数据库的四个项目。不是很方便,因为它们是连接在一起的,而且它们似乎没有被交给模板。



当我更改我的view.py以尝试与模板使用

  def todo(request):
latest_list = Item.objects.all()
return render_to_response,('index.html',{latest_list,latest_list})

p>

 'tuple'对象没有属性'status_code'

可能是返回self.task的模型将返回限制在只有该字段?我看过的其他教程似乎只返回一个值(并且只返回'self')得到一个非常相似的错误。



也可能是我没有传递在



任何有助于推动我正确路径的帮助将不胜感激。



Greg



我的model.py






 从django.db导入模型

类项目(models.Model):
task = models.CharField(max_length = 60)
taskNotes = models.CharField(max_length = 600 )
created = models.DateTimeField(auto_now_add = True)
done = models.BooleanField(default = False)

def __unicode __(self):
return self。任务






我的views.py






  from django.shortcuts import render_to_response,get_object_or_404 
from django.template import RequestContext
从django.http导入HttpResponse
从myTo do.todo.models import Item

def todo(request):
latest_list = Item.objects.all()
return HttpResponse(latest_list)






我的index.html(模板)






  {%if latest_list%} 
< ul>
{%for latest_list%}
< li> {{Item.task}}< / li>
{%endfor%}
< / ul>
{%else%}
< p>看起来全部完成了< / p>
{%endif%}


解决方案

  return render_to_response,('index.html',{latest_list,latest_list})

删除那个逗号分配器render_to_response,你应该可以。原因:逗号使返回值成为一个元组对象,但需要从视图返回一个HttpResponse对象。


I've gone through quite a few django tutorials, and I'm finally getting ready to head out on my own. However, my first non-tutorial program is throwing an error, and I've been banging my head for a couple of days. I expect it to be a very noob problem, because, well, I am.

When I use this view

 def todo(request): 
        latest_list = Item.objects.all()
        return HttpResponse(latest_list) 

I get

conquer djangocan I do this?learn thislearn this

which are the four items that populate the database. Not very handy since they are concatenated, and they don't appear to be handed off to the template.

When I change my view.py to try to talk to the template using

def todo(request):
        latest_list = Item.objects.all()
        return render_to_response,('index.html', {"latest_list", latest_list})

I get

'tuple' object has no attribute 'status_code'

Could it be that the model that's returning 'self.task' is limiting the return to only that field? Other tutorial I looked at seemed to return only one value (and returning just 'self' gets me a very similar error.

It could also be that I'm not passing in

Any help that would push me down the correct path would be greatly appreciated.

Greg

My model.py


from django.db import models

class Item(models.Model):
    task = models.CharField(max_length=60)
    taskNotes = models.CharField(max_length=600)
    created = models.DateTimeField(auto_now_add=True)
    done = models.BooleanField(default=False)

    def __unicode__(self):
        return self.task


My views.py


from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.http import HttpResponse
from myToDo.todo.models import Item

def todo(request): 
    latest_list = Item.objects.all()
    return HttpResponse(latest_list) 


My index.html (template)


{% if latest_list %}
    <ul>
    {% for task in latest_list %}
        <li>{{ Item.task }}</li>
    {% endfor %}
    </ul>
{% else %}
    <p>Looks like you're all done</p>
{% endif %}

解决方案

return render_to_response,('index.html', {"latest_list", latest_list})

Remove that comma affer render_to_response and you should be ok. Reason: the comma makes the return value a tuple object, but need to return an HttpResponse object from a view.

这篇关于简单的Django程序导致我麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆