loop trough foreignKey objects in HTML with UpdateView - html
I am in process of writing UpdateView Class form , I am using inlineformset_factory to get ForignKey objects and its works , my issue is that i would like to iterate all ForignKey with HTML for displaying all its objects , and i am don't really know how to achieve it , since all objects i refer it , form. as declared in my forms.py so my question is someone could help me with given view and form how to, in HTML should i iterate my ForignKey objects
Please advice
Thanks
my code is shown below
my views.py Update view
class TaskIdUpdateView(UpdateView):
taskidformset = inlineformset_factory(MainTask,ChildTask, fields=('task_description','task_info','task_complete',
'sub_task','task_precent_complete','task_due_date','task_assign'))
model = MainTask
template_name = "taskid_update.html"
form_class = TaskUpdateForm
forms.py class form
class TaskUpdateForm(ModelForm):
TASK_STATUS_CHOICES = [
('ST', 'STARTED'),
('NS', 'NOT STARTED'),
('IP', 'IN PROGRESS'),
('PA', 'PAUSED'),
('CO', 'COMPLETED'),
]
INPUTֹTIMEֹFORMATS = ['%Y-%m-%d', # '2006-10-25'
'%m/%d/%Y',
'%Y/%m/%d', # '10/25/2006'
'%Y/%m/%d %H:%M',
'%m/%d/%y',
'%Y-%m-%d %H:%M:%S'] # '10/25/06'
#Main Task objects
task_title = forms.CharField(required=False, widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Task Title'}))
global_task_info = forms.CharField(required=True, widget=forms.Textarea(attrs={'class':'form-control','placeholder':'Task Description'}))
due_date = forms.DateTimeField(required=False, input_formats=INPUTֹTIMEֹFORMATS, widget=forms.DateTimeInput(attrs={
'class': 'form-control',
'id': 'picker'
}))
global_task_assign = forms.ModelChoiceField(queryset= UserProfile.objects.all(), widget=forms.Select(attrs={'class':'form-control'} ))
task_status = forms.ChoiceField(label='', choices=TASK_STATUS_CHOICES, widget=forms.Select(attrs={'class':'form-control'}))
complete = forms.BooleanField( required=False, widget=forms.CheckboxInput(attrs={'type':'checkbox', 'class':'custom-control-input', 'id':'switchcomplete'}))
overall_precent_complete = forms.IntegerField(widget=(forms.NumberInput(attrs={'type':'range', 'min':'0', 'max':'100', 'value':'50', 'class':'range-slider__range', 'id':'PreRange'})))
task_location = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
#Child Tasks objects
task_description = forms.CharField(max_length=200, widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Sub Task Description'}))
task_info = forms.CharField(max_length=500, widget=forms.Textarea(attrs={'class':'form-control','placeholder':'Sub Task Description'}))
task_complete = forms.BooleanField( required=False, widget=forms.CheckboxInput(attrs={'type':'checkbox', 'class':'custom-control-input', 'id':'switchcomplete'}))
sub_task = forms.CharField(max_length=500, widget=forms.Textarea(attrs={'class':'form-control','placeholder':'Sub Task Description'}))
task_precent_complete = forms.IntegerField(widget=(forms.NumberInput(attrs={'type':'range', 'min':'1', 'max':'100', 'value':'50', 'class':'slider', 'id':'myRange'})))
task_due_date = forms.DateTimeField(input_formats=INPUTֹTIMEֹFORMATS, widget=forms.DateTimeInput(attrs={
'class': 'form-control',
'id': 'picker'
}))
task_assign = forms.ModelChoiceField(queryset= UserProfile.objects.all(), widget=forms.Select(attrs={'class':'form-control'} ))
class Meta:
model = MainTask
fields = ['task_title',
'global_task_info',
'due_date',
'global_task_assign',
'task_status',
'complete',
'overall_precent_complete',
'task_location',
'global_task_assign',
'task_status',]
taskidformset = inlineformset_factory(MainTask,ChildTask, fields=('task_description','task_info','task_complete',
'sub_task','task_precent_complete','task_due_date','task_assign'))
update.html
{% extends 'base.html'%}
{% load static from static %}
{% block content %}
<!--accordion css-->
<link href="{% static 'css/range.css' %}" rel="stylesheet">
<div class="conatainer">
<div class="col text-center py-5">
<form action="" class="" method="POST">
<h3 class="mt-3 text-left">Update Task </h3>
<hr>
<p class="text-muted text-left">Update Itom task</p>
{% csrf_token %}
{% if form.errors %}
</form>
<!-- Error messaging -->
<div id="errors">
<div class="inner">
<p>There were some errors in the information you entered. Please correct the following:</p>
<ul>
{% for field in form %}
{% if field.errors %}<li>{{ field.label }}: {{ field.errors|striptags }}</li>{% endif %}
{% endfor %}
</ul>
</div>
</div>
<!-- /Error messaging -->
{% endif %}
<div class="input-group mt-3 mb-3 mr-auto">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1"><i class="fas fa-book-medical"></i></span>
</div>
{{ form.task_title}}
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-pen"></i></span>
</div>
{{form.global_task_info}}
</div>
<!---date time picker-->
<h6 class="text-left">Task Due Date</h6>
<div class="input-group date mb-3 col-3">
<div class="input-group-append">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
{{ form.due_date }}
</div>
<!--end of date time picker-->
<!---Task Location-->
<div class="input-group mb-3 mt-3 col-8">
<div class="input-group-prepend">
<div class="input-group-text"><i class="fas fa-map-marked-alt"></i></div>
{{form.task_location}}
</div>
</div>
<!--End Of Task Location-->
<!---user assign-->
<h6 class="text-left">Assign Task to IT member</h6>
<div class="input-group mb-3 mt-3 col-8">
<div class="input-group-prepend">
<div class="input-group-text"><i class="fas fa-user-tie"></i></div>
{{form.global_task_assign}}
</div>
</div>
<!--End Of User Assign-->
<h6 class="text-left">Set Task Status</h6>
<div class="input-group mb-3 mt-3 col-4">
<div class="input-group-prepend">
<div class="input-group-text"><i class="far fa-caret-square-right"></i></div>
</div>
{{form.task_status}}
</div>
<div class="range-slider mb-4">
<h6 class="text-left">Set Task Global Progress</h6>
{{form.overall_precent_complete}}
<span class="range-slider__value">0</span>
</div>
<h4 class="mt-3 text-left">SubTask Section </h4>
<hr>
<!---subtask section-->
<div id="accordion">
{{formset.task_description }}
<div class="card">
<div class="card-header text-left bg-secondary">
<a class="card-link" data-toggle="collapse" href="#collapseOne">
Collapsible Group Item #1
</a>
</div>
<div id="collapseOne" class="collapse show" data-parent="#accordion">
<div class="card-body">
</div>
</div>
</div>
</div>
<!---End subtask section-->
<div class="col text-left">
<button type="submit" value="Save" class="btn btn-primary btn-lg text-white mt-2"><span><i class="fas fa-database"></i></span> Create Task</button>
</div>
</div>
</div>
{% endblock content %}
Related
How to make each product have functioning plus minus button on my product page and add its price to the total amount?
enter image description hereI'm working on Ecommerce Website that sells Coffee and my problem is that the only working plus and minus button is the first coffee. The plus and minus buttons on the others are not functioning. Is it about the for statement where I'm wrong? or the structure of the html isn't right. I've copied some of the code on cart.html file to make the product page had the option of adding products to the cart and also has a proceed button to the checkout page. The before structure of the site was you click on the image of a product and directed to the addtocart page then to the checkout page.`{% extends 'app/base.html' %} {% load static %} {% block title %}Category{% endblock title %} {% block main-content %} <div class="container my-5"> <div class="row"> <div class="col-sm-8"> <div class="card"> <div class="card-body"> {% for prod in product %} <div class="row"> <div class="col-sm-3 text-center align-self-center"><img src="{{prod.product_image.url}}" alt="" srcset="" class="img-fluid img-thumbnail shadow-sm" height="150" width="150"></div> <div class="col-sm-9"> <div> <h5>{{prod.title}}</h5> <p class="mb-2 text-muted small">{{prod.description}}</p> <div class="my-3"> <label for="quantity">Quantity:</label> <a class="minus-cart btn" pid={{product.id}}><i class="fas fa-minus-square fa-lg"></i></a> <span id="quantity">{{quantity}}</span> <a class="plus-cart btn" pid={{product.id}}><i class="fas fa-plus-square fa-lg"></i></a> </div> <div class="d-flex justify-content-between"> <a href="#" class="remove-cart btn btn-sm btn-secondary mr-3" pid={{prod.id}}>Remove item </a> <p class="mb-0"><span><strong>₱ {{prod.discounted_price}}</strong></span></p> </div> </div> </div> </div> <hr class="text-muted"> {% endfor %} </div> </div> </div> <div class="col-sm-4"> <div class="card"> <div class="card-body"> <h3>The Total Amount </h3> <ul class="list-group"> <li class="list-group-item d-flex justify-content-between align-items-center border-0 px-0 pb-0" >Amount<span id="amount">₱ {{amount}}</span></li> <li class="list-group-item d-flex justify-content-between align-items-center px-0">Shipping<span>₱ 144</span></li> <li class="list-group-item d-flex justify-content-between align-items-center border-0 px-0 mb-3"> <div> <strong>Total</strong> </div> <span id="totalamount"><strong>₱ {{totalamount}}</strong></span> </li> </ul> <div class="d-grid">Place Order</div> </div> </div> </div> </div> </div> {% endblock main-content%} `
how to fix the size of the comment section?
I'm working on a comments section for a blog in Flask. Having a very poor understanding of html, I decided to go with a public domain template. The script below works great, just one problem! After each submission, the comment window shrinks. Does anyone know how to make the size of the comment window constant? {% for comment in post.comments %} <div class="container my-2 py-2"> <div class="row d-flex justify-content-center"> <div class="col-md-12 col-lg-10"> <div class="card text-dark"> <div class="card-body p-4"> <p class="fw-light mb-4 pb-2"> Recent Comment By: </p> <div class="d-flex flex-start"> <img class="rounded-circle shadow-2-strong me-2" src="{{url_for('static', filename='uploads/' + comment.user.image_file)}}" alt="avatar" width="60" height="60" /> <div> <h6 class="fw-bold mb-2">{{comment.user.username}}</h6> <div class="d-flex align-items-center mb-2"> <p class="mb-0"> <i> {{comment.datetime.strftime('%B %d, %Y')}} </i> {% if user.id == comment.author or user.id == post.author %} <a href="/delete_comment/{{comment.id}}"> <span class="badge bg-danger">remove?</span> </a> {% endif %} </p> </div> <p class="mb-0"> {{comment.text|safe}} </p> </div> </div> </div> </div> </div> </div> {% endfor %}
MultiValueDictKeyError at /search/, Why am I getting this error?
I am very beginner at django, And fall onto this error MultiValueDictKeyError, I've got this error earlier, And I solved it after adding the name tag to HTML file, but this time I am not able to figure it out. please help me fix this error views.py from django.shortcuts import render from .models import Post # Create your views here. def main(request): return render(request, "blog/index.html", {"Posts":Post.objects.all()}) def viewpost(request, pk): return render(request, "blog/viewpost.html", {"Posts":Post.objects.get(id = pk)}) def search(request): if request.method == "GET": search = request.GET["search"] for post in Post.objects.all(): if search in post.Title: return render(request, "blog/search.html") else: return render(request, "blog/search.html", { "message": "Not Found" }) Index.html I am sorry this file is quite too big but its on the third block. I have commented out that block. {% extends 'blog/layout.html' %} {% load static %} {% block body %} <!-- Page Content --> <div class="container"> <div class="row"> <!-- Blog Entries Column --> <div class="col-md-8"> <h1 class="my-4">Page Heading <small>Secondary Text</small> </h1> <!-- Blog Post --> {% for post in Posts %} <div class="card mb-4"> <img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap"> <div class="card-body"> <h2 class="card-title">{{ post.Title }}</h2> <p class="card-text">{{ post.Description }}</p> Read More → </div> <div class="card-footer text-muted"> Posted on {{ post.Date }} by {{ post.Author }} </div> </div> {% endfor %} <!-- Pagination --> <ul class="pagination justify-content-center mb-4"> <li class="page-item"> <a class="page-link" href="#">← Older</a> </li> <li class="page-item disabled"> <a class="page-link" href="#">Newer →</a> </li> </ul> </div> <!-- Sidebar Widgets Column --> <div class="col-md-4"> **<!-- Search Widget --> <div class="card my-4"> <h5 class="card-header">Search</h5> <div class="card-body"> <form action="{% url 'search' %}" method="GET"> {% csrf_token %} <div class="input-group"> <input type="text" class="form-control" placeholder="Search for..." name="search"> <span class="input-group-append"> <button class="btn btn-secondary" type="button" >Go!</button> </span> </div> </form>** </div> </div> <!-- Categories Widget --> <div class="card my-4"> <h5 class="card-header">Categories</h5> <div class="card-body"> <div class="row"> <div class="col-lg-6"> <ul class="list-unstyled mb-0"> <li> Web Design </li> <li> HTML </li> <li> Freebies </li> </ul> </div> <div class="col-lg-6"> <ul class="list-unstyled mb-0"> <li> JavaScript </li> <li> CSS </li> <li> Tutorials </li> </ul> </div> </div> </div> </div> <!-- Side Widget --> <div class="card my-4"> <h5 class="card-header">Side Widget</h5> <div class="card-body"> You can put anything you want inside of these side widgets. They are easy to use, and feature the new Bootstrap 4 card containers! </div> </div> </div> </div> <!-- /.row --> </div> <!-- /.container --> {% endblock %} urls.py from django.urls import path, include from . import views urlpatterns = [ path('',views.main, name = 'main'), path('viewpost/<int:pk>/', views.viewpost, name = 'viewpost'), path('search/', views.search, name = 'search'), ] search.html {% extends 'blog/layout.html' %} {% load static %} {% block body %} <div class="card" style="width: 70vw; margin-left: auto; margin-right: auto; margin-top: 20px; ;"> <div class="card-body"> <h2 class="card-title">Search Results:</h2> {% for post in posts %} <h3 class="card-subtitle" style="margin-top: 30px;"><a href="#}" >{{ post.title }}</a></h3> <p class="card-text" style="margin-left: 1px;">{{ post.description }}</p> <hr> {% endfor %} </div> </div> {% endblock %}
You need to change your views and template like this. Change button type tosubmit and you don't need csrf token in the GET request. <form action="{% url 'search' %}" method="GET"> <div class="input-group"> <input type="text" class="form-control" placeholder="Search for..." name="search"> <span class="input-group-append"> <button class="btn btn-secondary" type="submit">Go</button> </span> </div> </form>** Now in the views First get the search query parameter then filter all the posts containing that query using __icontains and return the results to the template in a context def search(request): search = request.GET.get ('search', '') posts = Post.objects.filter(title__icontains=search) return render(request, "blog/search.html", {'posts':posts})
Django bootstrap modal not showing
I am trying to show a modal on a button click, however, after I click on the modal it does not show the modal, I do not know what seems to be the issue since I followed the exact tutorial on bootstrap and W3Schools. Here, is my template: {% for comment in comments %} <div class="border-bottom"> <div class="row pt-1 pl-4"> <div class="col-xs"> <img class="img-create-post rounded-circle mr-1" style="width: 2.1rem;height: 2.1rem;" src="https://mdbootstrap.com/img/Photos/Avatars/avatar-5.jpg" alt="Profile image"> </div> <div class="col-xs" style="margin-bottom: 0;"> <span class="text-dark font-size-smaller" href="#" style="font-weight: 500;">{{ comment.name.first_name }}</span> <span class="text-muted small">•</span> <a class="text-muted small" href="#">#{{ comment.name.username }}</a> <span class="text-muted small">•</span> <span class="text-muted small">{{ comment.get_created_on }}</span> <p class="font-weight-light pl-1">{{ comment.body }}</p> </div> </div> <div class="d-flex justify-content-between"> <div> <span class="text-muted small view-replies">view {{ comment.replies.count }} replies <i class="fas fa-caret-down"></i></span> </div> <div> <!-- button to show modal --> <button class="btn btn-sm small float-right text-muted button-outline-light reply-btn" type="button" data-toggle="modal" data-target="modal-comment-reply">Reply</button> </div> </div> <div class="comment-replies"> {% for reply in comment.replies.all %} <div class="row pt-1 pl-4"> <div class="col-xs"> <img class="img-create-post rounded-circle mr-1" style="width: 2.1rem;height: 2.1rem;" src="https://mdbootstrap.com/img/Photos/Avatars/avatar-5.jpg" alt="Profile image"> </div> <div class="col-xs" style="margin-bottom: 0;"> <span class="text-dark font-size-smaller" href="#" style="font-weight: 500;">{{ comment.name.first_name }}</span> <span class="text-muted small">•</span> <a class="text-muted small" href="#">#{{ comment.name.username }}</a> <span class="text-muted small">•</span> <span class="text-muted small">{{ comment.get_created_on }}</span> <p class="font-weight-light pl-1">{{ comment.body }}</p> </div> </div> {% endfor %} </div> </div> <div class="modal fade" id="modal-comment-reply"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <p class="font-size-smaller">{{ request.user.first_name }} replying to {{ comment.name.username }}</p> </div> <div class="modal-body"> <form method="POST" action="{% url 'home:post-detail' post.guid_url %}" class="post-comment-form" style="height: 1rem;"> {% csrf_token %} <input type="hidden" name="comment_id" value="{{ comment.id }}"/> {{ form }} <button class="btn btn-sm small btn-outline-primary ml-1" style="border-radius: 20px;" type="submit">Reply</button> <button type="button btn-sm small btn-outline-secondary" class="btn btn-secondary" data-dismiss="modal">Close</button> </form> </div> </div> </div> </div> {% empty %} <div class="d-flex justify-content-center"> <p class="font-weight-lighter text-muted">No comments to show</p> </div> {% endfor %} <style> .view-replies:hover { cursor: pointer; text-decoration: underline; } </style> <script> $(document).ready(function () { $('.reply-btn').on("click", function () { $('#modal-comment-reply').modal("show"); }); }) </script> I have even tried jQuery to open it but it does not seem to open, my other modals work fine I do not know why this does not open. Thanks for all the help in advance! EDIT: SOLVED - Typo in jquesry function!
Have liquid syntax include an additional liquid syntax?
I am trying to refactor my site using the D.R.Y. method, part of that is using liquid syntax to help. Currently everything works with what I have here: page-internal.html --- layout: default <div> <div class="d-flex" style="background-color: #e9ecef;"> <div class="jumbotron mx-auto mb-0 py-3 px-5" style="max-width: 1200px"> <div class="col-lg-12 p-3 mx-auto"> <img width="50" height="50" class="rounded-circle float-left mr-2" src="/assets/img/internal/{{ page.image }}" /> <h1 class="display-4">{{ page.title | escape }}</h1> <p class="lead">{{ content }}</p> </div> </div> </div> <div> <div class="d-flex justify-content-center"> {% include card-post-{{ page.passname }}.html %} </div> </div> </div> The {{ page.passname }} pulls from a .md file like so: --- layout: page-internal title: User Interface permalink: /pages/design-ui image: ui.svg passname: ui --- That works just fine, too. But then I have to create several pages to pull from instead of just referencing passname to grab the right .md page (I hope I'm making sense here, apologies if I'm not). That page looks like this card-post.ui.html And the html on that page is: <div class="container-fluid"> <div class="col-lg-12 mx-auto row d-flex justify-content-center mt-3" style="max-width: 1400px"> {% for post in site.categories.ui %} <div class="card col-sm-12 col-lg-3 m-2"> <div class="card-body d-flex flex-column"> <div class="media"> <div class="d-flex mr-3"> <a href="{{ post.url }}"> <img width="40" height="40" class="rounded-circle" src="/assets/img/{{ post.image }} " alt="{{ post.title }}" /> </a> </div> <div class="media-body"> <h6 class="mb-1">{{ post.title }}</h6> </div> </div> <div class="d-flex flex-column" style="height: 105px;"> <div class="p-2"> <p class="text-muted">{{ post.excerpt }}</p> </div> </div> <div class=" flex-column align-items-end"> <button type="button" class="btn btn-secondary btn-sm btn-block" onclick="location.href = '{{ post.url }}';">View project</button> </div> </div> </div> {% endfor %} </div> </div> What I'd like to do is take the two html sites and have it like this: --- layout: default --- <div> <div class="d-flex" style="background-color: #e9ecef;"> <div class="jumbotron mx-auto mb-0 py-3 px-5" style="max-width: 1200px"> <div class="col-lg-12 p-3 mx-auto"> <img width="50" height="50" class="rounded-circle float-left mr-2" src="/assets/img/internal/{{ page.image }}" /> <h1 class="display-4">{{ page.title | escape }}</h1> <p class="lead">{{ content }}</p> </div> </div> </div> <div> <div class="d-flex justify-content-center"> {% include card-post-{{ page.passname }}.html %} <div class="container-fluid"> <div class="col-lg-12 mx-auto row d-flex justify-content-center mt-3" style="max-width: 1400px"> {% for post in site.categories.ui %} <div class="card col-sm-12 col-lg-3 m-2"> <div class="card-body d-flex flex-column"> <div class="media"> <div class="d-flex mr-3"> <a href="{{ post.url }}"> <img width="40" height="40" class="rounded-circle" src="/assets/img/{{ post.image }} " alt="{{ post.title }}" /> </a> </div> <div class="media-body"> <h6 class="mb-1">{{ post.title }}</h6> </div> </div> <div class="d-flex flex-column" style="height: 105px;"> <div class="p-2"> <p class="text-muted">{{ post.excerpt }}</p> </div> </div> <div class=" flex-column align-items-end"> <button type="button" class="btn btn-secondary btn-sm btn-block" onclick="location.href = '{{ post.url }}';">View project</button> </div> </div> </div> {% endfor %} </div> </div> </div> </div> </div> This would work however the syntax here: {% for post in site.categories.ui %} Needs to be (and this is where I can't figure out what to do) {% for post in site.categories. {{ page.passname }} %} This throws an error: Liquid Warning: Liquid syntax error (line 23): Unexpected character { in "post in site.categories.{{ page.passname }}" in /_layouts/page-internal.html So my question is, how can I get the passname from said .md post (in this instance it'd be design-ui.md ) and put it into {% for post in site.categories.ui %} where the word ui would be dependint on the .md I hope I said all this right, apologies if not.
Your loop syntax {% for post in site.categories. {{ page.passname }} %} is incorrect : You may reach your category with bracket notation : {% for post in site.categories[page.passname] %}