문제

I'm trying to make a simple web script using the PythonAnywhere.com's web.py platform. My intention is to create a simples Form which gets the data of textboxes and is able to work with them just like we do with PHP and so. This is my main script:

import web
from web import form
import MySQLdb

render = web.template.render('/home/user/templates/')

conn = MySQLdb.connect("mysql.server","user","*********","userdb")
curs = conn.cursor()

curs.execute('''create table if not exists Dados (
id int not null auto_increment primary key,
nome varchar(200),
item1 varchar(50),
item2 varchar(50),
item3 varchar(50),
item4 varchar(50),
item5 varchar(50));
''')

urls = (
  '/', 'index'
)

formula = form.Form(
    form.Textbox('Nome', id='nome'),
    form.Textbox('Item 1', id='it1'),
    form.Textbox('Item 2', id='it2'),
    form.Textbox('Item 3', id='it3'),
    form.Textbox('Item 4', id='it4'),
    form.Textbox('Item 5', id='it5'),
    )


class index:
    def GET(self):
        form = formula()        
        return render.formtest(form)

    def POST(self):
        form = formula()        
        return render.finaliza(form['Nome'].value)

# comment out these two lines if you want to use another framework
app = web.application(urls, globals())
application = app.wsgifunc()

Then I have two templates in HTML, this one stores the Form:

$def with (form)

<form name="main" method="post"> 
$:form.render()
<input type="submit" name="send" id="envia" value="Ok" />    </form>

And this should give the result after the POST:

$def with (nome)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
Congratulations $:nome !
</body>
</html>

Everything works fine until a press the Ok button. It shows the right template but doesn't show the $nome variable. You can check this behavior on this link: http://jonathan_hepp.pythonanywhere.com/

I'm beginning on web.py and PythonAnywhere so there must be something I'm doing wrong but I can't find it out. Could you please help me out? Thanks.

EDIT:

I've just find out now that if I pass the textbox value as a string the result is different. The result page says "Congratulations None!" That makes me think that actually the POST is not recieving the value I'm typing in the textbox. So the code seems to be ok but somehow I didn't make it right so it cannot reach the value in the form's input. Still not working.

SOLVED:

Ok. I realized that the form.Form() option doesn't really create and html form output. Actually when you look at the source code of the formtest page you see that what I supposed was the form appears as a simple . So I just made the form into the formtest template directly in html an now it works pretty well. Just a dumb mistake, but if somebody else comes throught it, just do the same. Thank you all.

도움이 되었습니까?

해결책

It seems like the variable in the finaliza template should actually be "Nome". So:

Congratulations $:Nome ! 

Might do the trick. Remember to restart your web app on the Web tab of PythonAnywhere after making changes or you won't see them.

다른 팁

I am using the same web.py framework. when I try to access it via localhost and URL after is /templates/tutorial.html

then I can see "send" button but with these $ signs

$def with (form, text) $:form.render()

$text

Here is tutorial.html

$def with (form, text)

<head>
    <title>Python and AJAX tutorial for beginners with webpy and jQuery</title>
    <link rel="stylesheet" type="text/css" href="/static/tutorial.css" />

    <script type="text/javascript" src="/static/jquery.js"></script>

    <script type="text/javascript">
                            jQuery(document).ready(function() {
                            jQuery(".button").click(function() {
                                    var input_string = $$("input#textfield").val();
                                    jQuery.ajax({
                                            type: "POST",
                                            data: {textfield : input_string},
                                            success: function(data) {
                                            jQuery('#foo').html(data).hide().fadeIn(1500);
                                            },
                                            });
                                    return false;
                                    });
                            });

                    </script>
</head>

<body>
    <br>
    <form class="form" method="post"> 
    $:form.render()
    <input class="button" type="submit" value="send"/>    
    </form>

    <br><br>
    <span id="foo">$text</span>        
</body>

How I can get the values of these variables ?

$def with (form, text) $:form.render()

$text

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top