Cant go through list using with - django

In my template, i have:
{% with x=0 %}
<div class="wow fadeInUp content-works">
<span class="text-title center" style="color:white">Previous Orders</span>
<div class="card blue-grey darken-1">
<div class="card-content white-text">
<span class="card-title">Order #</span>
<p>${{ payment.}} - {{ user.first_name }} - {{ user.last_name }}</p>
</div>
<div class="card-action">
Link to somewhere
</div>
</div>
</div>
{% endwith %}
Payment.x in this wont output anything (its a filtered database) but when i do payment.0 or payment.1, etc, it shows the data. In the end, i want to put this in a loop so it displays all the data from payment from 0 to the number of values in the filter. Any ideas why this doesnt work?

Fixed - iterated with a for loop:
<div class="wow fadeInUp content-card" style="margin-top: 0;">
<span class="text-title center" style="color:black">Previous Orders</span>
<table class="highlight">
<thead>
<tr>
<th data-field="id">Price</th>
<th data-field="name">Date</th>
<th data-field="price">Name</th>
<th data-field="quantity">Quantity</th>
</tr>
</thead>
<tbody>
{% for payment in b.latest_event_list %}
<tr>
<td>${{payment}}</td>
<td>{{payment.date}}</td>
<td>{{payment.event}}</td>
<td>{{payment.quantity}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
my views:
def dashboard(request):
if request.user.is_anonymous():
messages.add_message(request, messages.ERROR, ' Please Login First before accessing the Dashboard')
return redirect("/student/login")
user = request.user
temp = Payment.objects.filter(user_id=user.id)
tempint = len(temp)
a = Payment.objects.order_by('-id')[:tempint]
a = a[::-1]
b = {'latest_event_list': a}
print a
return render(request, 'student/dashboard.html', {'b': b})
(Put in a table format using materialize to look cleaner)

Related

problem of django form in posting request

I'm working on a project and I made a form but when I submit sth it doesn't send it to database although in cmd it shows that request method is post.I really don'tkhow how to deal with it.
thats template code:
{%extends 'base.html'%}
{%block title%}
<title>projects</title>
{%endblock title%}
{%block content%}
<div class="container">
</br>
<form method="post">
{% csrf_token %}
<div class="form-group">
<input type="text" class="form-control" name="project" placeholder="new project?">
</div>
<button type="submit" class="btn btn-primary">add</button>
</form>
</br>
</br>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col"> project-title</th>
<th scope="col">update</th>
<th scope="col">delete</th>
<th scope="col">divide</th>
</tr>
</thead>
<tbody>
{% for obj in all_projects %}
<tr>
<td>{{ obj.proTitle }}</td>
<td>{{ obj.done }}</td>
<td>Delete</td>
<td>Devide</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{%endblock content%}
any advise will be greatly appreciated:))
I solved it ;
in the input tag i put name="project" but the attribute that I defined in my model was "proTitle"
so when I chnged it the problem solved.

how to pass the selected check box id of a input tag to views in django and bulk update the selected rows in table

