Django: how to pass an object / object id to another template

I am new to Django web infrastructure.

I have a template that displays a list of all objects. I have all the individual objects listed as a link (the name of the object), by clicking on which I want to redirect to another page that displays the details of the object for this particular object. I can enumerate objects, but could not forward the object / object identifier 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: list.html

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

detail.html

 {{ id }} 
+4
source share
3 answers

The variables you pass in the dictionary render_to_response are variables that fall into 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, if the identifier does not exist, it will be broken, so it is better to use get_object_or_404 .

In addition, your template goes through object_list , but the view goes through listings - one of them should be different from what you said if it works at the moment.

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

+4
source

You should check out @permalink decorator . This allows you to create the models you create based on the URL pattern and the corresponding view_function.

For instance:

 # 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 something like this in your templates:

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

Hope this helps.

0
source

In your detailed method, just go to your template in your template:

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

Source: https://habr.com/ru/post/1393092/


All Articles