Python - CSS无法在嵌入式HTML中使用

时间:2016-08-14 17:25:40

标签: python html css

我无法正确连接CSS。它的名字正确。它只是不想工作。我已经尝试了一切可能的方法来输入它。我听过很多不同的事情。有人说你必须输出整个项目的URL(simple-login / css / style.css)。我也听说你只需上升一级(../css/style.css)。我也听说你不必这样做(css / style.css)。似乎什么都不想工作。它总是在常规HTML文件中正常工作。我需要进口一些东西吗?我真的很沮丧。我不应该直接在这个项目中嵌入CSS。它必须是一个单独的文件。

import webapp2 #Use the webapp2 Library

class MainHandler(webapp2.RequestHandler): #declaring a class
    def get(self):
        if self.request.GET: #variables that store the information from the forms. This information will be used in the write() method to put the information on the page. Parts of the html, stored in variables, are strung together in the write() method.
            user = self.request.GET['user']
            email = self.request.GET['email']
            games = self.request.GET['games']
            city = self.request.GET['city']
            state = self.request.GET['state']
            gender = self.request.GET['gender']
            self.response.write(Page.interface_head + Page.interface_body + Page.interface_info + Page.info_name + user + '</br>' + Page.info_email + email + '</br>' + Page.info_games + games + '</br>' + Page.info_city + city + ', ' + state + '</br>' + Page.info_gender + gender + Page.info_close + Page.interface_closing)
        else: #This is what the write method does when there is no response to the request. Meaning this is the page you are met with in the beginning. Parts of the html, stored in variables, are strung together in the write() method.
            self.response.write(Page.interface_head + Page.interface_body + Page.interface_forms + Page.interface_closing)

class Page(object):
        interface_head = '''<!Doctype HTML> <!--This houses all code in the head of my HTML including the style portion.-->
<html>
    <head>
        <title>Simple Form</title>
        <link href="css/style.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>'''

        interface_body = '''<nav><h2>Game<span>Stuff</span></h2></nav> <!--This is the code for the nav all the way down to the forms.-->'''

        interface_info = '''<div class='info_background'> <!--This is the start of the div that houses the information entered from the forms. Below are all the labels split up into vriables. This is for organization and will be reassembled in the if and else statement.-->
            <h3>Is this information correct?</h3>'''
        info_name = '''<label>Name: </label>'''
        info_email = '''<label>Email: </label>'''
        info_games = '''<label>Favorite Games: </label>'''
        info_city = '''<label>City/State: </label>'''
        info_gender = '''<label>Gender: </label>'''
        info_close = '''
        </br><button>Yes</button><button>No</button></div>'''
        interface_forms = '''<form method='GET'><!--These forms enter the information to be stored in the variables in the if statement.-->
            <h3>Sign Up for a free account!</h3>
            <label>Name: </label><input type='text' name='user' />
            <label>Email: </label><input type='text' name='email' /></br>
            <label>Favorite Games: </label><input type='text' name='games' />
            <label>City: </label><input type='text' name='city' style='width: 120px;' />
            <label>State: </label><select name='state'>
            <option value="AL">Alabama</option>
            <option value="AK">Alaska</option>
            <option value="AZ">Arizona</option>
            <option value="AR">Arkansas</option>
            <option value="CA">California</option>
            <option value="CO">Colorado</option>
            <option value="CT">Connecticut</option>
            <option value="DE">Delaware</option>
            <option value="DC">District Of Columbia</option>
            <option value="FL">Florida</option>
            <option value="GA">Georgia</option>
            <option value="HI">Hawaii</option>
            <option value="ID">Idaho</option>
            <option value="IL">Illinois</option>
            <option value="IN">Indiana</option>
            <option value="IA">Iowa</option>
            <option value="KS">Kansas</option>
            <option value="KY">Kentucky</option>
            <option value="LA">Louisiana</option>
            <option value="ME">Maine</option>
            <option value="MD">Maryland</option>
            <option value="MA">Massachusetts</option>
            <option value="MI">Michigan</option>
            <option value="MN">Minnesota</option>
            <option value="MS">Mississippi</option>
            <option value="MO">Missouri</option>
            <option value="MT">Montana</option>
            <option value="NE">Nebraska</option>
            <option value="NV">Nevada</option>
            <option value="NH">New Hampshire</option>
            <option value="NJ">New Jersey</option>
            <option value="NM">New Mexico</option>
            <option value="NY">New York</option>
            <option value="NC">North Carolina</option>
            <option value="ND">North Dakota</option>
            <option value="OH">Ohio</option>
            <option value="OK">Oklahoma</option>
            <option value="OR">Oregon</option>
            <option value="PA">Pennsylvania</option>
            <option value="RI">Rhode Island</option>
            <option value="SC">South Carolina</option>
            <option value="SD">South Dakota</option>
            <option value="TN">Tennessee</option>
            <option value="TX">Texas</option>
            <option value="UT">Utah</option>
            <option value="VT">Vermont</option>
            <option value="VA">Virginia</option>
            <option value="WA">Washington</option>
            <option value="WV">West Virginia</option>
            <option value="WI">Wisconsin</option>
            <option value="WY">Wyoming</option>
        </select>
        <label>Gender: </label><input type="radio" name="gender" value="Male"> Male
        <input type="radio" name="gender" value="Female"> Female
        <input type="radio" name="gender" value="Other"> Other
        <input type="submit" value='Create Account'/> <!--This submit button finalizes all the information put in the forms.-->
        </form>'''

        interface_closing = '''
            <footer>&copy;Made by Matt Lee</footer> <!--This is a footer I made just so there was something final on the page.-->
    </body>
</html>'''

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

1 个答案:

答案 0 :(得分:0)

我建议您使用Jinja 2。它将帮助您组织代码。它是一种模板语言,非常适合与Webapp2一起使用。