django trying to put a table in a view - django

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?

Related

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

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.

Jinja2 - Loop over list to build a table

I am quite new to Flask and I am having a hard time to understand why I am only getting a list of elements in my browser (single column), I would like to get 3 different columns and my data is correct:
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Mail</th>
<th>Delete</th>
</tr>
{% for n in customers %}
<tr>
<td>{{n['First Name']}} </td>
</tr>
<tr>
<td>{{n['Last Name']}}</td>
</tr>
<tr>
<td>{{ n['Phone']}}</td>
</tr>
<tr>
<td> Supprimer <td></td>
</tr>
{% endfor %}
</table>
What you are really looking for is something like this:
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Mail</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{% for n in customers %}
<tr>
<td>{{n['First Name']}} </td>
<td>{{n['Last Name']}}</td>
<td>{{ n['Phone']}}</td>
<td> Supprimer <td></td>
</tr>
{% endfor %}
</tbody>
</table>
tr Stands for Table Row. Check out this link to learn a bit more about tables

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 i can fill my data on just one bootstrap table django 2.1

my table looks like
date name place
4.09.2018 jack London
date name place
4.09.2018 ed paris
date name place
5.09.2018 sam istabul
i have problem with using table. i want to show one table my data but , it doesnt work, my program showing all my data different table but i dont want this. how i repair this situation ?
i want to show one header and all my data below from header like this
- date name place
4.09.2018 jack London
4.09.2018 ed paris
5.09.2018 sam istabul
but this situation adding header for all of my data. it shows here
my html codes
thank u for your interest...
<div class="container">
<div class="row">
<div class="col-3">
<a class="btn btn-warning" href="{% url 'ANASAYFA'%}"> ANASAYFA </a>
<br>
{% now "jS F Y H:i" %}
</div>
<div class="col-6">
{% for i in veriler %}
<table class="table table-bordered table-dark">
<thead>
<tr>
<th scope="col">TARİH</th>
<th scope="col">İSİM-SOYİSİM</th>
<th scope="col">VARDİYA BÖLGE</th>
<th scope="col">VARDİYA DÖNEM</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="i.row">{{ i.gun }}</th>
<th>{{ i.personel }}</th>
<th>{{ i.bolge }}</th>
<td>{{ i.vardiya_donemi }}</td>
</tr>
</tbody>
</table>
{% endfor %}
Try this:
<table class="table table-bordered table-dark">
<thead>
<tr>
<th scope="col">TARİH</th>
<th scope="col">İSİM-SOYİSİM</th>
<th scope="col">VARDİYA BÖLGE</th>
<th scope="col">VARDİYA DÖNEM</th>
</tr>
</thead>
<tbody>
{% for i in veriler %}
<tr>
<th scope="i.row">{{ i.gun }}</th>
<th>{{ i.personel }}</th>
<th>{{ i.bolge }}</th>
<td>{{ i.vardiya_donemi }}</td>
</tr>
{% endfor %}
</tbody>
</table>

Is there any way to save and HTML table including forms without creating a model in Django?

I have created a table with django forms in it. The forms do get data from the database, from two different models. Since this form has 42 fields consisting of 7 days and 6 shifts, i would love to save it as the table it is rendered. Is there any way to do this?
forms.py
class EditSchedule(forms.Form):
def __init__(self,*args,**kwargs):
super(EditSchedule, self).__init__(*args,**kwargs)
for k in range(1,8):
for i in range(1,7):
self.fields["S"+str(i)+"D"+str(k)] = forms.ChoiceField(choices=get_my_choices(i,k))
self.fields["S"+str(i)+"D"+str(k)].widget.attrs.update({"class":"form-control select2 select2-hidden-accessible"})
html file
<div class="box">
<form method="POST" action="">{% csrf_token %}
<div class="box-body">
<div class="table-container table-responsive">
<table class="table table-bordered table-hover dataTable" role="grid" >
<thead>
<tr>
<th class = "shicht"><h3>Schicht</h3></th>
<th class = "montag"><h3>Montag</h3></th>
<th class = "dienstag"><h3>Dienstag</h3></th>
<th class = "mittwoch"><h3>Mittwoch</h3></th>
<th class = "donnerstag"><h3>Donnerstag</h3></th>
<th class = "freitag"><h3>Freitag</h3></th>
<th class = "samstag"><h3>Samstag</h3></th>
<th class = "sonntag"><h3>Sonntag</h3></th>
</tr>
</thead>
<tbody>
<tr class="even">
<td class="shicht">Schicht 1</td>
<td class="montag">{{ form.S1D1 }}</td>
<td class="dienstag">{{ form.S1D2 }}</td>
<td class = "Mittwoch">{{ form.S1D3 }}</td>
<td class = "donnerstag">{{ form.S1D4 }}</td>
<td class = "freitag">{{ form.S1D5 }}</td>
<td class ="samstag">{{ form.S1D6 }}</td>
<td class ="sonntag">{{ form.S1D7 }}</td>
</tr>
<tr class="odd">
<td class="shicht">Schicht 2</td>
<td class="montag">{{ form.S2D1 }}</td>
<td class="dienstag">{{ form.S2D2 }}</td>
<td class = "Mittwoch">{{ form.S2D3 }}</td>
<td class = "donnerstag">{{ form.S2D4 }}</td>
<td class = "freitag">{{ form.S2D5 }}</td>
<td class ="samstag">{{ form.S2D6 }}</td>
<td class ="sonntag">{{ form.S2D7 }}</td>
</tr>
<tr class="even">
<td class="shicht">Schicht 3</td>
<td class="montag">{{ form.S3D1 }}</td>
<td class="dienstag">{{ form.S3D2 }}</td>
<td class = "Mittwoch">{{ form.S3D3 }}</td>
<td class = "donnerstag">{{ form.S3D4 }}</td>
<td class = "freitag">{{ form.S3D5 }}</td>
<td class ="samstag">{{ form.S3D6 }}</td>
<td class ="sonntag">{{ form.S3D7 }}</td>
</tbody>
</table>
</div>
</div>
<input type="submit" value="Save">
You can use raw queries for this.
For capture the form filds you can use the "clean" methods in the base form class see forms validation