I have set of attributes in my Models from which one of the attribute is of Type ManyToMany Field. I am able to access all the Attributes in Template instead one which is ManyToMany Field.
I have tried following in my template
{% for post in all_posts %}
{{ post.likes }}
{% endfor %}
models.py
class Posts(models.Model):
title = models.CharField(max_length=250, blank=False)
content = models.CharField(max_length=15000,
help_text="Write Your thought here...")
creation_time = models.DateTimeField(auto_now_add=True, editable=False)
likes = models.ManyToManyField(User, blank=True, related_name='likes')
views.py
def home(request):
template = loader.get_template('home.html')
all_posts = Posts.objects.all()
context = {
'all_posts': all_posts,
}
return HttpResponse(template.render(context, request))
When i Use {{ post.likes }} what renders on page is auth.User.None
You will have to traverse over all the likes for the selected post
Try something like this:
{% for post in all_posts %}
{% for like in post.likes.all %}
{{ like }}
{% endfor %}
{% endfor %}
Related
I have a Blog model that looks like this:
class Blog(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blogs')
parent = models.CharField(max_length=50, choices=PARENT_TUTORIALS)
def get_absolute_url(self):
return reverse("blog_list", args=[str(self.parent), str(self.slug)])
I can succesfully display all the blogs on a table of contents via my table.html template:
{% for blog in blog_list %}
<li>{{blog.title}}</li>
{% endfor %}
However, I want to show only those blogs that have the same Blog.parent value as the current blog page. For example, the page example.com/biology/page1, has biology as a parent. When the user is on that page, the table of contents should show only the pages that have biology as a parent.
Why not just add an if statement like so?
template.html
{% for blog in blog_list %}
{% if blog.parent == current_blog.parent %}
<li>{{blog.title}}</li>
{% endif %}
{% endfor %}
Another option is to filter with js, something like (untested):
template.html
{% for blog in blog_list %}
<li class="blog-list-item {{blog.parent}}">
{{blog.title}}
</li>
{% endfor %}
script.js
$('.blog-list-item').filter(':not(.biology)').hide();
I have these two models and as you can see they have a relationship.
class Post(models.Model):
text = models.TextField()
class PostImage(models.Model):
post = models.ForeignKey(Post, default=None, on_delete=models.CASCADE)
image = models.FileField(upload_to = 'media/',blank=True, null=True)
As far as I understand if I query posts and push them to a template and post, I would expect to use something like this in my templates to retrieve the images URL attached to the posts but it doesn't seem to work.
{% for post in posts %}
{% for post_image in post.post_image_set.all %}
{{post_image.image.url}}
{% endfor %}
{% endfor %}
What am I doing wrong?
Here is my views.py file.
views.py
# Create your views here.
def index(request):
posts=Post.objects.filter(approved=True).order_by('-published_date')
context = {"posts":posts}
return render(request, 'post/home.html',context)
The default related name for a foreign key relational is the name of the model (PostImage) but in your template for loop you called it post_image Following relationships “backward”
change
{% for post_image in post.post_image_set.all %}
into
{% for post_image in post.postimage_set.all %}
Template code (with change)
{% for post in posts %}
{% for post_image in post.postimage_set.all %}
{{post_image.image.url}}
{% endfor %}
{% endfor %}
I started learning django few days ago and trying to build my first blog.
My problem is that I decided to add an extra field for my categories (subheading), which I want to be in my template, but can't understand how to do it.
my models.py
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=20)
subheading = models.CharField(max_length=160)
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
link = models.TextField()
categories = models.ManyToManyField("Category", related_name="posts")
def __str__(self):
return self.title
views.py
from django.shortcuts import render
from blog.models import Post, Category
def blog_category(request, category):
posts = Post.objects.filter(
categories__name__contains=category
).order_by(
'title'
)
context = {
"category": category,
"posts": posts
}
return render(request, "blog_category.html", context)
The only way category.name or category.subheading are displayed in template (by the teacher) is inside {% for post in posts %} {% endfor %}:
{% for post in posts %}
{% for category in post.categories.all %}
{{ category.subheading }}
{% endfor %}
{% endfor %}
In this case, if there are 10 posts on category page, subheading repeats 10 times. I only need to print 1 to describe category.
Is there a way to call category.subheading outside of {% for post in posts %} ? Or somehow to print only one result.
p.s. sorry for my primitive English level.
You can do this with a Prefetch object [Django-doc]:
from django.db.models import Prefetch
def blog_category(request, category):
posts = Post.objects.filter(
categories__name__contains=category
).prefetch_related(
Prefetch(
'categories',
Category.objects.filter(name__contains=category)
to_attr='relevant_categories'
)
).order_by(
'title'
)
# …
In your template, you can then render this with:
{% for post in posts %}
{% for category in post.relevant_categories %}
{{ category.subheading }}
{% endfor %}
{% endfor %}
Not sure to understand what you want to do but you can search and access to Category elements by doing something like that:
categories=Category.objects.filter(name='NameYouWanttoSearch').values_list('subheading')
can add a model manager to the categories , take single instance and call it in templates instead of all.
class CategoryManager(models.Manager):
def single_category(self):
return self.get_queryset()[:1]
class Category(models.Model):
name = models.CharField(max_length=20)
subheading = models.CharField(max_length=160)
objects = CategoryManager()
def __str__(self):
return self.name
and in templates
{% for post in posts %}
{% for category in post.categories.single_category %}
{{ category.subheading }}
{% endfor %}
{% endfor %}
Hello i am struggling already since 2 days to get a reverse relation going.
I am trying to get pictures of "bilder" into related "doku".
The site returns the {{doku.name}} but nothing from the loop from the related model "bilder"
I tried almost everything i have found on the internet, but maybe I just have overseen something?
here is my models.py:
class Doku(models.Model):
name = models.CharField(max_length=1000)
inhalt = models.TextField(blank=True, null=True)
erstellungsdatum = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name = "Dokumentation"
verbose_name_plural = "Dokumentationen"
def __str__(self):
return self.name
class Bilder(models.Model):
doku = models.ForeignKey(Doku, on_delete=models.CASCADE)
name = models.CharField(max_length=1000)
bilder = models.ImageField(upload_to="bilder/doku/")
def __str__(self):
return self.name
my views.py
#login_required(login_url='login')
def Dokus(request):
doku = Doku.objects.all()
context = {'doku':doku}
return render(request, "doku/index.html", context)
and my template:
{% for doku in doku %}
{{ doku.name }}
{{ doku.bilder.name }}
{% for bilder in doku.bilder_set.all %}
<img src="{{ bilder.bilder.url }}">
<p>{{ bilder.name }}</p>
{% endfor %}
{% endfor %}
EDIT: here's my urls.py:
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
app_name = "apps"
urlpatterns = [
path('doku/', views.Dokus, name="Doku"),
path('doku/create', views.DokuCreate.as_view(), name="DokuCreate"),
path('doku/detail/<int:pk>', views.DokuDetail.as_view(), name="DokuDetail"),
path('doku/update/<int:pk>', views.DokuUpdate.as_view(), name="DokuUpdate"),
path('doku/delete/<int:pk>', views.DokuDelete.as_view(), name="DokuDelete"),
path('doku/picadd', views.BilderCreate.as_view(), name="DokuBilderCreate"),
]
{{ doku.bilder.name }} isn't going to work. Remove that.
Similarly, {{dokubilder.dokubilder.url}} isn't going to work because there is no dokubilder in your template context.
For the models you have posted, looping through {% for bilder in doku.bilder_set.all %} in your template should work. If it doesn't, you'll need to provide more information about how to reproduce the problem.
I would suggest that you use different variable names for the instance and the queryset, e.g. {% for doku in docku_list %}. If you change it to docku_list, remember to update your view.
{% for doku in dokus %}
{{ doku.name }}
{% for bilder in doku.bilder_set.all %}
<p>{{ bilder.name }}</p>
{% endfor %}
{% endfor %}
Suppose I have the following model:
class Holder(models.Model):
other_field = models.BooleanField(default=True)
object_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
object = GenericForeignKey('object_type', 'object_id')
Then in views.py, I have created a formset for Holder, and passed it to the template:
formset = modelformset_factory(Holder, fields =('other_field',))
data = {'formset': formset}
In template.html, I want to access one field of the related object. I have tried with:
{% for form in formset %}
{{ form.object.related_field }}
{{ form.other_field }}
{% endfor %}
Then, other_field is displayed but related_field is not. How could I display the value in related_field in the template?
Ok, I finally solved it by using the formset instance:
{% for form in formset %}
{{ form.instance.object.related_field }}
{{ form.other_field }}
{% endfor %}