English 中文(简体)
CSS 在与 Python + Jinja2 + GAE 传输“ Get” 请求中的数值时, CSS 在传递数值时出现问题
原标题:CSS rendering issue when passing values in "get" requests with Python + Jinja2 + GAE

这是我第一次在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 %}
    &copy; 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的非常有用的信息,所以我想这可能是张贴这个问题的最佳地方。如果有任何帮助都会感激不尽!谢谢!谢谢!

最佳回答

您用相对路径重新装入 CSS 。 当 HTML 是顶级页面 (/ create_ game) 时, 然后 CSS 路径是相对于 root 的, 一切都是好的。 当您向下一个级别(/ edit_ game/ 1234) 时, CSS 试图从 / edit_ game/ views 装入 CSS 。 更改 HTML 中的链接 hrefs 开始 / views 并可能有效 。 (如果您检查您所选择的浏览器中的 Dev 工具, 您应该看到 CSS 的 404, 您将能够看到浏览器试图从何处装入它们 。)

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签