could not parse the remainder: '[0].title' from 'i[0].title' - python

I am trying to render the data from the database one by one by it does not work.
index.html
{% for i in edus %}
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">{{i[0].title}}
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<div class="row">
<div class="col-md-12">
<p>{{i[0].descripiton}} </p>
</div>
</div>
</div>
</div>
</div>
{% endfor %}

If you want to render specific index, you should change:
{{i[0].title}} into {{ i.0.title }}
{{i[0].descripiton }} into {{ i.0.description }}

Related

Bootstrap grid not aligned properly

With the django framework I use a loop in my templates to display the image inside my database, I also use the Bootstrap grid to make it clean, but it not working correctly. As you can see the image on at the bottom are not align correctly.
hmtl
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-3 col-lg-2">
<div class="card-filter" style="width: 10rem;">
<div class="card-header">
Pattern Type
</div>
<ul class="list-group list-group-flush">
{% for pattern in categories %}
<li class="list-group-item">{{pattern.name}}</li>
{% endfor %}
</ul>
</div>
</div>
{% for photo in photos %}
<div class="col-sm-12 col-md-5 col-ld-5">
<div class="card" style="width: 25rem;">
<img class="card-img-top" src="{{photo.image.url}}" alt="Card image cap">
<div class="card-body">
<p class="card-text">Symbol: GBPUSD<br>Pattern: ButterFly Bullish</p>
View Pattern
</div>
</div>
</div>
{% endfor %}
</div>
</div>
views.py
def home(request):
categories = Category.objects.all()
photos = Photo.objects.all()
context = {'categories': categories,
'photos': photos}
return render(request, "main/home.html", context)
You better put all the images in a parent that takes rest of the width after the list on the left, so it should be like this:
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-3 col-lg-2">
<div class="card-filter" style="width: 10rem;">
<div class="card-header">
Pattern Type
</div>
<ul class="list-group list-group-flush">
{% for pattern in categories %}
<li class="list-group-item">{{pattern.name}}</li>
{% endfor %}
</ul>
</div>
</div>
<div class="col-sm-12 col-md-9 col-lg-10">
{% for photo in photos %}
<div class="col-sm-12 col-md-6">
<div class="card" style="width: 25rem;">
<img
class="card-img-top"
src="{{photo.image.url}}"
alt="Card image cap"
/>
<div class="card-body">
<p class="card-text">
Symbol: GBPUSD<br />Pattern: ButterFly Bullish
</p>
<a href="{% url 'photo' photo.id %}" class="btn btn-dark"
>View Pattern</a
>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
</div>

How to connect 2 forms in HTML and Django?

