For some reason when checking to see if BooleanField post.featured is true I get no output. If I remove that it works fine but not as I intend.
<div class="carousel-inner">
{% for post in object_list%}
{% if post.featured is True %}<!-- This post.featured is BooleanField -->
{% if forloop.first %}
<div class="carousel-item active">
{% else %}
<div class="carousel-item">
{% endif %}
<div class="col-md-6 px-0">
<h1 class="display-4 font-italic">{{ post.title }}</h1>
<p class="lead my-3">{{ post.hook }}</p>
<p class="lead mb-0">
<a href="{% url 'article-details' post.pk %}" class="text-white fw-bold">
Continue reading...
</a>
</p>
</div>
</div>
{% endif %}<!-- and this -->
{% endfor %}
</div>
Heres how it looks like when not checking if post.featured == true:
However, content doesnt render with {% if post.featured is True %} or {% if post.featured %}
Can someone please explain what im doing wrong
EDIT:
Submiting my view:
class Home(ListView):
model = Post
template_name = 'home.html'
You should not filter in the template. This is not only inefficient, but a template is not meant to implement business logic, only rendering logic: a template should not be concerned with what it renders, it should only be concerned with rendering the data in a pleasant way.
You should filter in the ListView:
class Home(ListView):
model = Post
template_name = 'home.html'
queryset = Post.objects.filter(featured=True)
This will filter at the database side.
If you need both the featured items, and the ones that are not featured, you can make two queries:
class Home(ListView):
model = Post
template_name = 'home.html'
queryset = Post.objects.filter(featured=True)
def non_featured(self):
return Post.objects.filter(featured=False)
then you can render the non-featured items with:
{% for post in view.non_featured %}
…
{% endfor %}
Related
I have searching on the internet but I did not find a good answer, I would like to know if is possible to create multiple pagination with ListView, like I changed my context to return two list of different objects and then I will put each in a row and each row will have a paginatior for that list.
How should I proceed to achieve this ?
ListView
class IndexView(TemplateView):
template_name = "index.html"
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['oportunities_idea'] = Oportunity.objects.filter(goal_oportunity_idea=True)
context['oportunities_project'] = Oportunity.objects.filter(goal_oportunity_project=True)
return context
index.html
<div class="row mt15"><!--project-->
<div id="home" class="tab-pane row fade in active">
<legend>Projects</legend>
{% for project in oportunities_project %}
<h2>{{project.title}}<h2>
{% if forloop.counter|divisibleby:1 %}
</li>
<li>
{% endif %}
{% endfor %}
</div>
</div>
<div class="row mt15">
<div id="home" class="tab-pane row fade in active">
<legend>Ideas</legend>
{% for idea in oportunities_idea %}
<h2>{{idea.title}}<h2>
{% if forloop.counter|divisibleby:1 %}
</li>
<li>
{% endif %}
{% endfor %}
</div>
</div>
As you can see the help text is not being rendered as UL instead its just plain text
Here is my code
Forms.py:
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = CustomUser
now = datetime.datetime.now()
fields = ('username', 'email', 'gender', 'security_question', 'answer', 'birth_date', 'resume')
widgets={
'birth_date' :DatePickerInput(
options={
'maxDate':str(datetime.datetime.now()),
}
)
}
Views.py:
class SignUp(generic.CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('login')
template_name = 'users/signup.html'
signup.html:
{% extends 'base.html' %}
{% block title %}Sign Up{% endblock %}
{% block content %}
<div class="login-page">
<h1>Sign up</h1>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<!--
{{ form.as_p }}
-->
<div class="form">
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="color: grey">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<button type="submit">Sign up</button>
</div>
</form>
</div>
{% endblock %}
Can someone help me figure out how do i Fix the issue ? i am using Django2.0.6
help_text is allowed to contain HTML - but you are rendering it as a safe string - you need to use the safe template filter to allow the HTML to be rendered.
You are also rendering it inside a <small> tag which will result in invalid HTML if the help text contains a list, as it does in this case.
I'd suggest you consider refactoring your template do handle this - e.g.:
{% if field.help_text %}
<div style="color: grey">{{ field.help_text|safe }}</div>
{% endif %}
You might also want to consider using styled classes instead of inline styling for these elements.
If you don't want HTML to appear in the help_text then you will need to modify the help_text of the field in question.
Template tag {% if %} {% endif %} does't work correctly. I need to make the search results on the page appear only after the search query. But for some reason, when the page loads, all existing content appears at once. But after get request filter works correctly.
views.py
def search(request):
place_list = Places.objects.all()
place_filter = PlaceFilter(request.GET, queryset=place_list)
return render(request, 'search/user_list.html', {'filter': place_filter})
html
{% if filter.qs %}
<div class="row">
{% for obj in filter.qs %}
<div class="col-md-3 admin__block">
<div class="cover__wrapper">
<img src="{{ obj.main_photo.url }}" alt="">
<span>#</span>{{ obj.name }}
<p>{{ obj.description }}</p>
</div>
</div>
{% endfor %}
</div>
{% endif %}
filters.py
class PlaceFilter(django_filters.FilterSet):
name = django_filters.CharFilter(lookup_expr='icontains', widget=forms.TextInput(attrs={
'placeholder': 'Search place', 'class': 'input__search'}))
class Meta:
model = Places
fields = ['name']
FilterSet's qs property returns filterset's queryset. So it's always True.
You can use request.GET in template to check if GET contains any request data and render only filtered data:
{% if request.GET %}
<div class="row">
{% for obj in filter.qs %}
<div class="col-md-3 admin__block">
<div class="cover__wrapper">
<img src="{{ obj.main_photo.url }}" alt="">
<span>#</span>{{ obj.name }}
<p>{{ obj.description }}</p>
</div>
</div>
{% endfor %}
</div>
{% endif %}
So I'm attempting to implement the pagination for a website that i'm working on, but it seems not to work completely.
class ExampleListView(FormMixin, ListView):
model = Example
template_name = "example.html"
paginate_by = 3
context_object_name = "example_list"
allow_empty = True
page_kwarg = 'page'
paginate_orphans = 0
form_class = ExampleForm
Then in the html I have the following
<tbody>
{% for formset_form in formset %}
...
{% endfor %}
...
{% if is_paginated %}
<div class="pagination">
<span class="page-links">
{% if page_obj.has_previous %}
Previous
{% endif %}
<span class="page-current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}
</span>
{% if page_obj.has_next %}
Next
{% endif %}
</span>
</div>
{% else %}
<p></p>
{% endif %}
I currently have 5 forms there in the table, and paginated it by 3. What it does is that is displays Page 1 of 2 Next, like it should, but it displays all of the forms on the pages.
The Django ListView is meant to render 'A page representing a list of objects.'. You try to display a paginated list of formsets. Interesting thought, but not what ListView does.
This answer to a previous question may help you to achieve what you want.
I was stucked the last two days with this problem. First part of the code:
viewa.py
def A_dashboard(request):
user = User.objects.get(id=request.user.id)
users = User.objects.all()
return render_to_response('dashboard.html', {"user": user, "users": users} , context_instance=RequestContext(request))
The dashboard will be different depending of one property of the user.
dashboard.html
{% extends "index.html" %}
{% load staticfiles %}
{% block content %}
{% if user.is_A %}
{% include "dashboards/A_dashboard.html" %}
{% endif %}
{% if user.is_B %}
{% include "dashboards/B_dashboard.html" %}
{% endif %}
{% endblock content %}
The concrete dashboard to the A user.
dashboards/A_dashboard.html
{% load staticfiles %}
<div class="offrow rowpadding dashboard-info">
<div class="container">
<div class="row">
<div class="col-lg-7">
<div class="row">
{{user}}
{{users}}
{% for user_ in users %}
<p>{{user_}}</p>
{% endfor %}
</div>
</div>
<div class="col-lg-5">
<div class="row">
<div class="col-md-12">
{% include 'calendar.html' %}
</div>
</div>
</div>
</div>
</div>
Right Now Im logged in as an A user. In the view I check the class to check that everything is ok. Its of class <class 'django.db.models.query.QuerySet'>
However, when I try to iterate the list in the template, it doesn't exist.
Can you imagine whats happening?
Regards.
it is strange that objects don't exist.
User.objects.all() is queryset, so in template users is a queryset.
user is a user object, it has some attributes, such as username , email and so on.
for user_ in users you get every user object(user_) similar to user above
so try this
<div class="row">
name: {{ user.username }}
{% for user_ in users %}
<p>name: {{ user_.username }}</p>
{% endfor %}
</div>
{{user.username}} and {{ user.username }} are the same, spaces just for looking elegant