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_o开发者_如何转开发r_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.
You goofed on your return
.
def todo(request):
latest_list = Item.objects.all()
return render_to_response('index.html', {"latest_list", latest_list})
Note the lack of comma after the function name.
精彩评论