Django pass family in template inside for cycle - python

In a simple view I pass a family in template like this:
def page(request):
family= Author.objects.all()
return render(request, "myapp/page.html", {'family':family})
and I render in template like this:
{% for item in family %}
{{ item.pk }}
{% endfor %}
But, if I put my family inside a for cycle; for example:
def page(request):
family = []
for i in range(5):
family= Author.objects.filter(name='John')[i]
return render(request, "myapp/page.html", {'family':family})
it not render anything in template...
Any idea?
EDIT 1
I have more users in my app, every user has different blog and every blog has different post...
So when user is logged i need to show his blog and for every blog show last 5 post.
I do:
#login_required
def page(request):
user = request.user.id
numblog = Blog.objects.filter(user_id=user).aggregate(c=Count('id'))
for i in range(numblog['c']):
blogquery = Blog.objects.filter(user_id=user)[i]
postquery = Post.objects.filter(blog_id=blogquery.pk)[:5]
return render(request, "myapp/page.html", {'blogquery ':blogquery,'postquery ':postquery })
expected result in template:
{% for b in blogquery %}
{{ b.name }} ### here name of blog
{% for p in postquery %}
{% if p.blog_id == b.pk %}
{{ p.content }} ### here last 5 post of THAT blog
{% endif %}
{% endfor %}
{% endfor %}
EDIT 2
In a view, if I print result it work but not render in template
#login_required
def page(request):
user = request.user.id
numblog = Blog.objects.filter(user_id=user).aggregate(c=Count('id'))
for i in range(numblog['c']):
blogquery = Blog.objects.filter(user_id=user)[i]
postquery = Post.objects.filter(blog_id=blogquery.pk)[:5]
for p in postquery:
print (blogquery.pk, p.pk)
return render(request, "myapp/page.html", {'blogquery ':blogquery,'postquery ':postquery })

