I have a template that is rendered by generic List View. I want to give a download link in front of each row of data in table. The download link will create a PDF file of respective rows data. Please tell me how to write a code for that?
Views.py
class BookingConfirmationListView(LoginRequiredMixin, generic.ListView):
model = container_booking
template_name = 'home/booking_confirmation_detail.html'
context_object_name = 'all_container'
def get_queryset(self):
return container_booking.objects.all()
Templates look like
<table class="table-striped table-hover table-bordered" width="100%">
<tr>
<th>Date</th>
<th>Source</th>
<th>Destination</th>
<th>Container</th>
<th>Commodity</th>
<th>Agreed Rate</th>
<th>Edit</th>
<th>Delete</th>
<th>Status</th>
</tr>
{% for item in all_container %}
<tr>
<td>{{ item.date }} </td>
<td>{{ item.place_of_reciept }} </td>
<td>{{ item.final_place_of_destination }} </td>
<td>{{ item.equipment_type }}{{ item.quantity }} </td>
<td>{{ item.commodity }} </td>
<td>{{ item.agreed_rate }} </td>
<td><a href="{% url 'home:cont_bk-update' item.id %}" ><i class="fas
fa-edit"></i></a></td>
<td><a href="{% url 'home:cont_bk-delete' item.id %}" ><i class="fas
fa-trash"></i></a></td>
<td>{{ item.approved }}</td>
</tr>
{% endfor %}
</table>
urls.py
url(r'^cont_bkdetail$', views.BookingConfirmationListView.as_view(),
name='cont_bk-detail'),
url(r'^cont_bk/(?P<pk>[0-9]+)/$',
views.BookingConfirmationUpdate.as_view(), name='cont_bk-update'),
url(r'^cont_bk/(?P<pk>[0-9]+)/delete/$',
views.BookingConfirmationDelete.as_view(), name='cont_bk-delete'),
I want that whenever I click download, the PDF file of that row is generated.
Related
1 2 3
I'm trying to get data in the form of a table, but the data is not being fetched, I don't know how to get the record, I'm using cloud firestore to get the data.
In my code I have used {{buldings.building}} to get the record but this is not fetching the record from the firebase.
here is my table code for table
<div class="row">
<div class="col-lg-12 mb-4">
<!-- Simple Tables -->
<div class="card">
<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
<i class="fas fa-plus-circle "></i> Add Building
</div>
<div class="table-responsive">
<table class="table align-items-center table-flush" id="buildingList">
<thead class="thead-light">
<tr>
<th>BUILDING NAME</th>
<th>POSTAL CODE</th>
<th>CITY</th>
<th>STREET</th>
<th>HOUSE NO.</th>
<th>TOWN</th>
<th>ADDITIONAL INFO</th>
<th colspan="2">ACTION</th>
</tr>
</thead>
<tbody>
{% for building in buildings %}
<tr>
<td>{{ buildings.building }}</td>
<td>{{ buildings.postalCode }}</td>
<td>{{ buildings.city }}</td>
<td>{{ buildings.houseNo }}</td>
<td>{{ buildings.street }}</td>
<td>{{ buildings.town }}</td>
<td>{{ buildings.additionalInfo }}</td>
<td></i>Edit</p></td>
<td></i>Delete</p></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="card-footer"></div>
</div>
</div>
</div>
</div>
<!---Container Fluid-->
</div>
views.py file
def buildingMag(request):
context = {
'buildings': db.collection('Buildings').get()
}
return render(request,"EmployeeAdmin/buildingMag.html",context)
db.collection('Buildings').get() should return you a list of Firestore DocumentSnapshot which is sent to template context via buildings key. Looping through the buildings will let you populate the template(table) with each item on the list. Something like below.
In your views construct the dict from DocumentSnapshot and return the dict
views.py
buildings = db.collection('Buildings').get()
context = {
'buildings': [building.to_dict() for building in buildings]
}
template.html
{% for building_obj in buildings %}
<tr>
<td>{{ building_obj.building }}</td>
<td>{{ building_obj.postalCode }}</td>
<td>{{ building_obj.city }}</td>
...
...
</tr>
{% endfor %}
I am trying to delete severals rows at the same time in django.
How can I simply change the state of the booleanfield 'to_delete', just by clicking on the checkbox?
I am using datatables. The way how I am doing it is to create a boolean to_delete in my model, when the checkbox is selected, I am calling the function delete_multiple_company in my view. However, this doesn`t work. Any idea, what I am doing wrong please. Many Thanks,
I`ve created my view:
views.py
def delete_multiple_company(request, company_id):
company = get_object_or_404(Company, pk=company_id)
company = Company.objects.get(pk=company_id)
company.objects.filter(to_delete=True).delete()
return HttpResponseRedirect(reverse("company:index_company"))
urls.py
url(r'^(?P<company_id>[0-9]+)/delete_multiple_company/$', views.delete_multiple_company, name='delete_multiple_company'),
models.py
class Company(models.Model):
to_delete = models.BooleanField(default=False)
index.html
<span class="fa fa-plus"></span>Delete Companies
<table id="dtBasicExample" class="table table-striped table-hover">
<thead>
<tr>
<th>Select</th>
<th>#</th>
<th>Checked ?</th>
</tr>
</thead>
<tbody>
{% for company in companys.all %}
<tr>
<td id="{{ company.id }}"><input type="checkbox" class="companyCheckbox" name="checkedbox" id="{{ company.id }}" value="{{ company.id }}"></td>
<td>{{ company.id }}</td>
<td>{{ company.to_delete }}</td>
</tr>
{% endfor %}
</tbody>
</table>
I experienced a similar issue as you, what I did was put in a form.
index.py
<form method="POST" class="post-form">{% csrf_token %}
<button type="submit" class="save btn btn-default">Delete</button>
<span class="fa fa-plus"></span>Delete Companies</a>
<table id="dtBasicExample" class="table table-striped table-hover">
<thead>
<tr>
<th>Select</th>
<th>#</th>
<th>Checked ?</th>
</tr>
</thead>
<tbody>
{% for company in companys.all %}
<tr>
<td id="{{ company.id }}"><input type="checkbox" class="companyCheckbox" name="checkedbox" id="{{ company.id }}" value="{{ company.id }}"></td>
<td>{{ company.id }}</td>
<td>{{ company.to_delete }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="submit" class="save btn btn-default">Delete</button>
</form>
Clicking on the button then triggered the POST method in my view.
views.py
def index(request):
if request.method == 'POST':
print('I made it here')
# Put your code here, note you will return a dict, so some trial and error should be expected
I got 2 queryset list expired_item and queryset in Django ListView, but I don't know when item is expired(queryset is empty), how to display another list expired_item on frond end, no matter what I changed in abc.html, expired_item won't dispaly, I pasted my code as below:
class ABCListView(ListView):
model = ABC
ordering = ('name', 'skill_course')
context_object_name = 'abcs'
template_name = ''
def get_queryset(self, **kwargs):
# Omitted
......
......
# Omitted
expired_item = list(ABC.objects.filter(pk__in=aa).exclude(pk__in=z))
queryset = Permit.objects.filter(pk__in=z)
return queryset
And my html file of abc.html as below:
{% extends 'base.html' %}
{% block content %}
<nav aria-label="breadcrumb">
</nav>
<h2 class="mb-3">My Items list</h2>
<div class="card">
<table class="table mb-0">
<thead>
<tr>
<th>Name</th>
<th>Department</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
{% for a in abcs %}
<tr>
<td class="align-middle">{{ a.name }}</td>
<td class="align-middle">{{ a.department.get_html_badge }}</td>
<td class="align-middle badge badge-pill badge-danger">{{ a.status }}</td>
</tr>
{% empty %}
{% endfor %}
</tbody>
</table>
</div>
<h2 class="mb-3">My Expired Items list</h2>
<div class="card">
<table class="table mb-0">
<thead>
<tr>
<th>Name</th>
<th>Department</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
{% for b in expired_item %}
<tr>
<td class="align-middle">{{ b.name }}</td>
<td class="align-middle">{{ b.department.get_html_badge }}</td>
<td class="align-middle badge badge-pill badge-danger">{{ a.status }}</td>
</tr>
{% empty %}
{% endfor %}
</tbody>
</table>
</div>
<div class="card-footer">
{% endblock %}
Thanks so much for any advice!
I would suggest use a normal django view. This Generic ListView is just created for the use of one list. Just pass both querysets in your context and render your template with that.
You could also use get_context_data() but this would be more or less hacky and not the qay I would recommend.
code is:
def get_case():
...
table_step = zip(li_host,li_cmd)
return render_template('auto.html',table_step = table_step)
html is:
{% for row in table_step %}
<tr>
<td>{{ row[0]|safe }}</td>
<td>{{ row[1]|safe }}</td>
<td><button type="button" class="btn btn-default"> 编辑 </button></td>
</tr>
{% endfor %}
actual page shows no content:
First of all, I know there are lot of questions about accessing model in template but let me explain why this is different.
I want profile page where User can see their details. I am able to do that but with one little bug. If there are 3 Users(say A, B,C) and User A want to see his profile he sees it three time. Like this:
How do I stop the loop after one iteration so that the User only get's his profile information once.
This is my URL:
url(r'^profile/$', views.IndexView.as_view(), name='profile'),
Views.py:
class IndexView(generic.ListView):
template_name = 'profile.html'
def get_queryset(self):
return Profile.objects.all()
and profile.html:
{% if user.is_authenticated %}
{% for profile in object_list %}
<h1>{{ request.user }}'s Profile</h1>
<table>
<tr>
<td>Username:</td>
<td>{{ user.username }}</td>
</tr>
<tr>
<td>First Name:</td>
<td>{{ user.first_name }}</td>
</tr>
<tr>
<td>Last Name:</td>
<td>{{ user.last_name }}</td>
</tr>
<tr>
<td>Email:</td>
<td>{{ user.email }}</td>
</tr>
<tr>
<td>Personal Info:</td>
<td>{{ user.profile.personal_info }}</td>
</tr>
<tr>
<td>Job Title:</td>
<td>{{ user.profile.job_title }}</td>
</tr>
<tr>
<td>Department:</td>
<td>{{ user.profile.department }}</td>
</tr>
<tr>
<td>Location:</td>
<td>{{ user.profile.location }}</td>
</tr>
<tr>
<td>Expertise:</td>
<td>{{ user.profile.expertise }}</td>
</tr>
<tr>
<td>Phone Number:</td>
<td>{{ user.profile.phone_number }}</td>
</tr>
<tr>
<td>Contact Skype:</td>
<td>{{ user.contact_skype }}</td>
</tr>
<tr>
<td>Contact Facebook:</td>
<td>{{ user.contact_facebook }}</td>
</tr>
<tr>
<td>Contact Linkedin:</td>
<td>{{ user.profile.contact_linkedin }}</td>
</tr>
</table>
<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% include 'form-template.html' %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<a href={% url 'profile_edit' %}><input type="button" class = " col-sm-offset-2 btn bk-bs-btn-warning " name="cancel" value="Edit" /></a>
</div>
</div>
</form>
{% endfor %}
{% else %}
<h2>Please login to see your Profile</h2>
{% endif %}
I am a newbie to django, thanks is advance.
You're looping over all Profile's but not actually using this data in the loop, instead you're using user.profile.
This means that you likely have 3 Profile objects in the database, then for each you display the details of the current user, which isn't desirable.
So you can remove the loop entirely, and just keep its contents that refer to all the user.profile attributes to achieve the desired result.
edit
Looks like user is passed into your template by default, and points to the currently logged in user. So user.profile will return the profile, and this works without doing anything else. So user.profile.personal_info is valid and should work. When you tried to use profile directly, that is not the same thing, and wasn't defined, so profile.personal_info didn't work. Then in your loop you used profile to loop over Profile objects, but this wasn't necessary or was used. Hope this makes sense.