0

My python code:

import os
import webapp2
import jinja2
from google.appengine.ext import db

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape=True)

class Handler(webapp2.RequestHandler):
    def write(self, *a, **kw):
        self.response.out.write(*a, **kw)
    def render_str(self, template, **params):
        t = jinja_env.get_template(template)
        return t.render(params)
    def render(self, template, **kw):
        self.write(self.render_str(template, **kw))

class MainPage(Handler):
    #def render_front(self, title="", art="", error=""):
        #self.render("front.html", title=title, art=art, error = error) 

    def get(self): 
        self.render("front.html")

    def post(self):
        title = self.request.get("title")
        art = self.request.get("art")

        if (title and art):
            self.write("Thanks!")       
        #else :
            #self.write("Ooops")
            #error="we need both a title and some artwork!"
            #self.render("front.html", error=error)  

application = webapp2.WSGIApplication([('/',MainPage)],debug=True)

                    

My HTML Code:

<!DOCTYPE html>

<html>
    <head>
        <title>/ascii/</title>
    </head>
    
    <body>
        <h1>/ascii/</h1>
        <form method="post">
            <label>
                <div>title</div>
                <input type="text" name="title" value= "{{ title }}">
            </label>

            <label>
                <div>art</div>
                <textarea name="art">{{ art }}</textarea>
            </label>

            <div class="error">{{ error }}</div>

            <input type="submit">
        </form>
    </body> 
</html> 

I'm following an online class:

(start from the Form Handling)

Problem: (1) The function

#def render_front(self, title="", art="", error=""):
        #self.render("front.html", title=title, art=art, error = error) 

Has problems. If I don't comment it out, the page will have 500 error.

(2)

if (title and art):
            self.write("Thanks!")       
        #else :
            #self.write("Ooops")
         

if I don't comment the else part out, the page will have 500 error also!

Please help! I don't know what's wrong! I guess, maybe it's because I didn't have Jinja2 in my python. But I have run ez_setup.py Jinja2 and it said Jinja2 has installed!

I also tried, in the html file, change value="{{title}}" to value={{title}}, but still doesn't work.

Thanks!!

Community
  • 1
  • 1
fuiiii
  • 1,359
  • 3
  • 17
  • 33
  • 1
    If you get a 500 error in your browser, there will be a full error-message and stack-travce in the console, or in the Log window if you're using the launcher program – Greg Feb 15 '14 at 23:43
  • you don't use easy_setup for jinja it's included in the SDK. Get your error 500 in the logs and confirm what the problem is. If it is to do with jinja2 not being present then read up on the using 3rd party libraries docs https://developers.google.com/appengine/docs/python/tools/libraries27 and configure your app.yaml appropriately – Tim Hoffman Feb 16 '14 at 00:10

1 Answers1

0

Thanks to @Greg !!!

I didn't know the error message is in the log. After checking the error message, I have spotted the bugs!

About the fist problem, it's some space/tab mixed. Same as this post. Problem was spotted by using

python -m tabnanny yourfile.py

About the second problem, it was because I used":" instead of ":". It was a typo.

Community
  • 1
  • 1
fuiiii
  • 1,359
  • 3
  • 17
  • 33