Post comments in Django Homepage - html

i have this code with me as part of a tutorial i was following for blog post. I have a comment section on my detailview, I want it to be shown in the homepage as well. I tried with some codes, but the problem is same comments are showing in all posts (only) in homepage.
please take a look at the code.
I have a comment snippet which I use it in my homepage.
<!-- Comment showing section -->
<div class="main-comment-section">
<div class="container-fluid mt-2">
<div class="form-group row">
<!-- Comment Form -->
<form class="comment-form" method="post" action=".">{% csrf_token %}
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<button type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">Comment</button>
</form>
<!-- Comment Form end -->
</div>
</div>
{{ comments.count }} Comment{{ comments|pluralize }}
{% for comment in comments %}
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="10" width="10" src="{{ comment.user.profile.profile_pic.url }}"><h6>{{ comment.user.first_name|capfirst }} {{ comment.user.last_name|capfirst }}</h6><br>
<p style="font-size: 8px;">{{ comment.timestamp }}</p>
<p style="font-size: 14px;" class="mb-3">{{ comment.content }}</p>
<a type="button" name="button" class="reply-btn ml-4"><p style="font-size: 13px;"> Reply</p></a>
{% if request.user == comment.user %}
Delete</td>
{% endif %}
</blockquote>
{{ comment.reply.count }}
<div class="replied-comments col-md-5" style="display: none;">
{% for reply in comment.replies.all %} <!--replies is the related name in the model-->
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="50" width="50" src="{{ reply.user.profile.profile_pic.url }}"><h6>{{ reply.user.first_name|capfirst }} {{ reply.user.last_name|capfirst }}</h6><br>
<p style="font-size: 13px;" class="mb-3">{{ reply.content }}</p>
</blockquote>
{% endfor %}
<div class="form-group row">
<form class="reply-form" method="post" action=".">{% csrf_token %}
<input type="hidden" name="comment_id" value="{{ comment.id }}">
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<input type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">
</form>
</div>
</div>
{% endfor %}
</div>
PS. The comment is correctly showing in the detail view, but in homepage its showing all the comments of all posts in every posts.

Have the following changes,
views.py
...
comments = Comment.objects.filter(post=posts_list.first()) # Delete this line
context = {
'page':page,
'posts':posts,
'tag':tag,
'actions':actions,
'comments': comments, # Delete this too
}
return render(request, 'posts/users/myhome.html', context)
Change posts/comment_section.html too,
<!-- Comment showing section -->
<div class="main-comment-section">
<div class="container-fluid mt-2">
<div class="form-group row">
<!-- Comment Form -->
<form class="comment-form" method="post" action=".">{% csrf_token %}
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<button type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">Comment</button>
</form>
<!-- Comment Form end -->
</div>
</div>
{% if not post.comment_set.all %}
No comments to display.
{% endif %}
{% for comment in post.comment_set.all %}
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="10" width="10" src="{{ comment.user.profile.profile_pic.url }}"><h6>{{ comment.user.first_name|capfirst }} {{ comment.user.last_name|capfirst }}</h6><br>
<p style="font-size: 8px;">{{ comment.timestamp }}</p>
<p style="font-size: 14px;" class="mb-3">{{ comment.content }}</p>
<a type="button" name="button" class="reply-btn ml-4"><p style="font-size: 13px;"> Reply</p></a>
{% if request.user == comment.user %}
Delete</td>
{% endif %}
</blockquote>
{{ comment.reply.count }}
<div class="replied-comments col-md-5" style="display: none;">
{% for reply in comment.replies.all %} <!--replies is the related name in the model-->
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="50" width="50" src="{{ reply.user.profile.profile_pic.url }}"><h6>{{ reply.user.first_name|capfirst }} {{ reply.user.last_name|capfirst }}</h6><br>
<p style="font-size: 13px;" class="mb-3">{{ reply.content }}</p>
</blockquote>
{% endfor %}
<div class="form-group row">
<form class="reply-form" method="post" action=".">{% csrf_token %}
<input type="hidden" name="comment_id" value="{{ comment.id }}">
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<input type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">
</form>
</div>
</div>
{% endfor %}
</div>

Related

How check the user how much time spend on a video in

