One-To-Many Relationship in Django and QuerySets - python

I have a one-to-many relationship between restaurants and photos. I am having trouble displaying a single image for a corresponding restaurant.
Right now I get all of the restaurants like this:
restaurants = Restaurant.objects.all()
In my template I have a loop that goes through the restaurants and displays information. However, I am unsure how to retrieve an image that is associated with the restaurant:
{% for restaurant in restaurants %}
{{restaurant.name}}
{{restaurant.address}}
{{CODE TO DISPLAY IMAGE}}
...
{% endfor %}
How can I retrieve the first image corresponding to the restaurant? I have tried restaurant.restaurantphoto_set.get.image, but this only works when there is a single image.

It's a little hard to diagnose without knowing what your model looks like, but have you tried:
restaurant.restaurantphoto_set.all()
The docs have some pretty solid examples here:
https://docs.djangoproject.com/en/1.7/topics/db/examples/many_to_one/

do in your template
{% for photo in restaurant.restaurantphoto_set.all %}
{{ photo.image }}
{% endfor %}

This is how I ended up solving my problem:
{% for restaurant in restaurants %}
{{restaurant.name}}
{{restaurant.address}}
{% for photo in restaurant.restaurantphoto_set.all %}
{% if forloop.counter == 1 %}
<img src= "{% static photo.image %}" class="img-curved">
{% endif %}
{% endfor %}
{% endfor %}

Related

Django iterate queryset just n number of times in templates

My issue is quite simple. I am using Django taggit.
I want to iterate this only 2 times. Means to display only 4 tags in templates.
{% for tag in data.tags.all %}
{{tag}}
{% endfor %}
I have tried this, but it is not making any sense:
{% for tag in data.tags.all|ljust:"2" %}
{{tag}}
{% endfor %}
Can anyone suggest how can I achieve it?
You can make use of the |slice template filter [Django-doc]:
{% for tag in data.tags.all|slice:':2' %}
{{ tag }}
{% endfor %}

Django - The page don't load after click

i'm new in Django developing.
I'm following the tutorial about Library on MDN (https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django)
Until i follow the code all work but i'm trying implement author page by myself. Probably is very stupid issue but is one day that i'm turning around like a dog with its tail.
There is 2 page: author_list and author detail.
I set urls.py (in my project) i set view.py and crate my template.
I follow the same step of tutorial for realize book_list and book_detail but when i click on my author the page don't go to the detail of that author and stay in author_list.html.
Here the code urls.py :
path('authors/', views.AuthorListView.as_view(), name='authors'),
path('author/<int:pk>', views.AuthorDetailView.as_view(), name='author-detail'),
Here views.py:
class AuthorListView(generic.ListView):
model = Author
class AuthorDetailView(generic.ListView):
model = Author
Here author_list.html with link get_absolute_url:
{% extends "base_generic.html"%}
{% block content %}
<h1>Author list</h1>
{% if author_list %}
<ul>
{% for aut in author_list %}
<li>{{ aut.first_name }} - {{ aut.last_name }}</li>
{% endfor %}
</ul>
{% else %}
<p>There are no author.</p>
{% endif %}
{% endblock %}
Here author_detail.html:
{% extends "base_generic.html" %}
{% block content %}
<h1>Author</h1>
{% if author %}
<p><strong>Nome: </strong> {{ author }}</p>
<p><strong>Nato il : </strong> {{ author.date_of_birth }}</p>
<p><strong>Morto il : </strong> {{ author.date_of_death }}</p>
{% endif %}
{% endblock %}
Here the screenshot
Author_list.html before click url=catalog/authors/
After click url change but page not
Thank to all for help
I believe you need DetailView instead of ListView for AuthorDetailView.
Looks like a typo to me, you want generic.DetailView (instead of ListView) for the author/<int:pk> path.
I also don't think it's right to extend base_generic for the template for the detail view. But that depends exactly what is in this base template.

django template print out by filter id value

