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 %}
Related
models.py ( considering all required data ) how to get only logged in customer but it is showing all Customer
class Customer(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.TextField(max_length=50)
phone = models.CharField(max_length=15)
email = models.EmailField(max_length=100 ,default='')
password = models.CharField(max_length=500)
views.py ( considering all required done )
def profile(request):
data= Customer.objects.all()
return render(request,'profile.html' ,{'data':data})
profile.html ( considering making urls) and all doing requirement
{% if data %}
{% for d in data %}
<h2>{{ d.email }}</h2>
{% endfor %}
{% endif %}
data= Customer.objects.all()
data is a list of Customers not a single Customer, so there is no email attribute available. Calling data.email in the template returns None.
You need to loop through data in the template.
{% if data %}
{% for d in data %}
<h2>{{ d.email }}</h2>
{% endfor %}
{% endif %}
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>
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}}
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)
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.