Display image in twig symfony from database - twig

I am trying to display an image from my database into a twig file but it doesn't work, the image is not displayed. I don't know where is the problem.
The image name is stored as a string in the database.
<tr class="">
<td>{{ boutique.getId() }}</td>
<td>{{ boutique.getNomBoutique() }}</td>
<td>{{ boutique.getNomResponsableBoutique() }}</td>
<td>{{ boutique.getAdresse() }}</td>
<td>{{ boutique.getContact() }}</td>
<td>{{ boutique.getFournisseur() }}</td>
<td>{{ boutique.getType() }}</td>
<td><img src="{{ asset('public/images/{{ boutique.getImage()}}') }}" class="img-rounded"/></td>
<td>View Order</td>
<td>Cancel</td>
</tr>

You wrote {{ asset('public/images/{{ boutique.getImage()}}') }}.
This won't work, you need to properly concatenate your strings like this:
{{ asset('public/images/' ~ boutique.getImage()) }}
You can also use string interpolation within double-quoted string if you want:
{{ asset("public/images/#{boutique.getImage()}") }}

<img class="img-thumbnail" src="{{ asset('uploads/avatars/' ~ user.avatar) }}" alt="user avatar">
symfony 5 webpack encore, my upload folder is in : \public\uploads\avatars

Related

How to read data from the sqlalchemy database (with foreign keys ) and render it as a table

