5

Hello and thank you in advance.

I know this is total noob question, and I have searched in the various forum and read and re-read the documentation, so please be gentle.

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 get it to render to a template .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 field in the model class "Player" 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

dpbklyn
  • 781
  • 3
  • 10
  • 19
  • I get the HTML page, and other django code renders correctly on it, but all I get in the area I am writing now, is "your picks so far:" then nothing. I would love it to show a list of player numbers from the draft list/model. – dpbklyn Feb 08 '12 at 20:57
  • 1
    I can't spot any errors in your code ATM; try to debug it, e.g. via simple print statements (e.g. `print draft_list` before it goes to the template). – miku Feb 08 '12 at 21:00

2 Answers2

6

Well, it looks like your template is rendering fine. So you'll have to see if draft_list actually contained anything and what playernumber is for each object that was grabbed.

In the root directory of your project, run:

python manage.py shell

Now that you're in the shell, to test whether there are actually any Player objects in your database and see what the playernumber property of each object returns:

from acme.acmetest.models import Player
draft_list = Player.objects.all()
for draft in draft_list:
    print draft.playernumber
U-DON
  • 2,102
  • 14
  • 14
  • @ U-DON thank you...Here are the results:`code`for draft in draft_list: >>> for draft in draft_list: ... print draft.playernumber ... 23 21 24 174 1234 999 `code` – dpbklyn Feb 08 '12 at 21:19
  • Kind of a shot in the dark, but try modifying your view to have this line: `return render_to_response('makepick.html', {'draft_list' :draft_list}, context_instance=RequestContext(request))`. – U-DON Feb 08 '12 at 22:10
  • Failing that, could you see what happens if you use another list, but with arbitrary values, and loop through it in the template? – U-DON Feb 08 '12 at 22:16
  • I just tried this with the same result...`def draftShow(request): draft_list = ['1', 'hello', 'test', 'foo', 'bar'] return render_to_response('makepick.html', {'draft_list' :draft_list})` I am wondering if there is a problem with the other view I am feeding into the same html template. I have a form that is using a `return HttpResponseRedirect('/draft/')` to return a response. Note that `/draft/` actually calls the same page as we are starting on, but it is worth mentioning... – dpbklyn Feb 09 '12 at 02:33
  • Is there another option other than using `render_to_response`? All I really want to do is to show the items listed in the db that will update each time the table is updated. Or is the problem on the HTML page `for loop`? – dpbklyn Feb 09 '12 at 02:42
  • There is also `HttpResponse`, but that doesn't use the template. If you want to eventually show a substantial HTML page, you should probably rely on rendering a template. Can you show your `urls.py` file? And what's the URL you're using to access the page? – U-DON Feb 09 '12 at 04:51
  • Sorry it took so long to get back...This is the URLS file for the app, the app..`from django.conf.urls.defaults import patterns, include, url from acme.acmetest import views urlpatterns = patterns('', ('^$', 'acme.acmetest.views.playerAdd'), ) ` Here is where it is called in the project urls...`from django.conf.urls.defaults import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', ('^$', include('acme.acmetest.urls')), url(r'^admin/doc/', include('django.contrib.admindocs.urls')), url(r'^admin/', include(admin.site.urls)), )` – dpbklyn Feb 10 '12 at 00:55
  • As a tip, it would look cleaner if you just edit your original post and include any updates or new code there. Anyway, I don't see the `draftShow` view being matched to any URL in your app's `url.py`, but it should be if you want it to be accessible. Did you forget to include it here? If not, how were you able to access the page before? – U-DON Feb 10 '12 at 02:53
  • Thank you for the tip and I am sorry for the difficult reading. DraftShow isn't in urls.py. I guess I am confused about urls.py. How do I include it? I would like it called on the same template at the same url as the view playerAdd (which is included in the urls.py file.) – dpbklyn Feb 10 '12 at 15:11
  • I have a feeling this whole problem lies in my views.py file. I am re-printing my views.py file above in its entirety. I think the problem is with how I am handling the responses of the two views.... – dpbklyn Feb 10 '12 at 17:20
  • OK...when I created a new html page with only the code that references the draftShow view, and called the page by going to the "/draft" url it worked. I assume I have to create a new html "landing" page for each view then call THOSE to the template html page that visitors will access.... – dpbklyn Feb 10 '12 at 21:31
  • So has your problem been solved? Also, to answer your question about adding the URL to match to the draftShow view, it is very similar to the line `('^$', 'acme.acmetest.views.playerAdd'),`, but with `acme.acmetest.views.draftShow` being matched to something like `'^drafts/$'`. Refer to https://docs.djangoproject.com/en/dev/topics/http/urls/. – U-DON Feb 11 '12 at 00:17
  • Yes this problem has been solved, THANK YOU!!!Isit possible to have one URL call two separate views? – dpbklyn Feb 11 '12 at 17:33
1

Make sure that makepick.hmtl is in your apps templates directory, or in your TEMPLATE_DIR.

You can check in your view to verify that Player.objects.all() is actually returning something. Make sure playernumber is an actual property of the Player object.

dm03514
  • 54,664
  • 18
  • 108
  • 145
  • makepick.html is in the TEMPLATE_DIR. I have a form on that same page that is rendered correctly. How can I check my view to see if Player.objects.all() is returning something? playernumber is a property of the Player object in my models.py – dpbklyn Feb 08 '12 at 21:00