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 %}
Related
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 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 %}
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 %}
So far my blog project has :
Models.py
class Category(models.Model):
title = models.CharField(max_length=200)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = "categories"
class Post(models.Model):
title = models.CharField(max_length=200)
summary = models.CharField(max_length=500, default = True)
body = models.TextField()
pub_date = models.DateTimeField(default=timezone.now)
categories = models.ForeignKey('Category', default=True)
def __str__(self):
return self.title
Views
from django.shortcuts import render
from .models import Post, Category
def getAllCategories(request):
categories = Category.objects.all()
context = {
'categories':categories,
}
return render(request, 'categories/getAllCategories.html', context)
Template
{% extends "../posts/base.html" %}
{% block content %}
<div>
{% for category in categories %}
<div>
<h3>{{ category.title }}</h3>
<ul>
{% for post in category.post_set.all %}
<li><a href=#>{{post.title}}</a></li>
- {{post.summary}}
{% endfor %}
</ul>
</div>
{% endfor %}
</div>
{% endblock %}
This pulls through all of my posts per category fine - however I'm stuck as I would like to only pull through the latest three posts per category. I understand that in a simple view you can use order_by()[:3], but then you can't use post_set with that. So I need a way of filtering by top three posts within a category, so using the many-to-many relationship. Hope this is clear.
Many thanks
I am using django-categories to implement music related app. I want artist as my category and his/her songs as children
models.py
from django.db import models
from django_extensions.db.fields import AutoSlugField
from categories.models import CategoryBase
class Artist(CategoryBase):
cat = models.CharField(max_length=255, blank=True)
def __unicode__(self):
return self.name
class Song(models.Model):
title = models.CharField(max_length=255,)
slug = AutoSlugField(populate_from='title', unique=True)
description = models.TextField()
cat = models.ForeignKey(Artist, blank=True)
def __unicode__(self):
return self.title
In templates, artist_details.html
{% extends 'base_post.html' %}
{% load category_tags %}
{% block page_content %}
<h1>{{ artist.name }}</h1>
{% if artist.children.count %}
<h2>Subcategories</h2>
<ul>
{% for child in artist.children.all %}
<li>{{ child }}</li>
{% endfor %}
</ul>
{% endif %}
The template is getting rendered coz I can see artist's name. But i am unable to fetch the children. I checked the docs but I could not find much stuff related to fetching children.
There is data for both models in my DB, I added relevant info via admin interface. Can anyone tell me what I am missing ?
Also I open to using better packages. You can give any suggestions that implements categories.
SOLUTION: From django docs https://docs.djangoproject.com/en/1.6/topics/templates/#accessing-method-calls
thanks mariodev
Even using django-categories, you can't have songs as children of artists. Artists just do not form a category.
What you instead want is something like this:
from django.db import models
from django_extensions.db.fields import AutoSlugField
from categories.models import CategoryBase
class MusicCategory(CategoryBase):
# add extra fields, like images, "featured" and such here
pass
class Artist(CategoryBase):
name = CharField(max_length=255,)
categories = models.ManyToManyField(MusicCategory, related_name="artists")
def __unicode__(self):
return self.name
class Song(models.Model):
slug = AutoSlugField(populate_from='title', unique=True)
title = models.CharField(max_length=255,)
artist = models.ForeignKey(Artist, related_name="songs", on_delete=models.PROTECT)
categories = models.ManyToManyField(MusicCategory, related_name="songs")
description = models.TextField()
def __unicode__(self):
return self.title
and with some templates
{% extends 'base_post.html' %}
{% load category_tags %}
{% block page_content %}
<h1>{{ artist.name }}</h1>
{% if artist.songs.all.exists %}
<h2>Songs</h2>
<ul>
{% for song in artist.songs.all %}
<li>{{ song }}</li>
{% endfor %}
</ul>
{% endif %}
REF: https://django-categories.readthedocs.org/en/latest/custom_categories.html#creating-custom-categories