Am trying to get details from my users table and payroll table and display them yas a single table which i can add actions to , but am failing to get any data although my database is populated
below is my database models for users and roles
from flask_login import UserMixin
from datetime import datetime
from app import db
class Roles(db.Model):
id = db.Column(db.Integer, primary_key=True)
role_fullname = db.Column(db.String(500), nullable=False, unique=True)
role_shortcut = db.Column(db.String(500), unique=False, nullable=False)
created_at = db.Column(db.DateTime, server_default=db.func.now())
updated_at = db.Column(db.DateTime, server_default=db.func.now(), server_onupdate=db.func.now())
class User(UserMixin,db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(500),unique=True, nullable=False)
profile_pic = db.Column(db.String(50),nullable=True, default='default.png')
password = db.Column(db.String(160),nullable=False)
role_id = db.Column(db.Integer, db.ForeignKey('roles.id'), nullable=False)
role = db.relationship("Roles", backref="person", uselist=False)
created_at = db.Column(db.DateTime, server_default=db.func.now())
updated_at = db.Column(db.DateTime, server_default=db.func.now(), server_onupdate=db.func.now())
firstname = db.Column(db.String(500),nullable=True)
lastname = db.Column(db.String(500),nullable=True)
verification = db.Column(db.String(20), nullable=True)
organization = db.Column(db.String(15), nullable=True)
payrolls = db.relationship('Payroll', backref='employee', lazy='dynamic')
and below is my model for payroll
from flask_login import UserMixin
from datetime import datetime
from app import db
from sqlalchemy.ext.hybrid import hybrid_property
from app.models.user import User, Roles
class Payroll(db.Model):
#primary key
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
#audit trail
payroll_created_at = db.Column(db.DateTime, default=datetime.now)
payroll_updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
#salary date
salary_date = db.Column(db.DateTime, nullable=True)
currency= db.Column(db.String(500),unique=False, nullable=True)
#personal information
bank_details = db.Column(db.String(500),unique=False, nullable=True)
payment_basis = db.Column(db.String(500),unique=False, nullable=True)
#days worked
normal_days = db.Column(db.Integer, unique=False, nullable=True)
days_worked = db.Column(db.Integer, unique=False, nullable=True)
previous_monthe_days = db.Column(db.Integer, unique=False, nullable=True)
#earnings
basic_salary = db.Column(db.Float, unique=False, nullable=True)
paid_normal_days = db.Column(db.Integer, unique=False, nullable=True)
and below is my routes.py
from flask import Blueprint, render_template,redirect, url_for, request, flash, session, current_app, jsonify, abort
from app.forms.payroll_forms import PayrollEntryForm
from app import db
from app.models.user import User, Roles
from app.models.payroll import Payroll, hybrid_property
#payroll_blueprint.route('/interactive_table')
def interactive_table():
employees = Payroll.query
return render_template('payroll/interactive_table.html', title='Interactive Table',
employees=employees)
below is my interactive_table.html
{% extends "base/_layout.html" %}
{% block body %}
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h1 class="card-title">Employees Information</h1>
<h2 class="card-subtitle text-muted">Search and Edit Employee Information</h2>
<div class="table-responsive">
<table id="data" class="table table-striped">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Role</th>
<th>Organisation</th>
<th>Bank Details</th>
<th>Payment Basis</th>
<th>Pay Date</th>
<th>Currency</th>
<th>Normal Days</th>
<th>Days Worked</th>
<th>Previous Month Days</th>
<th>Total Days Worked</th>
<th>Basic Salary</th>
<th>Paid Normal Working Days</th>
<th>Actual Basic Salary</th>
<th>O\T Days #1.5</th>
<th>O/T Days # 2</th>
<th>O/T Days # 1.5 Value</th>
<th>O/T Days # 2 Value</th>
<th>Hardship Allowance</th>
<th>Discretionary Allowance</th>
<th>Weekly Incentives</th>
<th>Transport Allowance</th>
<th>Medical Aid Cover</th>
<th>Funeral Cover</th>
<th>Airtime and Data Allowance</th>
<th>CILL Days</th>
<th>CILL Rate</th>
<th>Leave Days Taken</th>
<th>Leave Days Due</th>
<th>Total CILL</th>
<th>Monthly CILL</th>
<th>CILL Paid</th>
<th>Terminal Benefits</th>
<th>Coupons Value</th>
<th>Perfomance Allowance</th>
<th>Annivesary Payout</th>
<th>Shift Allowance</th>
<th>Agent Benefits</th>
<th>HPC</th>
<th>Total Earnings</th>
<th>Medical Loan</th>
<th>Garnishees</th>
<th>Total Primary Deductions</th>
<th>Gross Salary</th>
<th>NPS</th>
<th>WCIF</th>
<th>NEC</th>
<th>Grand Total</th>
<th>NSSA Employee</th>
<th>NEC Employee</th>
<th>Total Allowable Deductions</th>
<th>Taxable Income</th>
<th>Gross Tax</th>
<th>Tax Credits</th>
<th>PAYE</th>
<th>Aids Levy</th>
<th>Total Monthly Tax</th>
<th>Total After Taxes</th>
<th>Fixed Deductions</th>
<th>Fixed Deductions Months </th>
<th>Total Fixed Deductions </th>
<th>Monthly Fixed Deductions Due </th>
<th>Canteen </th>
<th>CICM </th>
<th>ZOL</th>
<th>Staff Loan</th>
<th>Other Loans</th>
<th>Independent Contractors Deduction </th>
<th>Back Pay </th>
<th>Total Other Deductions </th>
<th>Net Salary </th>
<th>View Payslip</th>
</tr>
</thead>
<tbody>
{% for payroll in employees %}
<tr>
<td>{{ payroll.employee.firstname }}</td>
<td>{{ payroll.employee.lastname }}</td>
<td>{{ payroll.employee.email }}</td>
<td>{{ payroll.employee.person.role_fullname}}</td>
<td>{{ payroll.employee.organization }}</td>
<td>{{ payroll.bank_details }}</td>
<td>{{ payroll.payment_basis }}</td>
<td>{{ payroll.salary_date }}</td>
<td>{{ payroll.currency}}</td>
<td>{{ payroll.normal_days}}</td>
<td>{{ payroll.days_worked}}</td>
<td>{{ payroll.total_days_worked}}</td>
<td>{{ payroll.previous_monthe_days}}</td>
<td>{{ payroll.basic_salary}}</td>
<td>{{ payroll.paid_normal_days }}</td>
<td>{{ payroll.actual_basic_salary }}</td>
<td>{{ payroll.one_point_five_overtime_days }}</td>
<td>{{ payroll.double_overtime_days }}</td>
<td>{{ payroll.one_point_five_overtime_days_value }}</td>
<td>{{ payroll.double_overtime_days_value }}</td>
<td>{{ payroll.hardship_allowance }}</td>
<td>{{ payroll.discretionary_allowance }}</td>
<td>{{ payroll.weekly_incentives}}</td>
<td>{{ payroll.transport_allowances }}</td>
<td>{{ payroll.medical_aid_cover }}</td>
<td>{{ payroll.funeral_cover }}</td>
<td>{{ payroll.airtime_and_data_allowance }}</td>
<td>{{ payroll.cill_days }}</td>
<td>{{ payroll.cill_rate }}</td>
<td>{{ payroll.leave_days_taken }}</td>
<td>{{ payroll.leave_days_due}}</td>
<td>{{ payroll.total_cill }}</td>
<td>{{ payroll.monthly_cill }}</td>
<td>{{ payroll.cill_paid }}</td>
<td>{{ payroll.terminal_benefits }}</td>
<td>{{ payroll.coupons_value }}</td>
<td>{{ payroll.performance_allowance }}</td>
<td>{{ payroll.anniversary_payout}}</td>
<td>{{ payroll.shift_allowance}}</td>
<td>{{ payroll.agent_benefits}}</td>
<td>{{ payroll.hpc }}</td>
<td>{{ payroll.total_earnings}}</td>
<td>{{ payroll.medical_loan }}</td>
<td>{{ payroll.garnishees }}</td>
<td>{{ payroll.total_primary_deductions }}</td>
<td>{{ payroll.gross_salary }}</td>
<td>{{ payroll.nps }}</td>
<td>{{ payroll.wcif }}</td>
<td>{{ payroll.nec }}</td>
<td>{{ payroll.grand_total }}</td>
<td>{{ payroll.nssa_employee }}</td>
<td>{{ payroll.nec_employee }}</td>
<td>{{ payroll.total_allowable_deductions }}</td>
<td>{{ payroll.taxable_income }}</td>
<td>{{ payroll.gross_tax}}</td>
<td>{{ payroll.tax_credits }}</td>
<td>{{ payroll.paye }}</td>
<td>{{ payroll.aids_levy}}</td>
<td>{{ payroll.total_monthly_tax }}</td>
<td>{{ payroll.total_after_taxes }}</td>
<td>{{ payroll.fixed_deductions }}</td>
<td>{{ payroll.fixed_deductions_months }}</td>
<td>{{ payroll.total_fixed_deductions }}</td>
<td>{{ payroll.total_monthly_fixed_deductions }}</td>
<td>{{ payroll.canteen }}</td>
<td>{{ payroll.cicm }}</td>
<td>{{ payroll.zol}}</td>
<td>{{ payroll.staff_Loan}}</td>
<td>{{ payroll.loan }}</td>
<td>{{ payroll.last_month_salary }}</td>
<td>{{ payroll.other_deductions }}</td>
<td>{{ payroll.net_pay }}</td>
<td><a class="btn btn-sm" style="background-color:rgb(0, 75, 93); color:whitesmoke;" href="{{ url_for('payroll.payslip', empid = payroll.id )}}">View Full Profile</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
{% endblock %}
{% block scripts %}
<script>
$(document).ready(function () {
$('#data').DataTable();
});
</script>
{% endblock %}
and i get is an empty table

Laravel7 2 Table joining with complex data

I want to show the data from tbl_structure with people name from tble_peple but I don't know how to join it. I am a new on laravel.
my tbl_structure
my tble_peple
showing data
my code on Controller
public function get_people(){
$Showstructe= DB::table('tbl_structure')->orderby("name","asc")
->get();
return view('admin.TestCustomer.structure_show',compact('Showstructe'));
}
my code to show data
<table class="table table-bordered">
<tr>
<th>ID</th>
<th>Structure</th>
<th>Stage A</th>
<th>Stage B</th>
<th>Stage C</th>
<th>Stage D</th>
<th>Stage E</th>
<th>Stage F</th>
</tr>
#foreach($Showstructe as $people)
<tr>
<td>{{ $people->id }}</td>
<td>{{ $people->name }}</td>
<td>{{ $people->stage_a }}</td>
<td>{{ $people->stage_b }}</td>
<td>{{ $people->stage_c }}</td>
<td>{{ $people->stage_d }}</td>
<td>{{ $people->stage_e }}</td>
<td>{{ $people->stage_f }}</td>
<tr>
#endforeach
</table>
How to show people name not just people id?
public function get_people(){
$Showstructe= DB::table('tbl_structure')->leftJoin('people as p', 'tbl_structure.id', '=', 'p.id')->select('p.name as peopleName')->orderby("name","asc")
->get();
return view('admin.TestCustomer.structure_show',compact('Showstructe'));
}
<table class="table table-bordered">
<tr>
<th>ID</th>
<th>Structure</th>
<th>Stage A</th>
<th>Stage B</th>
<th>Stage C</th>
<th>Stage D</th>
<th>Stage E</th>
<th>Stage F</th>
</tr>
#foreach($Showstructe as $people)
<tr>
<td>{{ $people->peopleName}}</td>
<td>{{ $people->name }}</td>
<td>{{ $people->stage_a }}</td>
<td>{{ $people->stage_b }}</td>
<td>{{ $people->stage_c }}</td>
<td>{{ $people->stage_d }}</td>
<td>{{ $people->stage_e }}</td>
<td>{{ $people->stage_f }}</td>
<tr>
#endforeach
</table>

How to insert data for table in mangodb

