I am using ajax to call sqlalchemy and pyramid to pull data from my mysql database and paginate. My Ajax call is:
$.ajax({
type: 'GET',
url: "results",
dataType: 'json',
})
.fail( function (jqXHR, textStatus, errorThrown){
alert(errorThrown);
})
.done(function(data){
$.each(data.myitems, function(index, item){
// do stuff here
});
});
My view is:
def pager(request):
query = DBSession.query(MyTable)
page_url = paginate.PageURL_WebOb(request)
customers = paginate.Page(query,
page=int(request.params.get("page", 1)),
items_per_page=25,
url=page_url)
return render_to_response ("templates/my_json.jinja2",
{"customers": customers},
request=request)
In my jinja2 template, I have:
{
"page":{{ customers.page }},
"next_page":{{ customers.next_page }},
"customerlist":[{% for cus in customers %}
{"name":"{{ cus.name }}",
"birthday":"{{ cus.birthday }}",
{% if not loop.last %},{% endif %}{% endfor %}
]
}
My data then looks like this:
{
"thisvariable":1,
"anothervariable":2,
"myitems":[
{"name":"Matt",
"birthday":"1978-02-23 00:00:00"},
{"name":"Carol\y",
"birthday":"1967-05-05 00:00:00"},
{"name":"Bob",
"birthday":"1984-02-03 00:00:00"}
]
}
How do I escape the "\" in Carol\y? I get an error: "SyntaxError: Unexpected token y". In Jinja2, I've tried the |e escape but get the same error.
EDIT: I've also tried in my models.py:
from re import escape
class MyTable(Base):
....
def __init__(self, name=""):
self.name = escape(name)
I still get the same error.