how to access to foreign attributes model in django views - python

This is my code:
urls.py
url(r'^blog/$', views.blog, name='blog'),
models.py
class Category(models.Model):
name = models.CharField(max_length=100)
slug = models.CharField(max_length=100)
description = models.CharField(max_length=200)
creation_date = models.DateTimeField(auto_now_add=True)
class Post(models.Model):
Status = ((1, "Publicado"), (2, "Borrador"), (3, "Eliminado"))
status = models.IntegerField(choices=Status, default=3)
title = models.CharField(max_length=100)
slug = models.CharField(max_length=100, default='prueba')
description = models.CharField(max_length=200)
content = tinymce_models.HTMLField()
category = models.ForeignKey(Category)
creation_date = models.DateTimeField(auto_now_add=True)
image = models.ImageField(upload_to="photos", default=1)
autor = models.ForeignKey(User)
views.py
def blog(request):
posts = Post.objects.filter(status=1).values().order_by("-creation_date")
categories = Category.objects.order_by("name")
context = {
"posts":posts,
"categories":categories
}
return render(request, "blog.html", context)
blog.html
{% for post in posts %}
{{ post.title }}
<p>{{ post.description }}</p>
<p>{{ post.category }}</p>
<p>{{ post.autor }}</p>
<p>{{ post.creation_date }}</p>
{% endfor %}
I can't access to the attributes author and category from a post.
Please. I need help for how to do it.
I need fix the problem.
Thank you so much.

Try to do this in your template:
{% for post in posts %}
{{ post.title }}
<p>{{ post.description }}</p>
{% for category in post.category.all %}
<p>{{ category.name }}</p>
{% endfor %}
{% for author in post.author.all %}
<p>{{ author.name }}</p>
{% endfor %}
<p>{{ post.creation_date }}</p>
{% endfor %}
Hope this might help you.

Related

django foreign key table fields in html template

