123
123

Reputation: 8951

DetailView not showing data

I am trying to display a ListView of items that when clicked opens the respective DetailView for that item. This is a pretty straightforward thing to do in Django, but I'm running into some trouble with it.

I have successfully created a ListView, but I can't get the DetailView to display the information for each item.

I am building a password safe. The ListView lists all the passwords saved, and the DetailView is the view/edit page for each password. And yes, I realize actually putting a password safe on the internet is a terrible idea, it's just a test project :)

My PasswordSafe class:

class PasswordSafe(models.Model):
    user = models.OneToOneField(User)

    pwWebsite = models.CharField(max_length=30)
    pwUsername = models.CharField(max_length=30)
    pwEmail = models.CharField(max_length=30)
    pwPassword = models.CharField(max_length=30)

    def __unicode__(self):              # __unicode__ on Python 2, __str__ on Python 3
        return self.pwWebsite

User.passwordSafe = property(lambda u: PasswordSafe.objects.get_or_create(user=u)[0])

My urls.py:

from django.conf.urls import patterns, include, url
from django.views.generic import ListView, DetailView
from models import PasswordSafe

urlpatterns = [
    url(r'^passwordSafe/$', ListView.as_view(queryset=PasswordSafe.objects.all().order_by('-pwWebsite')[:10], template_name = 'userprofile/passwordSafe_list.html')),
    url(r'^passwordSafe/(?P<pk>\d+)/$', DetailView.as_view(model=PasswordSafe, template_name='userprofile/passwordSafe.html')),
    ]

I use the following code successfully in my ListView template (passwordSafe_list.html):

{% for passwordSafe in object_list %}

    <h2><a href="/accounts/passwordSafe/{{passwordSafe.id}}">{{ passwordSafe.pwWebsite }}</a></h2>

{% endfor %}

However, when I try to use the passwordSafe data in my Detailview (the passwordSafe.html template), nothing shows up. I use the following code to try and change the title:

<title>{% block title %} | Edit {{ passwordSafe.pwWebsite }} Password{% endblock %}</title>

This should show a title something like "Password Safe | Edit Google Password", but passwordSafe.pwWebsite returns with nothing. Am I doing something wrong in trying to reference passwordSafe from this template? I know I haven't passed a query set like I did with the ListView, but I'm not sure if that's part of the problem or not.

Upvotes: 0

Views: 543

Answers (1)

Alasdair
Alasdair

Reputation: 309109

In the DetailView, Django uses the model name in lowercase, by default, if you haven't set context_object_name.

{{ passwordsafe.pwWebsite }} 

Upvotes: 2

Related Questions