I am creating a web page and i'm wondering if i could make sure that the data on the left is filled before pressing the blue button. On the left you can see a form to pay by entering your address, city etc. but on the right you can see another form with stripe implemented to pay with credit card. I don't know how to get the data from the left and save it in the database so I can create a receipt after successful payment. Here is the code down below.
<div class="container-2">
<div class="gray-hover">
<form method="POST">
<div class="item" id="payment">
<div class="row">
<h4>Možnost nakupa 1: Plačilo po povzetju <small><i>(Za plačevanje s kartico je treba izbrati samo
količino in vrsto izdelka!)</i></small></h4>
{% csrf_token %}
{% if form %}
<div class="input-group">
<div style="color: red;">{{ form.name.errors }}</div>
{{ form.name }}
</div>
<div class="input-group">
<div style="color: red;">{{ form.last_name.errors }}</div>
{{ form.last_name }}
</div>
<div class="input-group">
<div style="color: red;">{{ form.street_name.errors }}</div>
{{ form.street_name }}
</div>
<div class="input-group">
<div style="color: red;">{{ form.city_name.errors }}</div>
{{ form.city_name }}
</div>
<div class="input-group">
<div style="color: red;">{{ form.email.errors }}</div>
{{ form.email }}
</div>
<div class="input-group">
<div style="color: red;">{{ form.number.errors }}</div>
{{ form.number }}
</div>
{% endif %}
</div>
</div>
<div class="item" id="payment2">
<div class="row">
<div class="input-group">
{{ form.num_elements.errors }}
{{ form.num_elements }}
</div>
<div class="input-group" id="check_div">
<div
style="display: flex;width:100%;justify-content: space-between;align-items: center;font-size:medium;flex-wrap: wrap;">
<div style="display: flex;justify-content: space-between;margin:3px;">
{{ form.select_type.errors }}
{{ form.select_type.label_tag }}
{{ form.select_type }}
</div>
<div style="display: flex;justify-content: space-between;margin:3px;">
{{ form.select_type2.errors }}
{{ form.select_type2.label_tag }}
{{ form.select_type2 }}
</div>
</div>
</div>
<div class="input-group">
{{ form.warning_el.errors }}
{{ form.warning_el }}
</div>
<div style="display: flex;justify-content: space-between;margin: 0.5rem;">
<button class="button" type="submit" id="button"> Naroči <small>(povzetje)</small></button>
<a class="button" id="stripe-button">Plačaj s kartico!</a>
</div>
</div>
</div>
</form>
</div>
<div class="gray-hover">
<div class="item" id="payment3" style="width:100%;">
<h4>Možnost nakupa 2: Plačilo s kartico <small><i>(Za plačevanje s kartico je treba izbrati samo
količino in vrsto izdelka!)</i></small></h4>
<div class="row" style="display: flex; justify-content: center;">
<form id="payment-form" data-locale="si" style="width:100%;">
<div id="payment-element">
<!--Stripe.js injects the Payment Element-->
</div>
<button id="submit" class="button1">
<div class="spinner hidden" id="spinner"></div>
<span id="button-text">Plačaj</span>
</button>
<div id="payment-message" class="hidden"></div>
</form>
</div>
</div>
</div>
</div>
AND HERE IS THE SCREENSHOT OF THAT 'CONTAINER-2'
Firstly make sure all your inputs are under one form and one view.
Use required=True in your Django form in order to make sure every input is entered.
If you are using Django forms, for example
from django import forms
class FooForm(forms.Form):
email = forms.EmailField(max_length=100,required=True,
widget=forms.TextInput(
attrs={ 'required': 'true' }),
)
In your view use the form is_valid method
form = FooForm(request.POST)
if form.is_valid():
# Complete Your Business Logic
...
return redirect("redirect_view")
# Else Return With Error Message
Use the form to save your data. If using Django form is not an option, you have to use javascript to disable the button until all inputs are not filled up

Colapsable Button Bootstrap Django

I'm trying to create a social media news feed, with the posts, but I dont want to show the comments just if the user presses the button Show Comments. The problem is that when I press the button of one post, all the post will extend comments. I don't really know how to solve this...
{% for post in posts %}
<div class="container">
<div class="row">
<div class="[ col-xs-12 col-sm-offset-1 col-sm-5 ]">
<div class="[ panel panel-default ] panel-google-plus">
<div class="panel-heading">
<img class="[ img-circle pull-left ]" width="50" height="50" src="{{post.author.profile.image.url}}" alt="Mouse0270" />
<h3 class="author">{{post.author}}</h3>
<h5><span>Shared publicly</span> - <span> {{post.date_posted}} </span> </h5><br>
</div>
<div class="panel-body">
{% if user == post.author %}
<a class="text-right" href="{% url 'post-update' post.id %}"><p>Update</a>
<a class="text-danger text-right" href="{% url 'post-delete' post.id %}">Delete post</a>
{% endif %}
<p>{{post.content}}</p>
{% if post.image %}
<img src ="{{post.image.url}}" width="300" height="300" border="0" class="img-thumbnail">
{% endif %}
{% if post.video %}
<video width="250" controls >
<source src="{{post.video.url}}" type="video/mp4" >
</video>
{% endif %}
</div>
<br/>
<div class="container" >
<button class="btn btn-primary">Add comment</button>
</div>
<br>
<button type="button" class="btn btn-danger btn-block" data-toggle="collapse" data-target="#demo">See comments</button>
<div id="demo" class="collapse">
{% for comment in post.comments.all %}
<div class="container">
<div class="row">
<div class="col-sm-5">
<div class="panel panel-default">
<div class="panel-heading">
<strong>{{comment.author}}</strong> <span class="text-muted">commented at {{comment.created_on|date:"d M g:i a"}}</span>
</div>
<div class="panel-body">
<img class=" img-circle" width="30" height="30" src="{{comment.author.profile.image.url}}">
{{comment.body}}
</div><!-- /panel-body -->
</div><!-- /panel panel-default -->
</div><!-- /col-sm-5 -->
</div>
</div>
{% endfor %}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
The problem is with the collapsable button above {% for comment in post.comments.all %}, but I dont know how to make it unique for every post.

