nirvana
nirvana

Reputation: 195

Django: How to pass object/object id to another template

I am new to the Django web framework.

I have a template that displays the list of all objects. I have all the individual objects listed as a link (object title), clicking on which I want to redirect to another page that shows the object details for that particular object. I am able to list the objects but not able to forward the object/object id to the next template to display the details.

views.py

def list(request):
    listings = listing.objects.all()
    return render_to_response('/../templates/listings.html',{'listings':listings})

def detail(request, id):
    #listing = listing.objects.filter(owner__vinumber__exact=vinumber)
    return render_to_response('/../templates/listing_detail.html')

and templates as: list.html

{% for listing in object_list %}
<!--<li> {{ listing.title }} </li>-->
<a href="{{ listing.id }}">{{ listing.title}}</a><br>
{% endfor %}

detail.html

{{ id }}

Upvotes: 4

Views: 10473

Answers (3)

Jamie Forrest
Jamie Forrest

Reputation: 11113

In your detail method, just pass the listing into your template like so:

def detail(request, id):
    l = listing.objects.get(pk=id)
    return render_to_response('/../templates/listing_detail.html', {'listing':l})

Upvotes: 0

Jingo
Jingo

Reputation: 3240

You should check the @permalink decorator. It enables you to give your models generated links based on your urls pattern and corresponding view_function.

For example:

# example model
class Example(models.Model):
    name = models.CharField("Name", max_length=255, unique=True)
    #more model fields here

    #the permalink decorator with get_absolute_url function
    @models.permalink
    def get_absolute_url(self):
        return ('example_view', (), {'example_name': self.name})

#example view
   def example_view(request, name, template_name):
        example = get_object_or_404(Example, name=name)
        return render_to_response(template_name, locals(),
                              context_instance=RequestContext(request))

   #example urls config
       url(r'^(?P<name>[-\w]+)/$', 'example_view', {'template_name': 'example.html'}, 'example_view')

Now you can do in your templates something like this:

<a href={{ example.get_absolute_url }}>{{ example.name }}</a>

Hope this helps.

Upvotes: 0

Danica
Danica

Reputation: 28846

The variables that you pass in the dictionary of render_to_response are the variables that end up in the template. So in detail, you need to add something like {'listing': MyModel.objects.get(id=vinumber)}, and then the template should say {{ listing.id }}. But hat'll crash if the ID doesn't exist, so it's better to use get_object_or_404.

Also, your template loops over object_list but the view passes in listings -- one of those must be different than what you said if it's currently working....

Also, you should be using the {% url %} tag and/or get_absolute_url on your models: rather than directly saying href="{{ listing.id }}", say something like href="{% url listing-details listing.id %}", where listing-details is the name of the view in urls.py. Better yet is to add a get_absolute_url function to your model with the permalink decorator; then you can just say href="{{ listing.get_absolute_url }}", which makes it easier to change your URL structure to look nicer or use some attribute other than the database id in it.

Upvotes: 4

Related Questions