Problem with DetailView

94 views
Skip to first unread message

Soviet

unread,
Jul 7, 2012, 4:36:10 PM7/7/12
to django...@googlegroups.com
Now that I have basic understanding of models, I encountered even more confusing subjects - views and urls. Now, the class-generic views are quite easy to grasp at basic level, but I fail to understand what's wrong with this code:

urlpatterns = patterns('',
    (r'^$', ListView.as_view(
        model=Car,
        context_object_name="cars_list",
        template_name='data/cars_list.html',
        )),
    (r'^(?P<pk>\d+)/$', DetailView.as_view(
        model=Car,
        context_object_name="car_details",
        template_name='data/car_details.html',
        )),
)

The ListView is working fine, but when I try to get the details about single car, all I'm getting is error: "No car found matching the query". I tried adding 'queryset = Car.objects.all()' both in urls.py and in views.py, creating custom class, but the error persists.

Smaran Harihar

unread,
Jul 7, 2012, 4:44:23 PM7/7/12
to django...@googlegroups.com

If I am not wrong. The issue is with the List View not Detail View.

You need to provide, List View with query set. If you are providing model parameter, you will also need to give it a primary key 'pk', like you did for Detail View.

Hope that helps,
Smaran

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/fQZA1sl13VkJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

kooliah

unread,
Jul 7, 2012, 4:46:28 PM7/7/12
to django...@googlegroups.com
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/fQZA1sl13VkJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
The parameters are from url and automatically passed to your view, i
don't have to pass them explicitily, take a look on
https://docs.djangoproject.com/en/1.3//topics/http/urls/

Soviet

unread,
Jul 7, 2012, 5:10:17 PM7/7/12
to django...@googlegroups.com
But the ListView is working fine. And they don't use <pk> in the documentation, just this, which I modified to fit my model:

urlpatterns = patterns('',
    (r'^publishers/$', ListView.as_view(
        model=Publisher,
    )),
)
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.

Thomas Orozco

unread,
Jul 8, 2012, 5:20:44 AM7/8/12
to django...@googlegroups.com

Could you give us the fill error message displaying what ended up being passed to the view?

Also, are you sure a car exists with the id / pk you're passing?

To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/2RYQ-kplLroJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.

Soviet

unread,
Jul 8, 2012, 6:42:28 AM7/8/12
to django...@googlegroups.com
> Also, are you sure a car exists with the id / pk you're passing?

Turns out that's the problem. I wanted to use a field from my Car model: car_id, so the link on the website would look like: www.blablabla.com/cars/3421, where 3421 is the car_id. Unfortunately, the car_id is not the same as <pk>. Can I achieve something like that with class based generic views? And where do I look for different solution?

lacry...@gmail.com

unread,
Jul 8, 2012, 9:00:39 AM7/8/12
to django...@googlegroups.com

More data would be nice. What url are you goimg to that's failing, exactly? Are you clicking a link from your ListView? If so, could you show your template?

-----Mensaje original-----
De: Soviet
Enviados: 07/07/2012 18:10:17
Asunto: Re: Problem with DetailView
>> django-users...@googlegroups.com.
>
Correo truncado a 2,000 caracteres.
:::0:a0e2896b0575c65d30a12e3ca365460d:7d0::::

Tomas Neme

unread,
Jul 8, 2012, 9:53:02 AM7/8/12
to django...@googlegroups.com
> Turns out that's the problem. I wanted to use a field from my Car model:
> car_id, so the link on the website would look like:
> www.blablabla.com/cars/3421, where 3421 is the car_id. Unfortunately, the
> car_id is not the same as <pk>. Can I achieve something like that with class

what's car_id and why isn't it your PK?

you should post your full models when asking for help, in general.

As a general rule, having a car_id field in a Car model is
unnecessary. Models come with ids of their own by default, you don't
need to define them. If you have IDs of your own that you want to
reflect in your database, first of all, I'd call it just "id", so you
can do my_car.id, my_car.car_id sounds a little bit redundant to my
taste, and secondly, you can define it as id =
models.IntegerField(primary_key=True). If you do that, your pk will be
the ID you defined (you can do this with a CharField as well, I think)

--
"The whole of Japan is pure invention. There is no such country, there
are no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.
Message has been deleted

Soviet

unread,
Jul 8, 2012, 12:53:59 PM7/8/12
to django...@googlegroups.com
That's because of my example. I have a LEGO Set model:

class Set(models.Model):
    lego_id = models.CharField(max_length=
32, primary_key=True, blank=True) #lego_id stores the unique number that's on every Lego box
    text_name = models.CharField(max_length=64)
    #...

And I resolved my problem by adding primary_key=True, like you said. (I hope it's OK if the pk is a CharField?). Now the link works fine, and the lego_id is the link, like so:

(r'^sets/(?P<pk>\d+)/$', SetDetailView.as_view(
    model=Set,
    context_object_name="set_details",
    template_name='data/set_details.html',
    )),

But the site is empty. Here's my extremely simple template:

{% block title %}{{ set.text_name }}{% endblock %}

{% block content %}
    <h2>Set:</h2>

    {{ set.theme }} - {{ set.subtheme }}
{% endblock %}

All I can see is the one " - " :).

EDIT: made a typo in the last post.

Tomas Neme

unread,
Jul 8, 2012, 1:05:17 PM7/8/12
to django...@googlegroups.com
Man...

> (r'^sets/(?P<pk>\d+)/$', SetDetailView.as_view(
> model=Set,
> context_object_name="set_details",
> template_name='data/set_details.html',
> )),
>
> But the site is empty. Here's my extremely simple template:
>
> {% block title %}{{ set.text_name }}{% endblock %}
>
> {% block content %}
> <h2>Set:</h2>
>
> {{ set.theme }} - {{ set.subtheme }}
> {% endblock %}

and where, pray, would that 'set' variable be populated when you very
explicitly told the DetailView you wanted your context object named
'set_details'?

Soviet

unread,
Jul 8, 2012, 2:47:06 PM7/8/12
to django...@googlegroups.com
Ha! Thank you and sorry for my stupidity. I was confused by the fact that I didn't had to do this in {for ... in ...}.

lacry...@gmail.com

unread,
Jul 8, 2012, 5:46:28 PM7/8/12
to django...@googlegroups.com

To do what? I'm trying to lead you to conclussions, but i seem to be leading you to confusion instead.

You should read the generic views docs again. For any single object view, the default object name is 'object', unless you set the context_object_name parameter. The list view calls it 'objects'. The template name doesn't need to be set up either, if you don't want to, it will default to '<appname>/<model>_list.html (or _detail.html)

-----Mensaje original-----
De: Soviet
Enviados: 08/07/2012 15:47:06
Asunto: Re: Problem with DetailView

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/0yKZyt9Wzd4J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages