Silex + Twig + SQL Server : rendering pictures - database

I have some pictures in a SQL Server database in a BLOB column.
I need to get them and show up to the HTML page.
But in the result on the HTML page, I have the long string in src attribute instead of the pictures.
Silex code:
<!-- language-all: php -->
$pict->get('/show_pic/{id}', function ($id) use ($app) {
//Request of photos
$photos=getPhotoByID($id, $app);
return $app['twig']->render('test/template_show_pictures.html.twig', array('photos'=>$photos));
})->bind('show_pic');
function getPhotoByID($ID, $app)
{
$sql = "<some SQL here>";
$statement = $app['dbs']['mssql']->prepare($sql);
$statement->bindValue(1, $ID, "text");
$statement->execute();
$photo = $statement->fetchAll();
if (empty($photo)) {
return "We don't have a photo with id=".$ID;
}
return $photo;
}
Twig file:
<!-- language: twig -->
{% extends 'test/test_base.html.twig' %}
{% block title %}Pictures from DB{% endblock %}
{% block content %}
{% for im in photos %}
<img alt="Embedded Image" src="data:image/jpeg;base64,{{ im['Foto'] }}" />
{% endfor %}
{% endblock %}
HTML markup :
<!-- language: lang-html -->
<!DOCTYPE html>
<html>
<head>
<title>Pictures from DB</title>
<link rel="stylesheet" href="/css/print.css">
</head>
<body>
<div class="page">
<img alt="Embedded Image" src="data:image/jpeg;base64,���JFIF...too many symbols here" />
</div>
</body>
</html>
What is wrong with it?

You should encode image with base64
Add filter for encoding to base64:
$app->extend('twig', function ($twig, $app) {
$twig->addFilter('base64_encode', new \Twig_SimpleFilter('base64_encode', function ($value) {
return \base64_encode($value);
}));
return $twig;
});
And use it in template:
{{ im['Foto']|base64_encode }}

Related

Decrement model value in django

It's my first time using django to clear my schoolwork, I want to lower Stock in my Book class by Quantity value,
ex.
Stock = 10 and Quantity = 1
Stock = 9 because 10 - 1 for Stock - Quantity
this is my code
main.html
{% load static %}
<html>
<head>
<title>
Bookstore Management System
</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<body>
{% include 'book/navbar.html' %}
{% block content %}
{% endblock %}
<br>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js#1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>
</html>
home.html
{% extends 'book/main.html' %}
{% block content %}
<br>
<div class="container jumbotron">
<div class="container row">
<div class="col-lg-10">
<table class="table table-sm">
<tr>
<th>title</th>
<th>Author</th>
<th>Edition</th>
<th>Stock</th>
<th>Price</th>
</tr>
{% for book in books %}
<tr>
<td>{{book.title}}</td>
<td>{{book.Author}}</td>
<td>{{book.Edition}}</td>
<td>{{book.Stock}}</td>
<td>{{book.Price}}</td>
<td><a class="btn btn-sm btn-info" href="{% url 'addtocart' book.id %}">Add to cart</a></td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
{% endblock %}
viewcart.html
{% extends 'book/main.html' %}
{% load static %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-lg-10"></div>
<br>
<h5>BOOKS:</h5>
<div class="card card-body">
<table>
<tr>
<th>title</th>
<th>Author</th>
<th>Edition</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
</tr>
{% for book in cart.books.all %}
<tr>
<td>{{book.title}}</td>
<td>{{book.Author}}</td>
<td>{{book.Edition}}</td>
<td>{{book.Quantity}}</td>
<td>{{book.Price}}</td>
<td>{{book.Total}}</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
{% endblock %}
Model.py
from django.db import models
from django.db.models import F
from django.contrib.auth.models import User
class Book(models.Model):
title=models.CharField(max_length=300, null=True)
Author=models.CharField(max_length=300, null=True)
Edition=models.IntegerField()
Price=models.IntegerField()
Stock=models.IntegerField()
Quantity=models.IntegerField()
Total=models.IntegerField()
def decrementStock(self):
self.Quantity += 1
self.update(Stock=F('Stock') - F('Quantity'))
self.save()
def Tprice(self):
self.update(Total=F('Price') * F('Quantity'))
self.save()
def _str_(self):
return str(self.title)
class Customer(models.Model):
user=models.OneToOneField(User, null=True, on_delete=models.CASCADE)
name=models.CharField(max_length=200, null=True)
phone=models.CharField(max_length=200, null=True)
email=models.CharField(max_length=200, null=True)
date_created=models.DateTimeField(auto_now_add=True, null=True)
def _str_ (self):
return str(self.name)
class Cart(models.Model):
customer=models.OneToOneField(Customer, null=True, on_delete=models.CASCADE)
books=models.ManyToManyField(Book)
def _str_ (self):
return str(self.customer)
views.py
from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import *
from django.contrib.auth import login,logout,authenticate
from .forms import *
def home(request):
books=Book.objects.all()
context={'books':books}
if request.user.is_staff:
return render(request, 'book/adminhome.html', context)
else:
return render(request, 'book/home.html', context)
def logoutPage(request):
logout(request)
return redirect('/')
def loginPage(request):
if request.user.is_authenticated:
return redirect('home')
else:
if request.method=="POST":
username=request.POST.get('username')
password=request.POST.get('password')
user=authenticate(request,username=username, password=password)
if user is not None:
print("working")
login(request,user)
return redirect('/')
context ={}
return render(request,'book/login.html',context)
def registerPage(request):
if request.user.is_authenticated:
return redirect('home')
else:
form=createuserform()
cust_form=createcustomerform()
if request.method=='POST':
form=createuserform(request.POST)
cust_form=createcustomerform(request.POST)
if form.is_valid() and cust_form.is_valid:
user=form.save()
customer=cust_form.save(commit=False)
customer.user=user
customer.save()
return redirect('login')
context={
'form':form,
'cust_form':cust_form
}
return render(request,'book/register.html', context)
def addbook(request):
form=createbookform()
if request.method=='POST':
form=createbookform(request.POST)
if form.is_valid():
form.save()
return redirect('/')
context={'form':form}
return render(request,'book.addbook.html', context)
def viewcart(request):
cust=Customer.objects.filter(user=request.user)
for c in cust:
carts=Cart.objects.all()
for cart in carts:
if(cart.customer==c):
context={
'cart':cart
}
return render(request, 'book/viewcart.html', context)
else:
return render(request,'book/emptycart.html')
def addtocart(request,pk):
book=Book.objects.get(id=pk)
Book.objects.get(Quantity=pk).decrementStock()
cust=Customer.objects.filter(user=request.user)
for c in cust:
carts=Cart.objects.all()
reqcart=''
for cart in carts:
if(cart.customer==c):
reqcart=cart
break
if(reqcart==''):
reqcart=Cart.objects.create(
customer=c,
)
reqcart.books.add(book)
return redirect('/')
in model.py and views.py are wrong but i dont know to fix it
sorry for my bad english writing and i thank you for poeple to helped me.
I try by editing model.py by using this but its failed and get these error
`
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/addtocart/1
Django Version: 3.2
Python Version: 3.10.7
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'book']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:\coding python\MHB\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\coding python\MHB\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\coding python\MHB\Scripts\bookstore\book\views.py", line 80, in addtocart
Book.objects.get(Quantity=pk).decrementStock()
File "C:\coding python\MHB\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\coding python\MHB\lib\site-packages\django\db\models\query.py", line 435, in get
raise self.model.DoesNotExist(
Exception Type: DoesNotExist at /addtocart/1
Exception Value: Book matching query does not exist.
`

In Synfony how do I divide a list according to the statut of his creator ? Error : Key for array with keys "0" does not exist

I'd like to display two lists of events : one list with events created by organizers with organizer status and a list with events created by organizers with member status. My conditions are like this : "if some event exist" and "if the organizer who created the event is an organizer (or a regular member)". For information, "organizer" and "member" depend on the status of the organizer. Because of the second condition, I had this error :
Key "organizer" for array with keys "0" does not exist.
I don't understand why... What needs to be corrected ?
Thank you very much for your help.
My twig file :
{% extends 'base.html.twig' %}
{% block title %}Liste des activités{% endblock %}
{% block main %}
<div class="events">
<div class="vr fixed-top start-50"></div>
<div class="row">
<div class="col-12 col-md-6">
<h2 class="text-center my-4">
<img src="{{ asset('img/titres/zpeak-sorties.svg') }}" alt="Les Zpeak Sorties !">
</h2>
<ul class="list-group">
{% if events and events.organizer.status == 'organizer' %}
{% for event in events %}
<a class="list-group-item list-group-item-action">
<img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="Drapeau {{ event.spokenlanguage.name }}" class="me-2"> {{ event.title }}
</a>
{% endfor %}
{% else %}
<p>Il n'y a pas de zpeak sortie organisée.</p>
{% endif %}
</ul>
</div>
<div class="col-12 col-md-6">
<h2 class="text-center my-4">
<img src="{{ asset('img/titres/zpeak-idees.svg') }}" alt="Les Zpeak Idées !">
</h2>
<ul class="list-group">
{% if events and events.organizer.status == 'member' %}
{% for event in events %}
<a class="list-group-item list-group-item-action">
<img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="Drapeau {{ event.spokenlanguage.name }}" class="me-2"> {{ event.title }}
</a>
{% endfor %}
{% else %}
<p>Il n'y a pas de zpeak idée proposée.</p>
{% endif %}
</ul>
</div>
</div>
</div>
{% endblock %}
My EventsController.php file :
<?php
namespace App\Controller\Front;
use App\Form\SearchType;
use App\Repository\EventsRepository;
use App\Repository\CategoriesRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class EventsController extends AbstractController
{
#[Route('/search', name: 'search')]
public function search(Request $request, SessionInterface $sessionInterface)
{
$data = $request->request->all();
$sessionSearchFormData = $sessionInterface->get('searchFormData');
$form = $this->createForm(SearchType::class, ['data' => $sessionSearchFormData]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$sessionInterface->set('searchFormData', $data);
return $this->redirectToRoute('events', [$data]);
}
return $this->renderForm('front/search.html.twig', [
'form' => $form
]);
}
#[Route('/events', name: 'events')]
public function events(
EventsRepository $eventsRepository,
CategoriesRepository $categoriesRepository
){
$events = $eventsRepository->findAll();
dd($events);
$categories = $categoriesRepository->findAll();
return $this->render("front/events.html.twig", ['events' => $events, 'categories' => $categories]);
}
}
dd($events) in EventsController.php :
EventsController.php on line 45:
array:1 [▼
0 => App\Entity\Events {#1026 ▼
-id: 1
-title: "Soirée à Belleville"
-description: "Viens boire un verre, rencontrer des natifs et pratiquer l'espagnol !"
-category: Proxies\__CG__\App\Entity\Categories {#933 ▶}
-spokenlanguage: Proxies\__CG__\App\Entity\Language {#981 ▶}
-address: Proxies\__CG__\App\Entity\Location {#744 ▶}
-date: DateTime #1665446400 {#1022 ▶}
-starttime: DateTime #72000 {#1023 ▶}
-endtime: DateTime #86340 {#1024 ▶}
-organizer: Proxies\__CG__\App\Entity\User {#996 ▼
-id: 1
-email: null
-roles: []
-password: null
-gender: null
-lastname: null
-firstname: null
-birthdate: null
-occupation: null
-photo: null
-status: null
-nationality: null
-nativelanguage: null
+__isInitialized__: false
…2
}
-participations: Doctrine\ORM\PersistentCollection {#1002 ▶}
}
]
In your case, you can have many Events, so in your twig, you need to use a for loop.
Place your for loop before the if.
Something like this :
<ul class="list-group">
{% for event in events %}
{% if event and event.organizer.status == 'organizer' %}
<a class="list-group-item list-group-item-action">
<img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="Drapeau {{ event.spokenlanguage.name }}" class="me-2"> {{ event.title }}
</a>
{% else %}
<p>Il n'y a pas de zpeak sortie organisée.</p>
{% endif %}
{% endfor %}
</ul>
In case you are sure to have only one item, you can use events[0].organizer.status == 'organizer'
Regards,

CKAN - adding custom fields with ckenxt-scheming

i'm working with ckanext-scheming and trying to add a custom field similar to repeating subfields with few modifications (below are few files that define the field and it's behavior), for the most part it works correct but it doesn't get submitted to the database (although once i intercept the package create action, i can see the field data is there), any ideas are welcomed:
the scheming yaml part:
- field_name: tag_string
label: Tags
form_snippet: custom_tags.html
display_snippet: modified_repeating_subfields.html
help_text: >-
Additional keywords useful for ...
the form snippet:
{% import 'macros/form.html' as form %}
{% asset 'ckanext-dalrrdemcdcpr/facets-active-js' %}
{%
set options=[
{'value': '001', 'text': _('Discipline')}
, {'value': '002', 'text': _('Place')}
, {'value': '003', 'text': _('Stratum')}
, {'value': "004", "text":_("Temporal")}
, {'value': "005", "text":_("Theme")}
]
%}
{%- set extra_html = caller() if caller -%}
{% set classes = (classes|list) %}
{% call form.input_block(field.field_name, field.label, "", classes, extra_html=extra_html, is_required=false) %}
<div class="repeating_custom_tag_row-index-0" id="repeating_custom_tag_row-index-0" data-module="repeating_tags_handler">
<div class="metadata_tags_holder">
<div class="metadata_tags_styler">
<div class="metadata_tags_input">
<label for="custom_delete" class="input-group-text">{{ _('Input tag') }}</label>
<input class="form-control" id="repeating_custom_tag_row-index0-tag-input-index-0" type="text" name="repeating_custom_tag_row-index0-tag-input-index-0" value="" placeholder=""/>
</div>
<div class="metadata_tags_type_select">
<div><label for="tag_select" class="input-group-text">{{_('Input tag type')}}</label></div>
<select id="tag_select" name="repeating_custom_tag_row-index0-select-input-index-0" required>
{% for option in options %}
<option value="{{ option.value }}"{% if option.value == selected %} selected{% endif %}>{{ option.text or option.value }}</option>
{% endfor %}
</select>
</div>
<div class="tags_buttons_wrapper">
<div class="tags_handling_buttons">
<button class="btn btn-success add_metadata_tags_btn">Add</button>
<button class="btn btn-danger remove_metadata_tags_btn"><i class="fa fa-trash"></i>Remove</button>
</div>
</div>
</div>
</div>
</div>
{% endcall %}
the display snippet (don't think it's useful):
{% set fields = data[field.field_name] %}
{% block subfield_display %}
{% for field_data in fields %}
<div class="panel panel-default">
<div class="panel-body">
<dl class="scheming-subfield-list">
{% for subfield in field.repeating_subfields %}
<dt class="dataset-label">
{{ h.scheming_language_text(subfield.label) }}
</dt>
<dd>
{%- snippet 'scheming/snippets/display_field.html',
field=subfield,
data=field_data,
entity_type=entity_type,
object_type=object_type
-%}
</dd>
{% endfor %}
</dl>
</div>
</div>
{% endfor %}
{% endblock %}
this is how the relevant part of the form looks (the tags):
the js module controlling tags fields behavior:
ckan.module("repeating_tags_handler", function ($){
/*
control muliple interaction regarding the tags
section, handles both css and buttons behavior.
*/
return {
initialize: function(){
$.proxyAll(this,/_on/);
// set the styles before copying the section
let add_btns = $(".add_metadata_tags_btn")
add_btns.each((idx,add_btn) => {add_btn.style.marginRight="2px"})
$(document).on('click','.add_metadata_tags_btn',this._on_add_tags)
$(document).on('click','.remove_metadata_tags_btn',this._on_remove_tags)
let repeating_fields_wrapper = $(".repeating_custom_tag_row-index-0")
repeating_fields_wrapper.css({"margin-bottom":"30px"})
let tags_holder = $(".metadata_tags_holder")
tags_holder.css("width:100%")
$(".metadata_tags_styler").css({"width":"100%", "display": 'flex', 'flex-direction': 'row'})
$(".metadata_tags_input").css({"width":"40%"})
$(".metadata_tags_type_select").css({"width":"30%"})//tag_select
$("#tag_select").css({"width":"100%", "height":"34px", "margin-left":"4px"})
$(".tags_buttons_wrapper").css({"margin-left": "4px", "padding-top": "2px"})
$(".tags_handling_buttons").css({"width":"100%","padding-top":"22px", "display": "flex", "flex-direction": "row", "margin-left":"4px"})
this.section_html = tags_holder.html()
const config = { attributes: true, childList: true, subtree: true };
//const added_items_observer = new MutationObserver(this.added_items_mutation_observer);
//added_items_observer.observe(repeating_fields_wrapper.get(0), config)
},
_on_add_tags:function(e){
e.preventDefault()
//let search_parent = e.target.parentElement.parentElement.parentElement.parentElement
let search_parent = e.target.closest(".metadata_tags_styler")
let new_index = Array.from(search_parent.parentNode.children).indexOf(search_parent) +1
let new_index_text = `index-${new_index}`
this.section_html = this.section_html.replace(/index-[0-9]/g, new_index_text)
this.el.append(this.section_html)
},
_on_remove_tags:function(e){
e.preventDefault()
let removed_tag_row = e.target.closest(".metadata_tags_styler")
//let removed_tag_row = e.target.parentElement.parentElement.parentElement.parentElement
removed_tag_row.remove()
},
}
})
and finally this is a Runtime error screenshot i raised for you - dear - to see the tags data is there:
thank you!

Angularjs filling a table

I am not able to enter a list in a table within the html of the body. This list came using Angularjs, ajax request with django. The incoming list contains other lists within it, which can vary in size every query. Example: [["a","b","c"],["a","b","c"],["a","b","c"]] or [["a","b","c","d","e"],["a","b","c","d","e"],["a","b","c","d","e"]]
File dfp_consulta.js
function ListDropdown($scope, $http) {
$scope.bvmf = {ccvms : [ '1023', '10456', '10472'],
consolidados : [ 't', 'f' ],
origens : [ 'ITR', 'DFP' ],
balancos : [ '1', '2', '3', '4'],
periodos : [ '1-3', '1-6', '1-9', '1-12' ],
anos : [ '1997', '1998', '1999', '2012' ]
$scope.range=[]
$scope.send=function(ccvm, consolidado, origem, balanco, periodo, ano, ano_doc){
$http({
method:'POST',
url:'/sending/',
data:ccvm+consolidado+origem+balanco+periodo+ano+ano_doc,
headers:{'Content-Type': 'application/x-www-form-urlencoded'},
}).success(function(c){
$scope.range=c
})
}
}
File dfp_consulta.html
{% extends "base.html" %}
{% block corpo %}
<div ng-app="" ng-controller="ListDropdown">
<select ng-model="ccvm" ng-options="ccvm as ccvm for ccvm in bvmf.ccvms"></select><br>
<select ng-model="consolidado" ng-disabled="!ccvm" ng-options="consolidado for consolidado in bvmf.consolidados" ></select><br>
<select ng-model="origem" ng-disabled="!consolidado" ng-options="origem for origem in bvmf.origens" ></select><br>
<select ng-model="balanco" ng-disabled="!origem" ng-options="balanco for balanco in bvmf.balancos" ></select><br>
<select ng-model="periodo" ng-disabled="!balanco" ng-options="periodo for periodo in bvmf.periodos" ></select><br>
<select ng-model="ano" ng-disabled="!periodo" ng-options="ano for ano in bvmf.anos" ></select><br>
<select ng-model="ano_doc" ng-disabled="!ano" ng-options="ano_doc for ano_doc in bvmf.anos" ng-change="send(ccvm,consolidado,origem,balanco,periodo,ano,ano_doc)"></select><br>
<table class='table table-hover'>
{% for i in range %}
<tr>
{% for s in i %}
<td>{{ s }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
{% endblock %}
I am getting in $scope.range for example, the following list [["a", "b", "c"] ["a", "b", "c"] ["a", "b "," c "]], however, {% for i in range %} is not using the range array
The problem is that both Django and AngularJS share the same syntax of Jinja2 for variable substitution in templates.
The easiest option would be to tell Django not to use it's syntax in places you need to use angularjs variable substitution - in other words, use verbatim tag:
{% block corpo %}
{% verbatim %}
<div ng-app="" ng-controller="ListDropdown">
<select ng-model="ccvm" ng-options="ccvm as ccvm for ccvm in bvmf.ccvms"></select><br>
<select ng-model="consolidado" ng-disabled="!ccvm" ng-options="consolidado for consolidado in bvmf.consolidados" ></select><br>
<select ng-model="origem" ng-disabled="!consolidado" ng-options="origem for origem in bvmf.origens" ></select><br>
<select ng-model="balanco" ng-disabled="!origem" ng-options="balanco for balanco in bvmf.balancos" ></select><br>
<select ng-model="periodo" ng-disabled="!balanco" ng-options="periodo for periodo in bvmf.periodos" ></select><br>
<select ng-model="ano" ng-disabled="!periodo" ng-options="ano for ano in bvmf.anos" ></select><br>
<select ng-model="ano_doc" ng-disabled="!ano" ng-options="ano_doc for ano_doc in bvmf.anos" ng-change="send(ccvm,consolidado,origem,balanco,periodo,ano,ano_doc)"></select><br>
<table>
<tr ng-model="item2" ng-repeat="item2 in range">
<td ng-repeat="item3 in item2">{{ item3 }}</td>
</tr>
</table>
</div>
{% endverbatim %}
{% endblock %}
The new table structure allows the build with angularjs amounts different of row and column <tr ng-repeat> and <td ng-repeat>
See also:
Integrate AngularJS with Django
Stackoverflow - Django not working with Angularjs

AngularJS ng-click not invoked where dom inside repeat

When the ng-repeat area in my code example is executed the ng-click actions are not working. Although if moved outside the ng-repeat it would work. Which am not sure how to solve or what is causing this to happen,
my HTML
<table class="table table-bordered table-hover" id="my-vehicles-table" ng-controller="VehicleController">
<tbody>
<tr ng-repeat="car in cars">
<td>{% ng car._get_model_display.make_display %} {% ng car._get_model_display.model_display %} {% ng car._get_model_display.trim_display %}</td>
<td>{% ng car.created_since %}</td>
<td>{% ng car.view_count %}</td>
<td>
{% trans 'Delete' %}
{% trans 'Edit' %}
</td>
</tr>
</tbody>
</table>
where by {% ng xxx %} will output {{ xx }} this is the django template tag for it. The content is rendered normally, even inside the anchor for delete, I can see delete($event, num) so its populating the values correctly.
But when I click the delete anchor its not invoking the function delete from my following controller
'use strict';
function VehicleController($scope, car) {
car.query(
// params
{created_by: '1'},
// success
function(data) {
$scope.cars = data.objects;
},
// error
function(data) {
}
);
var init = function() {
};
$scope.delete = function($event) {
console.log('dude');
// car.delete({id: id});
}
// initialize values
init();
}
delete($event, {% ng car.id %}) should probably just be delete($event, car.id)
I'm presuming {% ng %} is custom {{ }}? I've not seen that before. Either way, you don't need the {{ or {% ng inside of the ng-click="", as that's $eval'ed.

Resources