Django show render_to_response in template

Hello and thank you in advance.

I know this is a general question about noob, and I searched the forum and read and re-read the documentation, so please be careful.

I have a view:

#views.py

from django.shortcuts import render_to_response
from django.shortcuts import render
from django.http import HttpResponse, HttpRequest, HttpResponseRedirect
from acme.acmetest.models import Player
from acme.acmetest.models import PickForm

def playerAdd(request, id=None):
    form = PickForm(request.POST or None,
                       instance=id and Player.objects.get(id=id))

    # Save new/edited pick
    if request.method == 'POST' and form.is_valid():
        form.save()
        return HttpResponseRedirect('/draft/')

    #return render_to_response('makepick.html', {'form':form})
    return render(request, 'makepick.html', {'form':form})

def draftShow(request):
    draft_list = ['1', 'hello', 'test', 'foo', 'bar']
    #draft_list = Player.objects.all()
    #return render_to_response('makepick.html', {'draft_list' :draft_list}, context_instance=RequestContext(request))
    return render_to_response('makepick.html', {'draft_list' :draft_list})

I am trying to display it on a .html page.

#makepick.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML lang="en">
<head>
    <title>Pick</title>
</head>
<body>

    <form method="POST" action="">
        {% csrf_token %}
        <table>{{ form }}</table>
        <input type="submit" value="Draft Player" 
    </form><br /><br /> 

Your picks so far:<br />
{% for draft in draft_list %}
    {{ draft.playernumber }}
{% endfor %}

</body>
</HTML>

Where playernumber is the field in the "Player" model class in models.py.

#urls.py

from django.conf.urls.defaults import patterns, include, url
from acme.acmetest import views


urlpatterns = patterns('',
    ('^$', 'acme.acmetest.views.playerAdd'),
)

Thank you for your help!

dp

+5
source share
2 answers

Well, it looks like your template handles fine. Therefore, you will need to see if it really draft_listcontains anything and what playernumberfor each captured object.

In the root directory of your project, run:

python manage.py shell

, , , - Player , playernumber :

from acme.acmetest.models import Player
draft_list = Player.objects.all()
for draft in draft_list:
    print draft.playernumber
+6

, makepick.hmtl templates TEMPLATE_DIR.

, , Player.objects.all() - . , playernumber Player.

+1

All Articles