hi everyone.

i am trying to do this simple? thing:
. in a test page ("/test") send some parameters to the server using POST
. when the view detects those paremeters, redirect to home ("/")

i can get this to work but only if i post the values using a form, it does 
not work if i run a javascript function calling a xmlhttprequest.

i used the cookiecutter-starter and added some lines.

**__init__.py**

from pyramid.config import Configurator


def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(settings=settings)
    config.include('pyramid_chameleon')
    config.add_static_view('static', 'static', cache_max_age=0)
    config.add_route('home', '/')
    config.add_route('test', '/test')
    config.scan()
    return config.make_wsgi_app()





**views.py**
from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound,HTTPSeeOther


@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
    print("in my view")
    return {'project': 'project'}


@view_config(route_name='test', renderer='templates/jg.pt')
def y_view(request):
    prm_0 = request.POST.get("prm_0",None)
    prm_1 = request.POST.get("prm_1",None)
    if prm_0 and prm_1:
        print("parameters present")
        return HTTPFound(location=request.route_url("home"))
    else:
        print("no parameters found")
    return {}





**templates/jg.pt**
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml"; xmlns:tal=
"http://xml.zope.org/namespaces/tal"; xml:lang="es" lang="es">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>n</title>
    <script type="text/javascript">
    function postit(){
        console.log("pompom");
        var xhr = new XMLHttpRequest();
        xhr.open("POST", '/test', true);
        xhr.setRequestHeader("Content-Type", 
"application/x-www-form-urlencoded");
        xhr.setRequestHeader("Accept", 
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
);
        xhr.send("prm_0=898&prm_1=603");
    }
    </script>
</head>
<body>
    using XMLHttpRequest..
    <br/>
    <button onclick="postit()">through xmlhttprequest</button>
    <br/>
    wrapped in a form...
    <form method="POST">
        <input type="hidden" name="prm_0" value="3455">
        <input type="hidden" name="prm_1" value="6778">
        <button type="submit">in-form</button>
    </form>
</body>






When i press the button for the xhr i see in browser development tool 2 
requests:
    . name=test, status=302, type=text,html
    . name=localhost, status=200, type=xhr
i can see in the console that the view callable for home is called (prints 
"in my view") but it does not render.

Now, when i press the form button i see in browser development tools many 
requests, being the most important:
    . name=test, status=302, type=text,html
    . name=localhost, status=200, type=document
and this time it does render the home page.

I tried adjusting the xhr so the headers look like the other request..

request header sent using xhr button..
..for test page
POST /test HTTP/1.1
Host: localhost:6543
Connection: keep-alive
Content-Length: 19
Pragma: no-cache
Cache-Control: no-cache
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,
image/apng,*/*;q=0.8
Origin: http://localhost:6543
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like 
Gecko) Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.54
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost:6543/test
Accept-Encoding: gzip, deflate, br
Accept-Language: es-419,es;q=0.9
Cookie: pdtb_active=performance




..for home page
GET / HTTP/1.1
Host: localhost:6543
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,
image/apng,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like 
Gecko) Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.54
Referer: http://localhost:6543/test
Accept-Encoding: gzip, deflate, br
Accept-Language: es-419,es;q=0.9
Cookie: pdtb_active=performance





now using the form button..
..for test page
POST /test HTTP/1.1
Host: localhost:6543
Connection: keep-alive
Content-Length: 21
Pragma: no-cache
Cache-Control: no-cache
Origin: http://localhost:6543
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like 
Gecko) Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.54
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,
image/apng,*/*;q=0.8
Referer: http://localhost:6543/test
Accept-Encoding: gzip, deflate, br
Accept-Language: es-419,es;q=0.9
Cookie: pdtb_active=performance




..home page
GET / HTTP/1.1
Host: localhost:6543
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like 
Gecko) Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.54
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,
image/apng,*/*;q=0.8
Referer: http://localhost:6543/test
Accept-Encoding: gzip, deflate, br
Accept-Language: es-419,es;q=0.9
Cookie: pdtb_active=performance





they looks the same! why is one interpreted as xhr and other as document?
i am thinking that if i get the xhr-button generated request to be treated 
as document it may be rendered well, am i right? is this it? i think i read 
somewhere that the page is not rendered because of the raised exception 
(httpfound) but if it is so, how or when will it work?
in stackoverflow some suggested using "render_to_response" or 
"subrequests", i tried without success and looking at the examples, none of 
them uses declarative style for the views, what makes me think that the 
response that these options generate can not be handled right by the 
renderer.

what is the correct way of accomplishing this? should i stick to forms? why 
does it work but the other does not?

thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/7e872099-8fe0-4339-85ad-6ea067993937%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to