** 1.how to pass the selected check box id of a input tag(which is in for loop, so the name and value of input tag are dynamic ) to views in django and bulk update the selected rows of table in django**
<div class="jumbotron">
<form method="post" action="">
{% csrf_token %}
{{form|crispy}}
<input class = "btn btn-primary" type="submit" value="Search">
</form>
<h3 > Model values </h3>
<ul>
<table class="table" id="tab1">
<thead>
{% if teams %}
<tr>
<th>Select</th>
<th>#</th>
<th><b>Logo</b> </th>
<th><b> Team </b></th>
</tr>
</thead>
<tbody>
{% for value in models %}
<tr>
<td><input name="lol" type = "checkbox" value = "{{value.id}}"/> </td>
<td> {{forloop.counter}}</td>
<td> <img class="w3-display-topmiddle w3-container" src="{{ value.logUri.url }}" alt="alt txt" height="910" width="910"></td>
<td> {{ value.name }} </td>
</tr>
{% endfor %}
</tbody>
</table>
**realized that since i am not keeping input tag in form tag the input tag's name values is not captured by request.POST(now changes are dome in html as below ),later in django views i removed the unnecessary keysvalues using dict.pop() and created a list containing on primary keys
in django orm (model.filter(id__in= lst_of_id_captured).update(field='somevalue') **
<form method="post" action="">
{% csrf_token %}
{{form|crispy}}
<h3 > Model values </h3>
<ul>
<table class="table" id="tab1">
<thead>
{% if teams %}
<tr>
<th>Select</th>
<th>#</th>
<th><b>Logo</b> </th>
<th><b> Team </b></th>
</tr>
</thead>
<tbody>
{% for value in models %}
<tr>
<td><input name="lol" type = "checkbox" value = "{{value.id}}"/> </td>
<td> {{forloop.counter}}</td>
<td> <img class="w3-display-topmiddle w3-container" src="{{ value.logUri.url }}" alt="alt txt" height="910" width="910"></td>
<td> {{ value.name }} </td>
</tr>
{% endfor %}
</tbody>
</table>
<input class = "btn btn-primary" type="submit" value="Search">
</form>

Using a different counter from different object_list than the one used in for_loop

I want to count the objects in my template, but since I am using pagination to divide them into pages, I want to use counter from different object_list than the one used in the for loop.
This is my template:
{% extends 'base.html' %}
{% block content %}
{% if user.is_superuser %}
<div class="row">
<div class="col-sm-4 offset-sm-4">
<form class="form-inline md-form form-sm" method="GET" action="">
<i class="fa fa-search" aria-hidden="true"></i>
<input class="form-control form-control-sm ml-3 w-75" type="text" placeholder="搜索货品 Znajdź część" name="q" value="{{ request.GET.q }}"/>
</form>
</div>
</div>
<br/>
<hr/>
<div class='row'>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Image</th>
<th scope="col">Product name</th>
<th scope="col">Price (cost)</th>
<th scope="col">Price (PL)</th>
</tr>
</thead>
<tbody>
{% for product in objects %}
<tr>
<th scope="row">{{forloop.counter}}</th>
<td><img class="img img-fluid rounded" width="200" height="136" src="{{ product.photo.url }}"></td>
<td>{{product.product_name}}</td>
<td>{{product.price}}</td>
<td>{{product.price_pol}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="col-sm-4 offset-sm-4" align="center">
<div class="pagination">
<span class="step-links">
{% if objects.has_previous %}
« first
previous
{% endif %}
<span class="current">
Page {{ objects.number }} of {{ objects.paginator.num_pages }}.
</span>
{% if objects.has_next %}
next
last »
{% endif %}
</span>
</div>
</div>
{% endif %}
{% endblock content %}
My views.py:
Here I would like to use counter from object_list and not object
(Note the context in productview)
#login_required
def productview(request):
object_list = Product.objects.all().order_by('-date')
query = request.GET.get('q')
if query:
object_list = object_list.filter(
Q(product_name__icontains=query)|
Q(date__icontains=query)
).distinct()
paginator = Paginator(object_list, 5)
page = request.GET.get('page')
objects = paginator.get_page(page)
return render(request, 'products_list.html', {'objects': objects, 'object_list':object_list})
I wanted to use the nested loop, but it didn't work out. Is there another way that I don't know of? If I leave it like this every time I click nextpage the counter will start from "1". I would like the counter to continue when I click the next page...
You can enumerate you obect_list:
def productview(request):
object_list = Product.objects.all().order_by('-date')
query = request.GET.get('q')
if query:
object_list = object_list.filter(
Q(product_name__icontains=query)|
Q(date__icontains=query)
).distinct()
object_list =list(enumerate(object_list))
paginator = Paginator(object_list, 5)
page = request.GET.get('page')
objects = paginator.get_page(page)
return render(request, 'products_list.html', {'objects': objects, 'object_list':object_list})
Then the structure of the objects has changed: product.0 will contain the counter and product.1 will contain the model instance:
{% for product in objects %}
<tr>
<th scope="row">{{product.0}}</th>
<td><img class="img img-fluid rounded" width="200" height="136" src="{{ product.1.photo.url }}"></td>
<td>{{product.1.product_name}}</td>
<td>{{product.1.price}}</td>
<td>{{product.1.price_pol}}</td>
</tr>
{% endfor %}

Taking Attendance in Django Python

I am trying to mark attendance. But I am not able to get the code. My submit should send the input to database for each student. Any suggestion for doing this in better way is welcome.
My models.py is as follows
It has Employee, Mentor and Student and below is the model where values can be changed.
class ClassName(models.Model):
class_name = models.CharField(max_length=20)
class_date=models.DateField(blank=True, null=True)
Mentor= models.ForeignKey(Mentor, related_name='class_mentor')
student = models.ManyToManyField(Student, related_name='class_student')
attendance = models.BooleanField(default=True)
def __str__(self):
return str(self.class_name)
Views.py : This is just to display the markattendance page. I am not able to figure out how to get the value and Post the value to database. Right now, I am fetching Students value from the Students table. I think I should be creating Classname Form and and display students. I was getting Internal server error.
def markattendance(request):
students = Student.objects.all()
return render(request, 'home/markattendance.html',
{'students': students})
markattendance.html : On clicking submit, the attendance should be marked.
{% extends 'home/index.html' %}
{% block content %}
<div class="container-fluid">
<div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Class</th>
<th>Attendance</th>
</tr>
</thead>
<tbody>
{% for students in students %}
{% ifequal students.Men_name|stringformat:"s" user.username %}
<tr>
<td>{{ students.Student_name }}</td>
<td>{{ students.Student_Class }}</td>
<td>
<label class="switch">
<input id="toggle-slider_position()" type="checkbox">
<span class="slider round"></span>
</label>
</td>
</tr>
{% endifequal %}
{% endfor %}
</tbody>
</table>
</div>
<br/>
<div class="row" >
<div class="col-sm-4"></div>
<div class='col-sm-4' align="center">
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<button class="btn btn-lg">Submit</button>
</div>
<div class="col-sm-4">
</div>
</div>
<br/><br/><br/><br/><br/>
</div>
</div>
</div>
{% endblock %}
Please help me out. Any suggestion is welcome.

Django can't populate table rows on loop every x seconds with jquery post

I want to repopulate my table every few seconds to see what clients are doing on our servers. I'm new to Django but I've managed to stand this project up on a remote server through SSL. I need to request this information and then send back the list for table. I've gotten 404 forbidden errors, errors related to not being able to find the function, ect. Currently, I have the information being fetched but its refreshing the entire page rather than the table rows.
This is obviously because I was using:
**setInterval(function() {
document.getElementById('jobs').submit()
}, 5000);**
Which I'm trying to avoid
Views.py (part of it)
class ActiveServerJobsView(LoginRequiredMixin,generic.ListView):
template_name = 'project_name/active_server_jobs.html'
context_object_name = 'current_job_list'
def post(self, request):
if request.method == "POST":
db_default = connections['default']
default_cursor = db_default.cursor()
default_cursor.execute("select server_name from table_name where server_is_database = True")
host_list = default_cursor.fetchall()
current_job_list = get_current_jobs(host_list)
args = {'current_job_list': current_job_list}
return render(request,'project_name/active_server_jobs.html',args)
def get_queryset(self):
db_default = connections['default']
default_cursor = db_default.cursor()
default_cursor.execute("select junk, and, stuff from table_name where server_is_database = True")
host_list = default_cursor.fetchall()
return get_current_jobs(host_list)
def get_current_jobs(host_list):
current_jobs = []
for host in host_list:
host_name = host[0]
host_db = connections[host_name + "/example"]
host_cursor = host_db.cursor()
#get file jobs
host_cursor.execute("select junk, and, stuff")
host_file_list = host_cursor.fetchall()
for host_file in host_file_list:
junk, and, stuff = host_file
current_jobs.append(something))
#get report jobs
host_cursor.execute("select junk, and, stuff")
host_file_list = host_cursor.fetchall()
for host_file in host_file_list:
junk, and, stuff = host_file
current_jobs.append(Current_Jobs_On_Server.create(junk, and, stuff))
return current_jobs
template/project_name/active_server_jobs.html
setInterval(function() {
document.getElementById('jobs').submit()
}, 5000);
{% extends 'base.html' %}
{% load static %}
{% load app_filters %}
<html>
{% block content %}
<link rel="stylesheet" type="text/css" href="{% static 'project_name/template/index_style.css' %}">
<body>
<div id="header">
</div>
<center class="container" style="margin-top: 5%;">
<div class="panel panel-default">
<div class="panel-heading">
<h1 class="panel-title"><strong>Working Jobs</strong></h1>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div style="overflow: auto;">
<div class="table-wrapper-scroll-y">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Server</th>
<th scope="col">junktitle</th>
<th scope="col">junktitle</th>
<th scope="col">junktitle</th>
<th scope="col">junktitle</th>
<th scope="col">junktitle</th>
<th scope="col">junktitle</th>
<th scope="col">junktitle</th>
<th scope="col">junktitle</th>
</tr>
</thead>
<tbody>
<form id="jobs" action="active_server_jobs" method="post">
{% csrf_token %}
{% if current_job_list %}
{% for job in current_job_list %}
<tr>
<th scope="row">{{forloop.counter}}</th>
<th>{{job.junk}}</td>
<td>{{job.junk1}}</td>
<td>{{job.junk2}}</td>
<td>{{job.junk3}}
<td>{{job.junk4}}</td>
<td>{{job.junk5}}</td>
<td>{{job.junk6}}</td>
<td>{{job.junk7}}</td>
<td>{{job.junk8}}</td>
</tr>
{% endfor %}
{% else %}
<h3><strong>There are currently no working jobs</strong></h3>
{% endif %}
</form>
</tbody>
</table>
</div>
</div>
</div>
</div>
</center>
</body>
{% endblock %}
</html>
Any help is greatly appreciated! I'm amazed I've gotten this far but I'm now stumped on an ajax request SMH