Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 1.40 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Django: How to pass object/object id to another template
  2. def list(request):
  3.     listings = listing.objects.all()
  4.     return render_to_response('/../templates/listings.html',{'listings':listings})
  5.  
  6. def detail(request, id):
  7.     #listing = listing.objects.filter(owner__vinumber__exact=vinumber)
  8.     return render_to_response('/../templates/listing_detail.html')
  9.        
  10. {% for listing in object_list %}
  11. <!--<li> {{ listing.title }} </li>-->
  12. <a href="{{ listing.id }}">{{ listing.title}}</a><br>
  13. {% endfor %}
  14.        
  15. {{ id }}
  16.        
  17. # example model
  18. class Example(models.Model):
  19.     name = models.CharField("Name", max_length=255, unique=True)
  20.     #more model fields here
  21.  
  22.     #the permalink decorator with get_absolute_url function
  23.     @models.permalink
  24.     def get_absolute_url(self):
  25.         return ('example_view', (), {'example_name': self.name})
  26.  
  27. #example view
  28.    def example_view(request, name, template_name):
  29.         example = get_object_or_404(Example, name=name)
  30.         return render_to_response(template_name, locals(),
  31.                               context_instance=RequestContext(request))
  32.  
  33.    #example urls config
  34.        url(r'^(?P<name>[-w]+)/$', 'example_view', {'template_name': 'example.html'}, 'example_view')
  35.        
  36. <a href={{ example.get_absolute_url }}>{{ example.name }}</a>
  37.        
  38. def detail(request, id):
  39.     l = listing.objects.get(pk=id)
  40.     return render_to_response('/../templates/listing_detail.html', {'listing':l})