I am trying to insert data for table and show in my page but I do not know how to insert the data and how to get the data to show in the table.If anyone know please help.
db.maintable.insert(
{
"id":"1",
"orderno":"19345",
"name" : "tutorialspoint",
"startedate":"21/04/2018",
"enddate":"28/12/2018"
},
{
"id":"2",
"orderno":"12945",
"name" : "tutorialspoint",
"startedate":"01/12/2018",
"enddate":"21/02/2018"
},
{
"id":"3",
"orderno":"12325",
"name" : "tutorialspoint",
"startedate":"21/22/2018",
"enddate":"24/12/2018"
}
)
data.service.ts:
getTable() {
return this._http.get("/api/maintable")
.map(result => this.result = result.json().data);
}
app.component.ts:
this._dataService.getUsers().subscribe(res => this.users = res);
app.component.html:
<table>
<thead>
<th>Id</th>
<th>Order noo</th>
<th>Name</th>
<th>Start Date</th>
<th>End Date</th>
</thead>
<tbody>
<tr *ngFor="let getdata of maintable">
<td>{{ getdata.id }}</td>
<td>{{ getdata.orderno }}</td>
<td>{{ getdata.name }}</td>
<td>{{ getdata.startdate }}</td>
<td>{{ getdata.enddate }}</td>
</tr>
</tbody>
</table>
You have made mistake on loop, instead of maintable use result
<tr *ngFor="let getdata of maintable">

Django how to check if username already exists and return gracefully

Django checks if the entered user name already exists and if it exists it falls with an error.
I want it to check if username exists the same as he checks if email is valid or that the passwords are matched, i.e. return the appropriate HTML message to the user.
this is my attempt to do it from view.py
if request.method == 'POST':
form = SubscriberForm(request.POST)
if form.is_valid():
# Unpack form values
username = form.cleaned_data['username']
password = form.cleaned_data['password1']
email = form.cleaned_data['email']
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
# Create the User record
user = User(username=username, email=email,
first_name=first_name, last_name=last_name)
if User.objects.filter(username=username).exists():
context= {'form': form, 'error':'The username you entered has already been taken. Please try another username.'}
return render(request, template, context)
this is my html form (the relevant part):
<h5 class="sn-sh">CREATE A USERNAME AND PASSWORD</h5>
<table class="table">
<tbody>
<tr>
<th>{{ form.username.label }}</th>
<th>{{ form.email.label }}</th>
</tr>
<tr>
<td>{{ form.username }}</td>
<td>{{ form.email }}</td>
</tr>
<tr>
<td>{{ form.username.errors }}</td>
<td>{{ form.email.errors }}</td>
</tr>
<tr>
<th>Password</th>
<th>Confirm Password</th>
</tr>
<tr>
<td>{{ form.password1 }}</td>
<td>{{ form.password2 }}</td>
</tr>
<tr>
<td>{{ form.password1.errors }}</td>
<td>{{ form.password2.errors }}</td>
</tr>
</tbody>
</table>
Currently it is not crush but does not return any message to the user ... (nothing really happens)
What am I doing wrong? How can I get him to send a nice message to the user?
I tried using ValidationError - but it also just crashed and sent an ugly message to the user
----- edit ----
I added an ugly solution that work
I added to forms.py this lines:
from django.contrib.auth.models import User
.
.
.
class SubscriberForm(AddressMixin, UserCreationForm):
.
.
.
def clean_username(self):
username = self.cleaned_data['username']
if User.objects.filter(username=username).exists():
raise forms.ValidationError("User name already exist, please choose new one")
return username
This solution as far as I tested work - but it's ugly and feels rung (I now have views.py functionality inside of forms.py - hope that you guise have a better solution that keep the django model clean

Twig - Fill a table horizontally with HTML2PDF

I'm working on filling a table horizontally in Twig and transform this twig view into a PDF.
I succeeded in filling it horizontally but I have an annoying problem remaining : I have a black line on the right of the last page of my resulting PDF.
Here's an picture of the problem in the PDF :
In Twig view :
Here's my code :
<table align="center">
<thead>
<tr>
<th>Numéro</th>
<th>Note</th>
<th>Classement</th>
<th class="espace"></th>
<th>Numéro</th>
<th>Note</th>
<th>Classement</th>
</tr>
</thead>
<tbody>
{% for passercolle in passercolles %}
{% if (loop.index is odd) or loop.first %}
<tr>
{% endif %}
{% if loop.index is odd %}
<td>{{ passercolle.username }}</td>
<td>{{ passercolle.note }}/20</td>
<td>{{ passercolle.classement }}</td>
<td class="espace" ></td>
{% elseif loop.index is even %}
<td>{{ passercolle.username }}</td>
<td>{{ passercolle.note }}/20</td>
<td>{{ passercolle.classement }}</td>
{% endif %}
{% if loop.last and loop.last is odd %}
<td></td>
<td></td>
<td></td>
{% endif %}
{% if (loop.index is even) or loop.last %}
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
I think it's a problem with loop.last but I can't solve this.
Solved by replacing {% if loop.last and loop.last is odd %} by {% if loop.last and passercolles|length is odd %}

Resources