how to for loop the bootstrap grid

I'm trying to make it like the following: https://getbootstrap.com/docs/4.3/examples/pricing/
it shows the screen divided into three section
so it should be like this I think...
<div class="row mb-2">
<div class="col-4 bg-danger">
.col-4
</div>
<div class="col-4 bg-warning">
.col-4
</div>
<div class="col-4 bg-success">
.col-4
</div>
</div>
But the problem is I am using django jinja template and for loop. so it groups the whole content.
This is my code but it won't do what's like in the link
<div class="row">
<div class="col-6 col-md-4">
<div class="card">
{% for all_episode in episode %}
<img class="card-img-top" src='{{all_episode.image.url}}'>
<div class="card-body">
<h5 class="card-title">
{{ all_episode.title }}
</h5>
<p class="card-text">{{ all_episode.story |slice:":100" }}...</p>
</div>
<div class="card-footer">
<small class="text-muted">
<span class="h5">
{{ all_episode.series }}
</span> /
<span class="h6">{{ all_episode.season }}</span></small>
</div>
{% endfor %}
</div>
</div>

Hide None value of django form field

I have a end_date field on my news apps which is a facultative field.
When a author is editing one of his new and erase the end_date, I set in the model the end_date to None.
My problem is that the field then display the value 'None' on next edit instead of just being blank.
Here is my field :
models.py
end_date = models.DateTimeField(null=True, blank=True)
forms.py
end_date = forms.CharField(label='', required=False, widget=forms.DateTimeInput(attrs={'class': 'form-control', 'format': 'DD, d MM yy', 'placeholder': _('End date')}))
and the assignation in my view :
views.py
if news_form.cleaned_data['end_date'] == '' :
edited_new.end_date = None
edited_new.save()
EDIT :
here is my template :
news.html
<form action="#" method="post" role="form" id="news-form">{% csrf_token %}
{{ news_form.media }}
<div class="col-xs-12">
<div class="panel panel-primary">
<div class="panel-heading">
<div class="row">
<div class="col-xs-12 col-md-6">
<h2 class="panel-title">{% trans "Add or edit news" %}</h2>
</div>
<div class="col-xs-12 col-md-6">
<div class="pull-right">
<span class="glyphicon glyphicon-floppy-remove"></span> {% trans "Cancel" %}
<button type="submit" class="btn btn-primary form-btn save"><span class="glyphicon glyphicon-floppy-saved"></span><span class="button-text"> {% trans "Save" %}</span></button>
</div>
</div>
</div>
</div>
<div class="panel-body">
<div class="row">
<div class="col-xs-12 col-md-6">
{{ news_form.title }}
</div>
<div class="col-xs-4 col-md-2">
{% trans 'Tag' %}
</div>
<div class="col-xs-8 col-md-4">
{{ news_form.tag }}
</div>
</div>
<div class="row">
<div class="col-xs-12 no-label-field redactor-field">
{{ news_form.message }}
</div>
</div>
<div class="row">
<div class="col-xs-6 date-field no-label-field">
<div class="form-group">
<div class='input-group date datetimepicker'>
{{ news_form.start_date }}
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
<div class="col-xs-6 date-field no-label-field">
<div class="form-group">
<div class='input-group date datetimepicker'>
{{ news_form.end_date }}
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
{{ news_form.active }} {% trans 'Activate the news' %}
</div>
</div>
</div>
</div>
</div>
<span style="display:none;">{{ news_form.id }}</span>
</form>
You can use the filter default_if_none available since Django 1.3, it'll only work for values that are None.
{{ news_form.end_date|default_if_none:'' }}
Docs

Categories