I am using django framework. I embed the youtube video link in html, I want to know that the user spend how much time on the video and save the time in database with his deatils.
the details of the user is stored in cookies. like email.
here is my code.
<div id="request" class="modal fade" {% if request.session.email %} data-backdrop="static" {% endif %} role="dialog">
{% if 'email' not in request.session %}
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<span class="close" data-dismiss="modal" aria-label="Close">×</span>
<h2 class="modal-title">Get start</h2>
</div>
<div class="modal-body text-center">
<form class="form-request" action="" method="post">
{% csrf_token %}
<div class="row-fields row">
<div class="form-group col-field">
<input type="text" class="form-control" name="name" required placeholder="Name *" id="name">
</div>
<div class="form-group col-field">
<input type="email" class="form-control" name="email" required placeholder="Email *" id="email">
</div>
<div class="form-group col-field">
<input type="number" class="form-control" name="phone" required placeholder="Phone *" id="phone">
</div>
<div class="col-sm-12 buttons">
<button type="submit" class="bttn-hover color-1" data-text-hover="Submit">Send request</button>
</div>
</div>
</form>
</div>
</div>
</div>
{% elif request.session.email %} {% comment %} <a href="http://www.youtube.com/watch?v=ANwf8AE3_d0"> {% endcomment %}
<iframe class="embed-responsive-item set-style-of-iframe" src="https://www.youtube.com/embed/tgbNymZ7vqY/?autoplay=1;enablejsapi=1"
allowfullscreen style="height:50%; width:65%" id="myvideo-iframe"></iframe>
<button class="btn btn-close button-closing-style" aria-label="Close" onclick="my_video_pause();"
class="close" data-dismiss="modal">close</button>
<a href="http://cdn.onlinewebfonts.com/svg/img_414950.png" class="button-closing-style" onclick="my_video_pause();" data-dismiss="modal">
</a>
{% endif %}
</div>
</div>

HTML page, load 10 items, then wait for scroll and load more

I have a list of so many items that loading them all at the time is very uncomfortable and takes a long time. I would like to implement a script that loads only 10 items and waits for scroll to load more. However I have no idea how to code it.
This is how my page looks:
{% extends "layout.html" %}
{% block content %}
<div class="container">
{% for i in range(data|length) %}
<div class="re-container">
<div class="re-container-item">
<div class="re-item">
<image class="re-img" src="{{url_for('static', filename='img/' + data.UNIQUE_RE_NUMBER[i].replace('/', '-') + '.jpg')}}" onerror="if (this.src != 'static/img/default.jpg') this.src = 'static/img/default.jpg';"></image>
</div>
</div>
<div class="re-container-item">
<div class="re-item">
<p>{{ data.LOCATION[i] }}</p>
<p class="re-date"> {{ data.MIN_DATE[i] }} - {{ data.MAX_DATE[i] }} </p>
</div>
<div class="re-item">
<p>{{ data.ADDRSS[i] }}</p>
</div>
<div class="re-item">
<p><a class="article-title" href="{{url_for('/dashapp/', ure=data.UNIQUE_RE_NUMBER[i])}}">{{data.UNIQUE_RE_NUMBER[i]}}</a></p>
<p class="re-price">{{data.RE_PRICE[i]}}</p>
<hr>
</div>
<div class="re-item">
<p>{{data.RE_INFO[i]}}</p>
</div>
{% if current_user.is_authenticated %}
<div class="re-item">
<form action="{{ url_for('saved') }}" method="POST">
<input type="hidden" value="{{ data.LOCATION[i] }}" name="location">
<input type="hidden" value="{{ data.UNIQUE_RE_NUMBER[i] }}" name="unique">
<input type="hidden" value="{{ data.ADDRSS[i] }}" name="address">
<input type="hidden" value="{{ data.RE_INFO[i] }}" name="info">
<input type="hidden" value="{{ data.RE_HREF[i] }}" name="href">
<input type="hidden" value="{{ data.TYP_ID[i] }}" name="typ">
<input type="hidden" value="{{ data.RE_POLOHA[i] }}" name="poloha">
<input type="hidden" value="{{ data.RE_DRUH[i] }}" name="druh">
<input type="hidden" value="{{ data.RE_TYP[i] }}" name="re-typ">
<input type="hidden" value="" name="dispozice">
<input type="hidden" value="" name="f_druh">
<input type="hidden" value="" name="pplocha">
<input type="hidden" value="" name="plocha">
<input type="hidden" value="" name="l_druh">
<input type="hidden" value="" name="site">
<input type="hidden" value="{{ data.MIN_DATE[i] }}" name="min_d">
<button class="re-button" type="submit">Save</button>
</form>
</div>
{% endif %}
<div class="re-item">
<p>Poloha domu/vily: {{data.RE_POLOHA[i]}}</p>
</div>
<div class="re-item">
<p>Druh domu/vily: {{data.RE_DRUH[i]}}</p>
</div>
<div class="re-item">
<p>Typ domu/vily: {{data.RE_TYP[i]}}</p>
</div>
<div class="re-item">
{% if data.RE_HREF[i] != None %}
More here
{% endif %}
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock content %}
Does anyone have some suggestion?
You can use the loading attribute in HTML. Just like below.
<img src="niceCats.jpg" loading="lazy">
I would advise you to read this aricle that CSSTrick put up on their page. Note this attribute is not supported on any safari version for IOS.

Django Formset one form in each row

