Search code examples
pythonajaxweb-applicationsjinja2cherrypy

Cherrypy render new jinja2 page after login


I have an application that I want to put behind a login page. I receive login information through ajax on the login page I load, then I want to load some data based on the user and render them a new page. I'm also using jinja2 to render javascript based on their user data.

@cherrypy.expose
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def get_data(self):
    result = {"operation": "request", "result": "success"}

    input_json = cherrypy.request.json
    value = input_json["anno"]
    print value
    return result

@cherrypy.expose
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def login(self):
    result = {"operation": "request", "result": "success"}

    input_json = cherrypy.request.json
    uname = input_json["uname"]

    print uname
    tmpl = env.get_template('index.html')
    imlist = os.listdir('app/public/clips'+uname)
    imlist.sort()
    print "loading homepage for user " + uname
    imlist = ['static/clips/'+item for item in imlist]

    return tmpl.render(salutation='User:', target=uname,image_list=json.dumps(imlist))

And my login script in login.html is:

function enter_login(){
    var tb = document.getElementById("scriptBox");
        var tbtxt = tb.value;
        $.ajax({
            url: "login",
            type: "POST",
            contentType: 'application/json',
            data: JSON.stringify({"uname": tbtxt}),
            dataType:"json"
        });
  }

What am I doing wrong here? How do I render index.html with the given jinja parameters?


Solution

  • I was able to fix this via adding a success function to the AJAX call:

    success:function(data)
                {
                  if(data!="")
                  {
                    $("body").html(data);
                  }
                }