2015-09-09 4 views
0

В чем проблема? Статьи хранятся в объекте articleArchive, но я не могу заставить его отображаться в моем HTML. Где id = статья в html, где я хочу, чтобы запрос отображался. Благодаря!Google App Engine Datastore Jinja2 Query Не отображается

Python код:

import os 
import jinja2 
import webapp2 
import time 

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) 

#CREATE DATABASE ENTITIY/CHART CLASS to store all articles, titles, 
# subheadings, dates posted, author names, article ID, tags, etc. 
class articleArchive(db.Model): 
    title = db.StringProperty(required = True) 
    article = db.TextProperty(required = True) 
    author = db.StringProperty(required = True) 
    datetime = db.DateTimeProperty(auto_now_add = 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 homePage(Handler): 
    def get(self): 
     self.render("homePage.html") 

class submitPage(Handler): 
    def render_post(self, error="", title="", article="", author=""): 
     articles = db.GqlQuery("SELECT * FROM articleArchive ORDER BY datetime DESC") 
     self.render("submit.html", error=error, title=title, article=article, author=author) 

    def get(self): 
     self.render_post() 

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

     if title and article and author: 
      a = articleArchive(title = title, author = author, article = article) 
      a.put() 
      self.redirect("/submit") 

     else: 
      error = "The title, author, and article fields must be filled out. " 
      self.render_post(error, title, article, author) 

class singlePost(Handler): 
    def post(self): 
     self.render(submit.html) 

app = webapp2.WSGIApplication([('/', homePage), 
          ('/submit', submitPage), 
          ('/id', singlePost) 
          ], 
           debug=True) 

HTML:

{% extends "base.html" %} 
<!DOCTYPE html> 

<html> 
    <head> 
     {% block head %} 
     <title>Submit a Post!</title> 
     <link type="text/css" rel="stylesheet" href="/stylesheets/submit.css"> 
     {% endblock %} 
    </head> 

    <body> 
     {% block content %} 

      <h1>Article Submission</h1> 

      <form method="post" id="lol"> 
       <label> 
        <div>Title</div> 
        <input type="text" name="title" value={{title}}> 
       </label> 

       <br> 
       <br> 

       <label> 
        <div>Author</div> 
        <input type="text" name="author" value={{author}}> 
       </label> 

       <br> 
       <br> 

       <label> 
        <div>Article</div> 
        <textarea type="text" name="article" form="lol" value={{article}}></textarea> 
       </label> 

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

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

      <div id = "article"> 
       {% for article in articles %} 
        <h1> {{article.title}} </h1> 
        <h2> {{article.author}} </h2> 
        <h3> {{article.datetime}} </h3> 
        <br> 
        <pre> {{article.article}} </pre> 
        <hr> 
       {% endfor %} 
      </div> 

     {% endblock %} 
    </body> 
</html> 
+1

Вы представляете «статью», и вы перебираете «статьи», замечаете «s». – YOBA

+0

Где находится "s" отсутствует? Я не понимаю, что случилось. Я благодарен за ответ! –

+0

Если вы только начинаете, вам следует переключиться с 'db' на' ndb'. Это лучший API для доступа к хранилищу данных. –

ответ

1

Ваша render_post функция запроса и присвоения articles но вы передаете функцию kwargarticle к вашей render функции. Это означает, что в вашем шаблоне jinja2 articles всегда неопределен и молча игнорируется.

class submitPage(Handler): 
    def render_post(self, error="", title="", article="", author=""): 
     articles = db.GqlQuery("SELECT * FROM articleArchive ORDER BY datetime DESC") 
     self.render("submit.html", error=error, title=title, article=article, author=author) 
Смежные вопросы