I want to print value by id in database,And don't know which keywords to find in Google.
in my views.py, I send transen = TransEn.objects.all() to template
and this will print all datas from database:
{% for words in transen %}
{{words.words|safe }}
{% endfor %}
But I want to print by the value of the id Like:
(Because they are words in English for translating website)
I don't know how to write this in template, please guide me, Thank you very much.
<div><span> TransEn.objects.filter(id='2') </span></div>
<div> TransEn.objects.filter(id='3') </div>
UPDATE:
I have found a method:
I can use if tag, but are there another ideas??
<div>
{% for words in transen %}
{% if words.id == 2 %}
{{ words.words|safe }}
{% endif %}
{% endfor %}
</div>
<div>
{% for words in transen %}
{% if words.id == 3 %}
{{ words.words|safe }}
{% endif %}
{% endfor %}
</div>
If you want to access each item in the QuerySet individually, by index, you should cast it to a list first. You should change your views.py to:
transen = list(TransEn.objects.all())
And then in your template you can access them by index like so:
<div><span> {{ transen.1.words }} </span></div>
<div> {{ transen.2.words }} </div>
A warning from the Django docuemtnation about casting a QuerySet to a list:
Be warned, though, that this could have a large memory overhead, because Django will load each element of the list into memory. In contrast, iterating over a QuerySet will take advantage of your database to load data and instantiate objects only as you need them.

How To Access Many to Many Attribute in Django

I am very new to web development and I have created a sample project using Django. So far I have a Django powered page that displays the contents of one of my database's model objects which is called Publications. The code I have in my view template is:
<html><head><title>Publications</title></head>
<body>
<h1>Publications</h1>
<ul>
{% for publication in publication_list %}
<li>{{ publication.title }} </li>
{% endfor %}
</ul>
</body></html>
This works fine, but now I would like to access and display a many to many attribute on Publications called Tags. I have tried adding another for tag as follows:
<html><head><title>Publications</title></head>
<body>
<h1>Publications</h1>
<ul>
{% for publication in publication_list %}
<li>{{ publication.title }} </li>
{% for tag in publication_list.tags %}
<li>{{ tag.title }} </li>
{% endfor %}
{% endfor %}
</ul>
</body></html>
I realize this is quite wrong, but I don't see how to access the Tags model. For reference, my function for displaying the publications in the view is:
def display_publications(request):
publication_list = Publication.objects.order_by('title')[:10]
return render(request, 'publications.html', {'publication_list': publication_list})
And my Publications and Tag Models are:
class Tag(models.Model):
title = models.CharField(max_length=50)
class Publication(models.Model):
title = models.CharField(max_length=200)
tags = models.ManyToManyField(Tag, blank=True)
Any help is appreciated.
What you are doing only accesses the ManyRelatedManager. You need to specify a query against that manager. In python, it would be:
publication.tags.all()
In a django template it would be:
{% for tag in publication.tags.all %}
{{ tag }}
{% endfor %}
This should be covered in the official documention on many-to-many relationships.
Edit: Here's a good example of how many-to-many relationships work: https://docs.djangoproject.com/en/1.5/topics/db/examples/many_to_many/
Because you seem to be having some trouble with this, given your comments on the other question, here are the changes to the template. You do not need to modify the view at all from what you have given above.
{% for publication in publication_list %}
<li>{{ publication.title }}
<ul>
{% for tag in publication.tags.all %}
<li>{{ tag.title }} </li>
{% endfor %}
</li>
</ul>
{% endfor %}

Django query manytomanyfield in template

How can I query a manytomanyfield in a Django template?
For example, this if statement doesn't work (I know I can't call functions with arguments in Django templates), but this shows what I'd like to do:
template.html
{% for post in posts %}
{% if post.likes.filter(user=user) %}
You like this post
{% else %}
<a>Click here to like this post</a>
{% endif %}
{% endfor %}
models.py
class User(Model):
# fields
class Post(Model):
likes = ManyToManyField(User)
In order to do what you are looking for, you could do the following:
{% for post in posts %}
{% if user in post.likes.distinct %}
You like this post
{% else %}
<a>Click here to like this post</a>
{% endif %}
{% endfor %}
Alternatively, you could use Greg's approach. The advantage of his answer is that it would scale better when you get into very large datasets. This approach does not require you to write any custom filters.
It doesn't work because you appear to be writing python code in a template... you need to either run the loop in your view and pass a list of posts and their information to the template, or write a template filter that determines whether a certain user likes a post. For example:
from django import template
register = template.Library()
#register.filter
def is_liked_by(post, user):
return bool(post.likes.filter(user=user))
Then in your template:
{% for post in posts %}
{% if post|is_liked_by:request.user %}
You like this post
{% else %}
<a>Click here to like this post</a>
{% endif %}
{% endfor %}

Categories

Resources