How to place each form of a formset on a horizontal line, one below the other without using a table?
My html is putting everything on one line.
Any help much appreciated.
Html
[<div class="form-group row">
<div class="row">
{{ formset.management_form }}
{% for form in formset.forms %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
<div class="col-sm-2">
{{ form.logical_operator|as_crispy_field }}
</div>
<div class="col-sm-3">
{{ form.event|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.operator|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.event_value|as_crispy_field }}
</div>
<div class="col-md-1 align-self-center delete-form">
<button onclick="exclude(this)" type="button"
title="Excluir"
class="btn btn-danger btn-sm">
<i class="feather icon-trash-2"></i>
</button>
</div>
{% endfor %}
</div>
</div>
image from screen
This reorganization solved the problem.
Thanks for the help!
<div class="form-group row">
<div class="col-sm-12">
{{ formset.management_form }}
{% for form in formset %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
<div class="row">
<div class="col-sm-2">
{{ form.logical_operator|as_crispy_field }}
</div>
<div class="col-sm-3">
{{ form.event|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.operator|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.event_value|as_crispy_field }}
</div>
<div class="col-md-1 align-self-center delete-form">
<button onclick="exclude(this)" type="button"
title="Excluir"
class="btn btn-danger btn-sm">
<i class="feather icon-trash-2"></i>
</button>
</div>
</div>
{% endfor %}
</div>
</div>
Please try using </br> (break line) command:
<h1> HEllo</h1> </br> <h2>How are you</h2>

How to loop foreach in blade while using jquery in Laravel

I'm using Laravel 5.4 and I have never tried jquery so I have no idea what I'm doing, unfortunately.
This is my blade:
<h3>Comments:</h3>
<ul class="list-unstyled activity-list">
#foreach ($threads->comments()->where('parent', 0)->orderBy('created_at', 'desc')->get() as $comment)
<li>
<img src="{{ $comment->user->getAvatar() }}" alt="Avatar" class="img-circle pull-left avatar">
<p><a href="{{ route('profile.view', $comment->user->id) }}">
{{ $comment->user->user_name }}</a>:
<br>
{{ $comment->content }}
<span class="timestamp">{{ $comment->created_at->diffForHumans() }}</span>
</p>
#if ($threads->lock == 0)
<a id="btn-comment" style="padding-left:3.5em">reply</a>
<br>
<form action="{{ route('threads.comments.add') }}" id="comment" method="POST" style="padding-left:3.5em; display:none;">
{{ csrf_field() }}
<input type="hidden" name="thread_id" value="{{ $threads->id }}">
<input type="hidden" name="parent" value="{{ $comment->id }}">
<input type="text" name="content" class="form-control" placeholder="Add reply...">
<input type="submit" class="btn btn-primary btn-xs" value="Add Reply">
</form>
<br>
#endif
#foreach ($comment->childs as $child)
<div style="padding-left:3.5em;">
<img src="{{ $child->user->getAvatar() }}" alt="Avatar" class="img-circle pull-left avatar">
<p><a href="{{ route('profile.view', $child->user->id) }}">
<span style="padding-left:1em;">{{ $child->user->user_name }}</a>: </span>
<br>
<span style="padding-left:1em;">{{ $child->content }}</span>
<span class="timestamp" style="padding-left:4.3em;">{{ $child->created_at->diffForHumans() }}</span>
</p>
</div>
#endforeach
</li>
#endforeach
</ul>
Here's my custom script:
<script>
$(document).ready(function(){
$('#btn-comment').click(function(){
$('#comment').toggle('slide');
});
});
</script>
These lines specifically are the ones that I'm trying to run using the script above:
#if ($threads->lock == 0)
<a id="btn-comment" style="padding-left:3.5em">reply</a>
<br>
<form action="{{ route('threads.comments.add') }}" id="comment" method="POST" style="padding-left:3.5em; display:none;">
{{ csrf_field() }}
<input type="hidden" name="thread_id" value="{{ $threads->id }}">
<input type="hidden" name="parent" value="{{ $comment->id }}">
<input type="text" name="content" class="form-control" placeholder="Add reply...">
<input type="submit" class="btn btn-primary btn-xs" value="Add Reply">
</form>
<br>
#endif
Ask me if you need further detail

Html form not submitting data

This html form is not submitting data and i don't know why.I have tried to find the error and i do not seem to get around it,any help will be gladly appreciated
{% extends 'registration/base.html' %}
{% load widget_tweaks %}
{% block title %}Create account{% endblock %}
{% block body %}
<div class="container" style="padding-top:20px">
<div class="row">
<div class="col-md-4 card jumbotron ">
<h2><span class="badge badge-light">Create Account</span></h2><br>
<form action="{% url 'tzuzz:create_account' %}" method="post">
{% csrf_token %}
<div class="form-group">
<label>Email </label>
<input class="form-control is-valid" type="text" name="email" placeholder="Email">
<label>Names</label>
<input class="form-control is-valid" type="text" name="names" placeholder="Enter first and last name">
<label>Sex</label>
<select class="form-control" name="sex">
<option>Male</option>
<option>Female</option>
</select>
<label>Date of birth</label>
<input class="form-control is-valid" name="date" type="date">
<label>Password</label>
<input class="form-control is-valid" type="password" name="password" placeholder="Password">
</div>
<button class="btn btn-secondary">Login</button>
</form>
<br><br>
{% if error %}
<p class="alert alert-danger alert-dismissible fade show" role="alert">
{{error}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</p>
{% endif %}
</div>
</div>
{% endblock %}
Here is the answer i forgot to include type submit in my button
<button class="btn btn-secondary" type="submit">Login</button>