It is surprising how you don't understand that repeatedly assigning to the same variable within a loop will just give you the last value.
But nevertheless, you don't need any of this code. You should just follow the relationship in the template.
#login_required
def page(request):
blogs = Blog.objects.filter(user=user).prefetch_related('post_set')
return render(request, "myapp/page.html", {'blogs ': blogs })
{% for blog in blogs %}
{{ blog.name }}
{% for post in blog.post_set.all|slice:"5" %}
{{ post.content }}
{% endfor %}
{% endfor %}
(You haven't shown your models so I presume the related_name from Blog to Post is called post_set, change as necessary.

UPDATE
That's not correct. You don't need to use for loop. If you need to get the last 5 rows you can do this i.e.:
def page(request):
family= Author.objects.all().order_by('-pk')[:5]
return render(request, "myapp/page.html", {'family':family})
another approach is to limit the results in your template:
{% for item in family %}
{% if forloop.counter < 6 %}
{{ item.pk }}
{% endif %}
{% endfor %}

Related

Adding a maximum limit to the number of post using python

I need to limit the number of posts in Django queries. I have tried to add a min and max but nothing seemed to have worked. I have added home.html into the code.
Example: I should only have the 15 most recent posts in my blog. The rest can be seen by clicking on the category button.
Home.html:
{% extends 'base.html' %}
{% block content %}
<h1>Posts</h1>
<ul>
{% for post in object_list %}
<li>{{post.title}}
<style>
a {
text-transform: capitalize;
}
</style>
- {{ post.category }} - <a href="{% url 'show_profile_page' post.author.profile.id %}">{{ post.author.first_name }}
{{ post.author.last_name }}</a> - {{ post.post_date }} <small>
{% if user.is_authenticated %}
{% if user.id == post.author.id %}
- (Edit)
(Delete)
{% elif user.id == 1 %}
- (Edit)
(Delete)
{% endif %}
{% endif %}
</small><br/>
{{ post.snippet }}</li>
{% endfor %}
</ul>
{% endblock %}
view.py:
class HomeView(ListView):
model = Post
template_name = 'home.html'
ordering = ['-id']
def get_context_data(self, *args, **kwargs):
cat_menu = Category.objects.all()
context = super(HomeView, self).get_context_data(*args,**kwargs)
context["cat_menu"] = cat_menu
return context
models.py:
class Post(models.Model):
title = models.CharField(max_length=255)
header_image = models.ImageField(null=True, blank=True, upload_to='images/')
title_tag = models.CharField(max_length=255)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = RichTextField(blank=True, null=True)
post_date = models.DateField(auto_now_add=True)
category = models.CharField(max_length=255, default='intro')
snippet = models.CharField(max_length=255)
likes = models.ManyToManyField(User, related_name='post_likes')
dislikes = models.ManyToManyField(User, related_name='post_dislikes')
I think you have another template for displaying categorised objects when you click category button. As you said
"I should only have the 15 most recent posts in my blog. The rest can
be seen by clicking on the category button."
In this case you can use a simple hack to display most recent posts from your table.
query all objects in descending order in views
all_objs = Post.objects.all().order_by('-id')
Then use {% if forloop.counter <= 15 %} to display last 15 items only. as follow.
templates
{% for post in object_list %}
{% if forloop.counter <= 15 %}
<h4>{{obj}} #or anything really that is meant to be shown on the home page.</h4>
{% endif %}
{% endfor %}
You can do something like this:
def get_context_data(self, *args, **kwargs):
context = super(HomeView, self).get_context_data(*args,**kwargs)
context["cat_menu"] = Category.objects.all()
context["most_recent_posts"] = Post.objects.filter(author=self.request.user).order_by('-post_date')[:15]
return context
This will get the 15 most recent posts authored by the current user, ordered by the date it was posted.
Then just handle displaying this in home.html for example:
<ul>
{% for p in most_recent_posts %}
<li>{{ p.title }}</li>
{% endfor %}
</ul>
Just limit your query to the latest 15 entries sorted by post_date:
cat_menu = Category.objects.latest("post_date")[:15]
https://docs.djangoproject.com/en/3.2/topics/pagination/
The best way is Django Pagintion.
{% for contact in page_obj %}
{# Each "contact" is a Contact model object. #}
{{ contact.full_name|upper }}<br>
...
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
« first
previous
{% endif %}
<span class="current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
next
last »
{% endif %}
</span>
</div>
from django.core.paginator import Paginator
from django.shortcuts import render
from myapp.models import Contact
def listing(request):
contact_list = Contact.objects.all()
paginator = Paginator(contact_list, 25) # Show 25 contacts per page.
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
return render(request, 'list.html', {'page_obj': page_obj})
you can use Django pagination api . Manage your data through page number. Initially pass 1 and after that page number given by pagination.
paginator = Paginator(yourquerysetdata, 20)
page_num = request.data.get('page')
result = yourSerializerName(paginator.get_page(page_num) many=True).data
try:
page = paginator.page(page_num)
except:
page = paginator.page(1)
count = paginator.num_pages
resultobj = paginator.get_page(page_num)
has_prev = resultobj.has_previous()
has_next = resultobj.has_next()
page_range = resultobj.paginator.page_range.stop - 1
if has_prev:
prev_page_no = resultobj.previous_page_number()
else:
prev_page_no = 0
if has_next:
next_page_no = resultobj.next_page_number()
else:
next_page_no = None
context = dict()
context['page'] = page.number
context['page_no'] = count
It is very simple. You just have to modify the query that you are using to fetch the posts.
In the get_context_data() method, replace cat_menu = Category.objects.all() with cat_menu = Category.objects.all().order_by('-post_date')[:15]. This will limit the number of results to 15 most recent objects.
For more understanding, you can take a look at the official Django docs for Limiting QuerySets.

Django disable a link if list returns empty

I don't want to display a link if the list returns empty.
template.html
{% for item in cart %}
<h1>{{ item.product.product_title }}</h1>
Remove item
{% empty %}
<p>No items in cart</p>
{% endfor %}
{% if item is not None %}
<p>
Checkout
</p>
{% endif %}
views.py
def cartview(request):
if request.user.is_authenticated():
cart = Cart.objects.filter(user=request.user.id, active=True)
orders = ProductOrder.objects.filter(cart=cart)
#total = 0
count = 0
for order in orders:)
count += order.quantity
context = {
'cart': orders,
'count': count,
}
return render(request, 'store/cart.html', context)
else:
return redirect('index:index')
I want to hide checkout link if the cart list is empty. putting it in the for loop would make the link appear many times. I want to display checkout button only once.
Instead of 'item' check for 'cart' in the template.
{% if cart %}
<p>
Checkout
</p>
{% endif %}

Django views is it possible to select only the first four elements for a specific model.item?

I have a profile model that is displaying various items.
One of those is the country attached to the profile.
Here is what happen in the view:
class ProfilePartnerListView(FormMixin, BaseProfilePartnerView, ListView):
model = ProfilePartner
context_object_name = 'profile_list'
view_url_name = 'djangocms_partner_profile:profile-list'
def get(self, request, *args, **kwargs):
context = {}
self.object_list = self.get_queryset().order_by('-date_created')
context.update(self.get_context_data(**kwargs))
context[self.context_object_name] = context['object_list']
country_for_articles = Country.objects.exclude(regions_partner_profile=None).order_by('name')
industries_qs = ProfilePartnerIndustry.objects.active_translations(
get_language()).order_by('translations__name')
budget_qs = ProfilePartner.objects.values_list('budget',
flat=True).distinct()
context['load_more_url'] = self.get_load_more_url(request, context)
context['regions_list'] = country_for_articles
context['industry_list'] = industries_qs
context['budget_list'] = budget_qs
return self.render_to_response(context)
I know, for example 'regions_list', how to return only 4 elements from it.
But the thing is, my main object 'profile_list' that I use in the template for the rendering, is displaying all the country of the item when I do:
{% for profile in profile_list %}
{% for country in profile.regions.all %}
<div class="col-xs-12">{{ country }}</div>
{% endfor %}
{% endfor %}
And some of the profiles got 5 or 6 country. I want to only display the first 4.
Is there a way of doing it?
Many thanks!
ps: region_list, industry_list and budget_list are use for categories, it has nothing to do with what I want here.
You could use slice filter for this:
{% for profile in profile_list %}
{% for country in profile.regions.all|slice:":4" %}
<div class="col-xs-12">{{ country }}</div>
{% endfor %}
{% endfor %}

recover the questions related to responses retrieve to ManyToMany

First my model contains questions and answers pages to manage issues.
My models.py
class Question(models.Model):
label = models.CharField(max_length=30)
def __str__(self):
return self.label
class Page(models.Model):
title = models.CharField(max_length=30)
def __str__(self):
return self.title
class Reply(models.Model):
page = models.ManyToManyField(Page)
question = models.ForeignKey(Question)
user = models.ForeignKey(Personne)
answer = models.CharField(max_length=30)
creationDate = models.DateTimeField(default=datetime.now())
def __str__(self):
return str(self.answer)
So I managed to retrieve the answers for each page
One page is equal to: 1 visit
another: Visit 2 etc ... (i go share a screenshot)
I managed to retrieve the answers for each pages but I fail to see the questions corresponding to each response for a page!
This is my views.py
def reply(request):
replies = Reply.objects.all()
questions = Question.objects.all()
logged_user = get_logged_user_from_request(request)
pages = Page.objects.all()
form = ReplyForm(request.GET)
personnes = Personne.objects.all()
if logged_user:
if len(request.GET) > 0:
form = ReplyForm(request.GET)
if form.is_valid():
form.save(commit=True)
return HttpResponseRedirect('/reply')
else:
return render_to_response('polls/reply.html', {'personnes': personnes, 'replies': replies, 'questions': questions,'pages':pages, 'form': form})
else:
form = ReplyForm()
return render_to_response('polls/reply.html', {'personnes':personnes, 'replies': replies, 'questions': questions, 'pages':pages, 'form': form})
else:
return HttpResponseRedirect('/login')
and for finish this is my template reply.html
{% for reply in replies %}<br>
<br>{{ reply.user }}
{{ reply.question }} -
{{ reply.answer }}
(dans la :{% for page in reply.page.all %} {{ page }}) {% endfor %}
{% endfor %}
<hr>
{% for page in pages %}<hr>
{{ page }}:
<br>{{ page.reply.question_set all }} : {{ page.reply_set.all }}
{% endfor %}
How is it possible to retrieve the issues here?
I manage to recover the questions for every answer but not for every visit
The questions will not be displayed for each response in each visit
What is the call in the template to do this?
or then add a line in views.py ?
** Edit :**
edit : add a screen shot of the result right now
template :
{% for reply in replies %}<br>
<br>{{ reply.user }}
{{ reply.question }} -
{{ reply.answer }}
(dans la :{% for page in reply.page.all %} {{ page }}) {% endfor %}
{% endfor %}
<hr>
{% for page in pages %}<hr>
{{ page }}:
<br>{% for reply in page.reply_set.all %}<br> {{ reply.question }} (Author : {{ reply.user }}) {% endfor %}
{% endfor %}
{% for reply in replies %}<br>
<br>{{ reply.user }}
{{ reply.question }} -
{{ reply.answer }}
(dans la :{% for page in reply.page.all %} {{ page }}) {% endfor %}
{% endfor %}
<hr>
{% for page in pages %}<hr>
{{ page }}:
<br>{% for each_reply in page.reply_set.all }}{{each_reply.question}}{% endfor %} : {{ page.reply_set.all }}
{% endfor %}

