这是我第一次在Stack Overflow 上张贴。 我试图用 Python、 Jinja2 和 Google App 引擎构建一个相对简单的游戏。 目前, 我刚刚为创建、 编辑、 列表和销毁游戏创建了一些 CRUD 简单功能。 大部分情况下, 它看起来是有效的。 但是, 我似乎没有在每次我通过一个值来检索特定游戏时应用我的 CSS 样式表。 但是, 如果我调用“ 创建游戏” 、 “ 全部游戏” 或“ 全部游戏” 等函数, 而不使用任何参数, 那么它们各自的模板会用相关的 CSS 样式被正确转换 。 但是, 如果我调用“ 编辑_ game/ 12345567 ” 这样的函数, 那么 CSS 样式表似乎根本不应用, 即使 html 标签和 python 生成的内容似乎有效 。
我不知道问题在哪里。 这和模板的制作方式有什么关系吗? 还是路由/绘图问题?
这是我的主.py:
import webapp2
from controllers.authController import *
from controllers.baseController import *
from controllers.gameController import *
app = webapp2.WSGIApplication([( / , MainPage),
( /create_game ,CreateGame),
( /player_games/([d]+) ,PlayerGames),
( /all_games ,AllGames),
( /edit_game/([d]+) ,EditGame),
( /delete_game/([d]+) ,DeleteGame),
],
debug=True)
和我的基地 主计长。 py:
from controllers.authController import LoginSession
import webapp2
import cgi
import datetime
import urllib
import jinja2
import os
import wsgiref.handlers
from google.appengine.api import users
TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), ../views/templates )
jinja_environment =
jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_DIR))
class BaseHandler(LoginSession):
@webapp2.cached_property
def jinja2(self):
return jinja2.get_jinja2(app=self.app)
def render_template(
self,
filename,
temp_values,
**template_args):
template = jinja_environment.get_template(filename)
self.response.out.write(template.render(temp_values))
def authvals(self):
current_player = LoginSession.current_player(self)
url = LoginSession.url(self)
url_linktext = LoginSession.linktext(self)
auth_vals = {
url : url,
url_linktext : url_linktext,
c_player : current_player,
}
return auth_vals
class MainPage(BaseHandler):
def get(self):
gurl = self.request.get( /create_game )
gurl_linktext = Create a New Game!
mp_vals = {
game_url : gurl,
game_url_linktext : gurl_linktext,
}
authvals = self.authvals()
vals = dict(mp_vals.items() + authvals.items())
self.render_template( index.html , vals)
下面是游戏管理员. Py 的两类。 虽然“ AllGames” 似乎使所有的东西都是正确的, 包括正确的 CSS 样式, “ PlayerGames” 似乎使 Python 生成的内容和 html 成为正确的 Python 生成的内容和 html, 但似乎没有使用任何 CSS 。 我希望 CSS 和“ base. html ” 一起继承, 因为这就是 CSS 风格正在应用的地方。 但是, 不知怎的, 这并没有发生 。
from models.gameModel import *
from authController import *
from baseController import *
import baseController
from google.appengine.ext import db
from google.appengine.ext.db import Key
class AllGames(BaseHandler):
#this returns all games
def get(self):
games = GameModel.all()
ag_vals = {
games : games
}
authvals = self.authvals()
vals = dict(ag_vals.items() + authvals.items())
self.render_template( all_games.html ,vals)
class PlayerGames(BaseHandler):
#This returns a given player s games
def get(self,player_id):
player = users.User(_user_id = player_id )
g = GameModel.all()
games = g.filter( game_created_by = , player)
pg_vals = {
games : games
}
authvals = self.authvals()
vals = dict(pg_vals.items() + authvals.items())
self.render_template( player_games.html ,vals)
这里是基点. html :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
{% block head %}
<link type="text/css" rel="stylesheet" href="views/static/css/main.css" />
<link type="text/css" rel="stylesheet" href="views/static/css/reset.css" />
<title>O2O: {% block title %}{% endblock %}</title>
{% endblock %}
</head>
<body>
<div class="login">
{% block login %}
<table>
<tr>
{% if c_player %}
<td>You are logged in as: {{ c_player }} </td>
{% else %}
<td>You are not logged in.</td>
{% endif %}
<td><a href="{{ url }}">{{ url_linktext }}</a></td>
</tr>
</table>
{% endblock login %}
</div>
<div class="navbar">
{% block navbar %}
<table>
<col class="navbar" />
<colgroup class="controls">
<col /> <col /> <col /> <col />
</colgroup>
<tr>
<th>
<form action="/create_game" method="post">
<div><input type="submit" value = "Create a new Game!"></div>
</form>
</th>
<th>
<form action="/player_games/{{ c_player.user_id() }}" method="get">
<!--div><input type="hidden" value = {{ c_player.user_id() }} ></div-->
<div><input type="submit" value = "My Games" ></div>
</form>
</th>
<!--th>
<a href ="player_games/{{ c_player.user_id() }}">My Games</a>
</th-->
<th>
<form action="/all_games" method="get">
<div><input type="submit" value = "All Games" ></div>
</form>
</th>
<th>
Stats?
</th>
<th>
Current Games?
</th>
</tr>
</table>
{% endblock navbar %}
</div>
<div class="content">{% block content %}{% endblock %}</div>
<div class="footer">
{% block footer %}
© Copyright 2012</a>.
{% endblock %}
</div>
</body>
</html>
这里是所有_ games. html, 似乎可以正确表达 :
{% extends "base.html" %}
{% block title %} All Games {% endblock %}
{% block content %}
<h2>All Games</h2>
<h4>These are all of the games:</h4>
<table>
<tr>
<th>Time Created: </th>
<th> ---------- </th>
<th>Game ID: </th>
<th>Creator: </th>
<th>Edit? </th>
<th>Edit via button? </th>
<th>Delete? </th>
</tr>
{% for game in games %}
<tr>
<td> {{ game.game_created_at }} </td>
<td> ---------- </td>
<td> {{ game.key().id() }} </td>
<td> {{ game.game_created_by }} </td>
<td>
<a href ="edit_game/{{ game.key().id() }}"> edit </a>
</td>
<td>
<!--button to view the game -->
<form action="/edit_game/{{ game.key().id() }}" method="get">
<!--div><input type="hidden" name ="game_id" value={{ game.key().id() }}></div-->
<div><input type="submit" value = "Edit Game!" ></div>
</form>
</td>
<td>
<!-- button to destroy the game -->
<form action="/delete_game/{{ game.key().id() }}" method="get">
<!--div><input type="hidden" name ="this_game" value={{ game.key().id() }}></div-->
<div><input type="submit" value = "Delete This Game?" ></div>
</form>
</td>
</p>
</tr>
{% endfor %}
</table>
{% endblock %}
这是玩家_games.html, 几乎和所有_games.html完全相同。 然而, 这似乎没有 CSS 显示, 尽管 html 和 内容显然显示得正确 :
{% extends "base.html" %}
{% block title %} PLayer s Games {% endblock %}
{% block content %}
<h2>{{ c_player }} s Games</h2>
<h4>These are all of the games:</h4>
<div id="gamelist">
<table>
<tr>
<th>Time Created: </th>
<th> ---------- </th>
<th>Game ID: </th>
<th>Creator: </th>
<th>Edit?</th>
<th>Delete?</th>
</tr>
{% for game in games %}
<tr>
<td> {{ game.game_created_at }} </td>
<td> ---------- </td>
<td> {{ game.key().id() }} </td>
<td> {{ game.game_created_by }} </td>
<td>
<!--button to edit the game -->
<form action="/edit_game/{{ game.key().id() }}" method="get">
<!--div><input type="hidden" name ="game_id" value={{ game.key().id() }}></div-->
<div><input type="submit" value = "Edit Game!" ></div>
</form>
</td>
<td>
<!-- button to destroy the game -->
<form action="/delete_game/{{ game.key().id() }}" method="get">
<div><input type="submit" value = "Delete This Game?" ></div>
</form>
</td>
</p>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}
以下是主页, 可能不是问题所在, 因为有些页面正确提供:
.login {
background-color: black;
color: #DDDDDD;
}
.navbar {
background-color: black;
color: #DDDDDD;
}
.content {
background-color: white;
}
.footer {
background-color: #DDDDDD;
}
虽然我怀疑这与问题真的有关,
import webapp2
import main
from google.appengine.api import users
class LoginSession(webapp2.RequestHandler):
def current_player(arg):
return users.get_current_user()
def linktext(arg):
if users.get_current_user():
return logout
else:
return Login
def url(arg):
if users.get_current_user():
return users.create_logout_url(arg.request.uri)
else:
return users.create_login_url(arg.request.url)
I ve based some of my code on this simple note-taking app example: https://github.com/fRuiApps/cpfthw/tree/master/webapp2 because I found it to be have a nice, clear MVC structure. Obviously my code is quite a bit more extensive, but some of the code for how the templates should be rendered is more or less based on the example. However, the example does not suffer from the disappearing CSS style problem that I seem to be having. I.e. when I pass requests to edit a specific note, the example appears to render the CSS correctly.
我试图解决这个问题已有几个小时了,尽管我做了许多努力,我似乎一无所获。因为我通常能够找到关于StackOverflow的非常有用的信息,所以我想这可能是张贴这个问题的最佳地方。如果有任何帮助都会感激不尽!谢谢!谢谢!