0

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.

user_78361084
  • 3,538
  • 22
  • 85
  • 147

3 Answers3

1

May be you want to use json.dumps instead? Do JSON generation by Jinja2 is realy strange thing.

seriyPS
  • 6,817
  • 2
  • 25
  • 16
1

Don't use a template to render JSON; use pyramid's built-in JSON renderer instead:

from pyramid.view import view_config

@view_config(renderer='json')
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 {"customers": customers}

You probably want to apply the JSON conversion method we discussed in your other question to customers here though; the last line would then be:

    return {"customers": [c.json_dump() for c in customers]}

See the pyramid renderer documentation.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

A backslash is normally an escape character. Use a second backslash to escape a backslash and treat it as a normal character. Like this. carol\\y.

Hans Then
  • 10,935
  • 3
  • 32
  • 51
  • yes, I need to escape the backslash. How? Where do I put that? – user_78361084 Sep 22 '12 at 21:44
  • Usually at the time you create your json. How do you get your data and how is it transformed into json? – Hans Then Sep 22 '12 at 21:47
  • See my edits in my question....I've tried to do that in models.py but I still get the same error – user_78361084 Sep 22 '12 at 21:50
  • I'm thinking this may be a different problem. Maybe carol/y is actually something to indicate the polish alternative l character? – Hans Then Sep 22 '12 at 21:57
  • Anyway, how do you transform your model data into json? I think it is not a clean design to store your data with escape characters. The best place to escape the data is just begore you make json out of it. – Hans Then Sep 22 '12 at 21:58