开发者

Django generic view not receiving an object (template issue?)

开发者 https://www.devze.com 2022-12-26 18:13 出处:网络
My model: class Player(models.Model): player_name = models.CharField(max_length=50) player_email = models.CharField(max_length=50)

My model:

class Player(models.Model):
    player_name = models.CharField(max_length=50)
    player_email = models.CharField(max_length=50)

def __unicode__(self):
    return self.player_name

My root urls.py

urlpatterns = patterns('',
    (r'^kroster/', include('djangosite.kroster.urls')),
    (r'^admin/(.*)', admin.site.root),
)

My kroster urls.py:

from djangosite.kroster.models import Player

info_dict = {
    'queryset': Player.objects.all(),
}

urlpatterns = patterns('',
    (r'^$', 'django.views.generic.list_detail.object_list', info_dict),
    (r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
)

My player_list.html template:

<h1>Player List</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<ul>
    {% for player in object.player_set.all %}
        <li id="{{ player.id }}">{{ forloop.counter }} .)&nbsp;&nbsp;{{ player }}&开发者_运维问答lt;/li>
    {% endfor %}
</ul>

Sadly my template output is this.

<h1>Player List</h1>
<ul>

</ul>

Apologies if this is a stupid mistake. There has to be something wrong with my template.


The variable for list view (unless otherwise specified) is object_list.

For details, it's object. Also, you'll need another template for the detail view. By default the template name is: <app_label>/<model_name>_detail.html (unless you specify it differently)

All is in Generic views.


Your template should be something like this:

<h1>Player List</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<ul>
    {% for player in object_list %}
        <li id="{{ player.id }}">{{ forloop.counter }} .)&nbsp;&nbsp;{{ player }}</li>
    {% endfor %}
</ul>
0

精彩评论

暂无评论...
验证码 换一张
取 消