I have two tables(article,comment) are related one to many relashionship using a foreign key. I would have want in the html template list and some fields from the table one article but that I create don't work ,here the code :
models.py
class article(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
last_name = models.CharField(max_length=254)
age = models.CharField(max_length=254)
def __unicode__(self):
return str(self.id)
class comment(models.Model):
field_1 = models.CharField(max_length=100, blank=True, null=True)
field_2 = models.CharField(max_length=254)
field_3 = models.CharField(max_length=254)
field_fk= models.ForeignKey('article', blank=True, null=True)
def __unicode__(self):
return str(self.id)
views.py
def index(request):
apps = article.objects.all()
comments = comment.objects.all()
return render(request, 'index.html', {'apps':apps,'comments':comments})
html template :
{% for b in apps %}
<p>{{ b.field_1 }}</p>
<p>{{ b.field_2 }}</p>
<p>{{ b.field_3 }}</p>
{% for c in b.field_fk.comments %}
<p>{{ c.name }},{{ c.last_name}},{{ c.age}}</p>
{% endfor %}
{% endfor %}
in my example in the template don't show me name , last_name and age is empty the paragraph
You can't access the comments using just .comments. Use modelname_set. In your case it would be comments_set. Your for-loop will look like this:
{% for c in b.field_fk.comment_set.all %}
<p>{{ c.name }},{{ c.last_name}},{{ c.age}}</p>
{% endfor %}
Also, you aren't looping the correct model. apps is set to Article but in your template you are using the Comment fields (field_1, field_2...). The first part should be:
{% for article in apps %}
<p>{{ article.name}}</p>
<p>{{ article.last_name}}</p>
<p>{{ article.age}}</p>
...
Since the article is the main loop you don't to use the foreign key. The loop should use the comment_set directly:
{% for comment in b.comment_set.all %}
<p>{{ comment.field_1 }},{{ comment.field_2 }},{{ comment.field_3}}</p>
{% endfor %}
use this code
views.py :
def index(request):
comments = comment.objects.all()
return render(request, 'index.html', {'comments':comments})
HTML template :
{% for b in comments %}
<p>{{ b.field_1 }}</p>
<p>{{ b.field_2 }}</p>
<p>{{ b.field_3 }}</p>
<p>{{ b.field_fk.name }},{{ b.field_fk.last_name}},{{ b.field_fk.age}}</p>
{% endfor %}

Django - Looping over unique model field in template

I have a model which a choice field, category, which the user must enter when submitting an entry. I would like to create a view in which each category has its own heading (only once), therefore each unique category would have its own heading, and then display the title associated to each category.
models.py
class Position(models.Model):
club_functions = Choices('Corporate Relations', 'Events & Conference', 'Marketing & Operations', 'Software Development', 'Product')
title = models.CharField(max_length=50)
category = models.CharField(choices=club_functions, max_length=30, blank=False)
description = models.TextField(blank=True)
spec_q1 = models.CharField(max_length=500)
spec_q2 = models.CharField(max_length=500)
views.py
def position_list_view(request):
all_objects = Position.objects.all()
context = {
'object_list' : all_objects
}
return render(request, "exec_list.html", context)
exec_list.html
{% for object.category in object_list %}
<h3>{{ object.category }}</h3>
<p>{{ object.title }}</p>
{% endfor %}
Any ideas on how to do this?
you can use regroup
{% regroup object_list by category as category_list %}
<ul>
{% for category in category_list %}
<li>{{ category.grouper }}
<ul>
{% for position in category.list %}
<li>{{ position.title }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>

display contents of manytomany and foreignjey fields in Django

I'm new to Django and I'ma building a basic blog application.
I cant show manytomany field (in tags) and a foreignkey field (comments) in my details page.
models.py
class BlogContent(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=200)
content = models.TextField()
date_published = models.DateField(auto_now=True)
image = models.ImageField(upload_to='media/')
def __str__(self):
return self.title
class TagName(models.Model):
tag = models.ManyToManyField(BlogContent, null=True)
name = models.CharField(max_length=100, blank=True, null=True)
def __str__(self):
return self.name
class Comment(models.Model):
comt_text = models.TextField()
comments = models.ForeignKey(BlogContent, on_delete=models.CASCADE)
date_published = models.DateField(auto_now=True)
name = models.CharField(max_length=200, blank=True, null=True)
def __str__(self):
return self.name
views.py
def details(request, blogcontent_id):
data_blog = get_object_or_404(BlogContent, pk=blogcontent_id)
data_tag = get_object_or_404(TagName, pk=blogcontent_id)
data_comment = Comment.objects.select_related()
return render(request, 'details.html',
{'data_blog': data_blog, 'data_tag':data_tag, 'data_comment':data_comment})
details.html
{% extends 'base.html' %}
{% block body_base %}
<img class="card-img-top img-responsive" src={{ data_blog.image.url }} alt="Card image cap">
<h2 class="blog-post-title">{{ data_blog.title }}</h2>
<p class="blog-post-meta">{{ data_blog.date_published }} {{ data_blog.author }}</p>
<p>{{ data_blog.content }}</p>
{% endblock %}
how do i show foreignkey and manaytomany fieds after this?
TBH this is much easier if you use class based views.
The view would simply be:
class BlogContentDetail (DetailView):
model = BlogContent
The url call would be url(r'^blog-detail/(?P<pk>\d+)/$, BlogContentDetail.as_view(), name="blog_detail")
Your html file should be called blogcontent_detail.html and held within the app subfolder in the templates folder
The template would then be:
{% extends 'base.html' %}
{% block body_base %}
<img class="card-img-top img-responsive" src={{ object.image.url }} alt="Card image cap">
<h2 class="blog-post-title">{{ object.title }}</h2>
<p class="blog-post-meta">{{ object.date_published }} {{ object.author }}</p>
<p>{{ object.content }}</p>
{% for tag in object.tags_set.all %}{{ tag }}{% endfor %}
{% endblock %}
You can iterate the ManyToMany Field in this way
{% for tags in data_tag.tag.all %}
<p > {{tags}} </ p>
{% endfor %}
For foreign key
{{data_comment.comments}}

Django: Showing "featured" items?

I'm having a little conundrum with sorting some items. I have a field called featured thats a boolean. I'm trying to display the featured coins first and then the remaining will be sorted by a different metric. In this code im using pub_date.
However, when I put an if statement in my template for the featured items it's still showing those that are set to false as well. I'll post code below.
index.html loops and if's
{% if featured_coins_list %}
{% for coin in featured_coins_list %}
<div class="large-6 medium-6 cell">
<h2>{{ coin.name }}</h2>
<p>Ticker: {{ coin.ticker }}</p>
<p>{{ coin.summary }}</p>
More Info</strong>
</div>
{% endfor %}
{% endif %}
{% if latest_coins_list %}
{% for coin in latest_coins_list %}
<div class="large-6 medium-6 cell">
<h2>{{ coin.name }}</h2>
<p>Ticker: {{ coin.ticker }}</p>
<p>{{ coin.summary }}</p>
More Info</strong>
</div>
{% endfor %}
</div>
{% else %}
<p>No coins are available.</p>
{% endif %}
views.py for index
def index(request):
featured_coins_list = Coin.objects.order_by('-featured')[:4]
latest_coins_list = Coin.objects.order_by('-pub_date')[:8]
context = {'featured_coins_list': featured_coins_list,
'latest_coins_list': latest_coins_list}
return render(request, 'coins/index.html', context)
models.py
class Coin(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
ticker = models.CharField(max_length=5)
featured = models.BooleanField(default=False)
logo = models.ImageField(upload_to='uploads/', verbose_name='image')
website = models.URLField(max_length=200, default="https://example.com/")
reddit = models.URLField(max_length=200, default="https://reddit.com/r/")
twitter = models.URLField(max_length=200, default="https://twitter.com/")
summary = models.CharField(max_length=500, blank=True)
description = models.TextField()
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.ticker
def is_featured(self):
return self.featured
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
How should I go about listing the featured coins that are set to true first and then display the remaining items after?
You need to filter for featured=True in your queryset.
def index(request):
featured_coins_list = Coin.objects.filter(featured=True).order_by('-featured')[:4]
latest_coins_list = Coin.objects.order_by('-pub_date')[:8]
context = {'featured_coins_list': featured_coins_list,
'latest_coins_list': latest_coins_list}
return render(request, 'coins/index.html', context)

how to filter story by it's belonging tag

I have two models news and category, and in news I have foreignkey of category. I know how to display news with same category in a single template. but furthermore, in my home page I'm trying to display featured news of each category. this is where I'm having problem.
this is my models.py
class News(models.Model):
title = models.CharField(max_length=120)
content = models.TextField()
category = models.ForeignKey("Tag")
active = models.BooleanField(default=True)
featured = models.BooleanField(default=False)
top = models.BooleanField(default=False)
slug = models.CharField(max_length=255, unique=True)
featuredInCat = models.BooleanField(default=False)
objects = StoryManager()
class NewsQueryset(models.query.QuerySet):
def active(self):
return self.filter(active=True)
def featuredInCat(self):
return self.filter(featuredInCat=True)
class NewsManager(models.Manager):
def get_queryset(self):
return NewsQueryset(self.model, using=self._db)
def get_featuredInCat(self):
return self.get_queryset().active().featuredInCat()
def all(self):
return self.get_queryset().active()
class Category(models.Model):
title = models.CharField(max_length=120)
description = models.TextField(max_length=5000, null=True, blank=True)
In views.py
def category_list(request):
categoryList = NewsCategory.objects.all()
featuredInCat = News.objects.get_featuredInCat()
context = {
"featuredInCat":featuredInCat
"categoryList":categoryList,
}
return render(request,"news/category_list.html", context)
In my template
{% for category in categoryList %}
<div class='col-sm-4'>
<div id="container">{{category.title}}</h1>
<ul>
{% for x in featuredInCat %}
{{x.title}}</li>
{% endfor %}
</ul>
</div>
<hr>
</div>
{% endfor %}
then this shows the featuredInCat in every category where featuredInCat should be shown only in its Category section.
how do I fix this?
Take a look at the built-in regroup template tag of django. You will need to change your template to something like this:
{% regroup featuredInCat by category as news_list %}
<ul>
{% for news in news_list %}
<li>{{ news.grouper.title }}
<ul>
{% for item in news.list %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
You can change your for loop to iterate over the correct objects
{% for x in category.news_set.get_featuredInCat %}
You won't need the context variable anymore

Categories

Resources