Im currently trying to figure out how to autonumber my tables in my html template.
I can't seem to use the id in the database as it is not autoincrementing as it it is user dependant what they see, so the user would see not a correct ordering. Instead of seeing the table numbering go 1. 2. 3. it is 4. 8. 9. for example.
Originally i have tried this, which gave me the wrong outcome as described above:
template.html
{% for item in x%}
<tr>
<td>{{ item.id }}</td>
<td>{{ item.a}}</td>
<td>{{ item.b}}</td>
<td>{{ item.c}}</td>
</tr>
I have tried something with a while loop:
template.html
{% for item in x %}
{% while z<= item %}
{% z=z+1 %}
<tr>
<td>{{ z }}</td>
<td>{{ item.a}}</td>
<td>{{ item.b}}</td>
<td>{{ item.c }}</td>
</tr>
{% endwhile %}
{% endfor %}
For your reference the model that these templates refer to:
models.py
from django.db import models
from django.conf import settings
class x(models.Model):
creation_date = models.DateTimeField(auto_now = True)
a = models.CharField(max_length=50)
b = models.CharField(max_length=50)
c = models.CharField(max_length=50)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
Thanks in advance for your help!
You can use forloop.counter to count the iterations of a loop. Try this:
{% for item in x %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ item.a}}</td>
<td>{{ item.b}}</td>
<td>{{ item.c}}</td>
</tr>
{% endfor %}
Reference: for template tag - Django Docs
Related
I have a project and a task model and I want to make a table in a detail html that displays the tasks in the project.
I've tried doing
<table>
<tr>
<th>Name</th>
<th>Assignee</th>
<th>Start Date</th>
<th>Due Date</th>
<th>Is compeleted</th>
</tr>
<tr>
<td>{{ task.name }} </td>
<td>{{ task.assignee }}</td>
<td>{{ task.start_date }}</td>
<td>{{ task.due_date }}</td>
<td>{{ task.is_completed }}</d>
</tr>
</table>
but it just shows the table headers and not its content
here is my task model
from django.db import models
from django.conf import settings
from django.urls import reverse
# Create your models here.
class Task(models.Model):
name = models.CharField(max_length=200)
start_date = models.DateTimeField()
due_date = models.DateTimeField()
is_completed = models.BooleanField(default=False)
project = models.ForeignKey(
"projects.Project",
related_name="tasks",
on_delete=models.CASCADE,
)
assignee = models.ForeignKey(
settings.AUTH_USER_MODEL,
null=True,
related_name="tasks",
on_delete=models.SET_NULL,
)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("show_my_tasks")
An answer to your previous question had something like:
{% for project in projects_list %}
...
{% if project.tasks.count > 0 %}
# Displaying the table in here with the project's task info...
{% else %}
<p> no tasks for this project </p>
{% endif %}
...
{% endfor %}
You should loop over all the project's tasks set within the table. For example:
# Within the true portion of the if statement...
{% if project.tasks.count > 0 %}
# Displaying the table in here with the project's task info...
<table>
<thead>
<tr>
<th>Name</th>
<th>Assignee</th>
<th>Start Date</th>
<th>Due Date</th>
<th>Is compeleted</th>
</tr>
</thead>
<tbody>
# The set of related tasks for the current project -> project.tasks.all
{% for task in project.tasks.all %}
<tr>
<td>{{ task.name }} </td>
<td>{{ task.assignee }}</td>
<td>{{ task.start_date }}</td>
<td>{{ task.due_date }}</td>
<td>{{ task.is_completed }}</d>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p> no tasks for this project </p>
{% endif %}
I am trying to access a Django related field value in a Django template:
{% for item in order %}
<tr>
<td>{{ item.id }}</td>
<td class="text-left">{{ item.created }}</td>
<td class="text-left">{{ item.payments__charge_status }}</td>
<td class="text-left">{{ item.shipping_method_name }}</td>
<td class="text-right">£{{ item.total_gross_amount }}</td>
</tr>
{% endfor %}
Actual queryset defined in views.py:
orders = Order.objects.values().filter(created__range=(start_date, end_date)).filter(Q(payments__charge_status="fully-charged") | Q(payments__charge_status="not-charged"))
Payment is in a different table and I can access using payments__charge_status in the view but not in the actual template.
Any help is appreciated.
Updated with models.py for Payment Model:
....
class Payment(models.Model):
order = models.ForeignKey(
Order, null=True, related_name="payments", on_delete=models.PROTECT
)
From your database modeling order can have multiple payments ( Many-to-One )so payments is a QuerySet you should iterate through
{% for payment in item.payments.all %}
{{ payment.charge_status }
{% endfor %}
I am trying to get url parameters like this:
<tbody>
{% for object in objects %}
<tr>
<td>{{ object.id }}</td>
<td>{{ object.user }}</td>
<td>{{ object.url }}</td>
<td>{{ object.minimum }}</td>
<td>{{ object.maximum }}</td>
<td>{{ object.requests }}</td>
<td>{{ object.stay }}</td>
<td class="text-Centre">
Run
</td>
</tr>
{% endfor %}
</tbody>
If I put my URL like this, it works since the view exists in view.py:
{% url 'trafficapp:generate_traffic' %}
Now, I am trying to take URL parameters with it, but I am unable to make how can I pass data from this button, and what should I put in my URL pattern for this. Kindly help, I am new to Django and still figuring out how it works.
you can add parameters after {% url %}
Run
I have table users that contain column username and entries (a one-to-many relationship with table entries). In my HTML file, I want to display the username and the number of entries from the particular user.
It looks like this:
{% for i in users %}
<tr>
<td>{{ i.username }}</td>
<td>{{ i.entries|length }}</td>
</tr>
{% endfor %}
I want to sort the rows from the user who has the biggest number of entries to the least.
I try this but it doesn't work:
{% for i in users|sort(entries|length) %}
How can I achieve this?
You should ideally create your query-set in you view and in your template, you can access it like so
{% for user in users|sort(attribute="length")
<tr>
<td>{{ user.username }}</td>
<td>{{ user.entries|length }}</td>
</tr>
{% endfor %}
Using length attribute on your item, you can do so with sort(attribute='length'). Taken from the Jinja2 sort filter documentation
I created an html table in a django template
<form action="/delete_team/" method="POST" >
{% csrf_token %}
<input type="submit" name = "delete" class="btn btn-danger float-right" value="Delete">
<table>
<thead>
<tr>
<th><input type="checkbox" class="checkAll" name="checkAll"></th>
<th>Team ID</th>
<th>Male</th>
<th>Female</th>
<th>Officer</th>
<th>Deadline</th>
</tr>
</thead>
<tbody>
{% for team in teams %}
<tr>
<td><input type="checkbox" name="checked" class="case"></td>
<td>{{ team.team_id}}</td>
<td>{{ team.male }}</td>
<td>{{ team.female }}</td>
<td>{{ team.officer }}</td>
<td>{{ team.date }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
here is the code that i wrote in my views.py:
def delete_team(request):
if request.method == "POST":
pkm = request.POST.getlist("checked")
selected_objects = mtch_tbl.objects.filter(team_id__in=pkm)
selected_objects.delete()
return HttpResponseRedirect("/main/")
now when i check a row and click delete nothing happens...i only get returned to my page again with the same data. Kindly point out my mistake, i can't figure out how to write the views
Here are my urls.py
from django.urls import path
from teamplanner import views
urlpatterns = [
...
path("delete_team/",views.delete_team),
]
Is it possible for the jQuery code to be interfering?
Your views have many codes that to me aren't necessary, if you're planning on deleting a team, it's just Normal that you've FIRST registered a team. So here's what you should do..
def delete_team(request,delete_id):
delete = mtch_tbl.objects.get(id=delete_id)
#to make it more interesting, you should add a view to your models (i,'ll display the model below)
delete.view=True
delete.delete()
return HttpResponseRedirect("/main/")
Add this to your models
Class modelname (models.Model):
view=models.BooleanField(default=False)
In your URLs here's what you should do:
urlpatterns = [
path("delete_team/(?<delete_id>\d+)/$",views.delete_team),
]''''
I hope this helps
I think you're storing it in a variable. Try this:
def delete_team(request):
if request.method == "POST":
pkm = request.POST.getlist("check")
mtch_tbl.objects.filter(team_id__in=pkm).delete()
return HttpResponseRedirect("/main/")
I figured out the answer after reading this S.O solution, it's not related to checkboxes but the idea is pretty much the same. All I did was edit my checkbox input tag a little and added values to it:
<tbody>
{% for team in teams %}
<tr>
<td><input type="checkbox" name="checked" class="case" value="{{ team.team_id }}"></td>
<td>{{ team.team_id}}</td>
<td>{{ team.male }}</td>
<td>{{ team.female }}</td>
<td>{{ team.officer }}</td>
<td>{{ team.date }}</td>
</tr>
{% endfor %}
</tbody>
that's all there was to it, no need to change the views or the url path.
Still I really appreciate the help, Thank You!