Django - AttributeError at / 'tuple' object has no attribute 'get' - django

I am getting an attribute error at:
Exception Location: C:\Users\user\Desktop\django-basics\env\lib\site-packages\django\middleware\clickjacking.py, line 26, in process_response
This is the said location (the process_response function):
def process_response(self, request, response):
# Don't set it if it's already in the response
if response.get('X-Frame-Options') is not None:
return response
My views file
from django.shortcuts import render
from .models import Student
def studentlist(request):
get_students = Student.objects.all()
data = {
'get_students' : get_students
}
return render(request, 'studentlist.html', data)
My templates file studentlist.html:
{% load static %}
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" />
<title>Student List</title>
</head>
<body>
<div class="container">
<h2 class="text-center mt-5">Student List</h2>
<table class="table table-hover table-dark">
<thead>
<tr>
<th scope="col">Roll No.</th>
<th scope="col">Photo</th>
<th scope="col">Full Name</th>
<th scope="col">Gender</th>
<th scope="col">Course</th>
<th scope="col">Grade in course</th>
</tr>
</thead>
<tbody>
{% for student in get_students %}
<tr>
<td>{{ student.roll_no }}</td>
<td>
<img src="{{ student.photo.url }}" width="40" height="40"/>
</td>
<td>{{ student.full_name }}</td>
<td>{{ student.course }}</td>
<td>{{ student.grade }}</td>
<td>{{ student.gender }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
Please let me know if I need to add more info. Thanks in advance.

if someone has the same issue, the answer from Willem van Onsem was the one that fixed my issue, I had a coma at the end.

Related

Flask-security delete user return an error

I'm trying to add a delete button on my users admin page in a flask app.
But I already have this error when I click on button :
sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.str' is not mapped
This is my adminusers.html file :
{% extends 'base.html' %}
{% block main %}
<main>
<table class="table table-striped users">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Roles</th>
<th scope="col" class="text-center">Delete</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<th scope="row">{{ user.id }}</th>
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>
{% for role in user.roles %}
{{role.name}};
{% endfor %}
</td>
<td style="text-align: center;"></i></td>
</tr>
{% endfor %}
</tbody>
</table>
</main>
{% endblock main %}
And my app.py file :
#app.route('/adminusers')
def list_users():
users= User.query.all()
return render_template('adminusers.html', users=users)
#app.route('/delete_user/<user>')
def delete_user(user):
user_datastore.delete_user(user=user)
db.session.commit()
return redirect(url_for('adminusers'))
I'm trying to use the 'email' or 'name' but it already return an error
Thanks #pjcunningham, like that it works :
#app.route('/delete_user/<id>')
def delete_user(id):
user = user_datastore.get_user(id)
user_datastore.delete_user(user)
db.session.commit()
return redirect(url_for('adminusers'))

How to generate PDF in Django Generic Views?

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.

Flask - jinja2.exceptions.UndefinedError: 'employee' is undefined

I have a page that displays Employees Masterlist and I wanted to do is when I clicked the Employee No. it will view the timesheet of that employee.
I tried using this as the result of my research.
In my routes.py
#app.route("/employee/<int:employee_id>")
def employee(employee_id):
employee = Employee.query.get_or_404(employee_id)
return render_template('emp_timesheet.html', employee=employee)
Here is my masterlist.html
{% extends 'header.html' %}
{% block content %}
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Employee Number</th>
<th>Name</th>
<th>Contract</th>
<th>Position</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
{% for employee in list_employee %}
<tr class="odd gradeX">
<td>{{ employee.employee_number }}</td>
<td>{{ employee.last_name }}, {{ employee.first_name }}</td>
<td>{{ employee.contract }}</td>
<td class="center">{{ employee.title }}</td>
<td class="center">{{ employee.salary }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
But I'm getting this error:
File "/home/jxmtsi/Desktop/flask_training/rep/templates/header.html", line 293, in top-level template code
{% block content %}{% endblock %}
File "/home/jxmtsi/Desktop/flask_training/rep/templates/masterlist.html", line 27, in block "content"
<td>{{ employee.employee_number }}</td>
File "/home/jxmtsi/.local/lib/python3.6/site-packages/jinja2/environment.py", line 430, in getattr
return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'employee' is undefined
I think, that a problem is in your Python code, more precisely in a communication between your application and SQL base. Or, maybe, you do not have in SQL database a data about requested employee_id.
Check, does variable employee in method employee (employee_id) is receiving data from your database. For example, try print(employee.last_name):
#app.route("/employee/<int:employee_id>")
def employee(employee_id):
employee = Employee.query.get_or_404(employee_id)
print(employee.last_name) # or print other value from table (Model) Employee
# print(employee.id)
return render_template('emp_timesheet.html', employee=employee)

How to display 2 queryset list Frond end?(Django ListView get_queryset)

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.

django trying to put a table in a view

in my views.py:
def show(request):
query_results=Emails.objects.all()
#from py_utils import open_py_shell;open_py_shell.open_py_shell()
context = { 'query_result' : query_results }
return render(request, 'show.html', context)
in show.html:
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
{% for item in query_result %}
<tr>
<td>Ohad</td>
<td>{{ item.email }}</td>
<td>{{ item.baseurl }}</td>
</tr>
{% endfor %}
</tbody>
</table>
an email object has email field and baseurl field but it seems that i cannot load it,
i checked and in query_result i have a list of Emails object just cannot put it in the table, any help?