How to use multiple forms with a Form Wizard form in the same template (Django)

I'm using multiple forms in the same template and they all work until I add my Form Wizard, when it becomes that either the FormWizard - form works or the rest of the forms works but not both simultaniousely.
When I have the URL's with postid (?P\d+) -url placed prior to the ContactWizard.as_view -urls the forms in the view - function one_labeling are displayed but not the Form Wizard/ContactWizard.as_view in views.py class ContactWizard(SessionWizardView)
url(r'^label$', LabelingIndex),
url(r'^label(?P<postID>\d+)$',one_labeling),# <----- here
url(r'^label',ContactWizard.as_view([Form1, Form2, Form3])),
url(r'^label(?P<one_labeling>\d+)/$', 'formwizard.views.one_labeling'),
and vice versa, when the URL's for the Form Wizard is placed before the postID - url for the forms in the view function one_labeling then the FormWizard is displayed (and works) but the other forms aren't displayed/evoked.
url(r'^label$', LabelingIndex),
url(r'^label',ContactWizard.as_view([Form1,Form2, Form3])),
url(r'^label(?P<one_labeling>\d+)/$', 'formwizard.views.one_labeling'),
url(r'^label(?P<postID>\d+)$',one_labeling), #<----- here
I'm not sure on how to prefix the Wizard Form so that I could use it as {{ form9.as_p }} like with {{ form3.as_p }} instead of {{ form1 }}{{ wizard.management_form }} or {{ form }} in the done.html below, so that it would work simultaniousely with the other forms in template one_labeling_index.html.
in template done.html
{% extends 'base_one_labeling.html' %}
{% block content %}
{% for form in form_data %}
{{ form }}
{% endfor %}
{% endblock %}
in views.py
class ContactWizard(SessionWizardView):
template_name = "one_labeling_index.html"
def done(self, form_list, **kwargs):
form_data = process_form_data(form_list)
return render_to_response("done.html",{"form_data":form_data})
def process_form_data(form_list):
form_data = [form.cleaned_data for form in form_list]
logr.debug(form_data[0]['subject'])
logr.debug(form_data[1]['sender'])
logr.debug(form_data[2]['message'])
send_mail(form_data[0]['subject'],form_data[1]['sender'],
form_data[2]['message'], 'xxxx#gmail.com',
fail_silently=False)
return form_data
in views.py,LabelingIndex,function evoking template labeling_index.html
def LabelingIndex(request):
#labelings, objects in class Labeling() in models.py
labelings = Labeling.objects.all()
c ={"labelings":labelings}
c.update(csrf(request))
return render(request,"labeling_index.html", c)
in views.py,one_labeling, views function one_labeling
def one_labeling(request,postID):
#one_labeling, object in class Labeling
one_labeling= Labeling.objects.get(id=postID)
template = one_labeling_index.html
if request.method == "POST":
# forms used in one_labeling_index.html
form = SentenceForm(request.POST, prefix="sentence")
form2 = OpenFileForm(request.POST, prefix="file")
form3 = LabelingForm(request.POST,prefix="form3")
form9 =LabelingForm2(request.POST,prefix="labeling")
if form.is_valid() and 'button1' in request.POST:
# do ....
if form3.is_valid() and 'button2' in request.POST:
post_one_labeling(request.POST, one_labeling)
else:
form = SentenceForm()
form2 = OpenFileForm()
form3 = LabelingForm()
form9 = LabelingRRGForm2()
c = {"one_labeling":one_labeling,"form3":form3,"form":form,"form9":form9...... }
c.update(csrf(request))
return render(request,template,c,context_instance=RequestContext(request))
in template Labeling_index.html
{% for one_labeling in labelings %}
# Link to new id (postid) - page; template one_labeling.html
&nbsp Subject: <a href="/label{{ one_labeling.id }}">{{ one_labeling.title }}<a/><br/>
{% endfor %}
in template one_labeling_index.html
# extending base_one_labeling.html
{% extends "base_one_labeling.html" %}
{% block content %}
# form3
<form3 action="" method="post" enctype="multipart/form-data">{% csrf_token %}
{{ form3.as_p }}
</form3>
# Form Wizard
<p>Label {{ wizard.steps.step1 }} out of {{ wizard.steps.count }} ({{ wizard.steps.step1 }}/{{ wizard.steps.count }})</p>
{% for field in form %}
{{field.error}}
{% endfor %}
<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form1 in wizard.form.forms %}
{{ form1 }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
{% endif %}
<html>
<body>
The line
url(r'^label',ContactWizard.as_view([Form1, Form2, Form3])),
matches any url beginning by "label", so any of your other url you put after are never matched.
Also, except the trailing "/", the lines:
url(r'^label(?P<one_labeling>\d+)/$', 'formwizard.views.one_labeling'),
url(r'^label(?P<postID>\d+)$',one_labeling), #<----- here
match the same thing.
So you have to order carefully your urls, and distinguish them somehow to avoid any ambiguity.

Categories

Resources