Python Jinja2 Google App Engine:从 HTML 选择中获取价值

延斯·贝克瓦尔

我正在使用 Python、Jinja2 和 Google App Engine/Datastore 构建一个网络应用程序。

当尝试为用户实现下拉列表以将多个预定义输入之一提供给数据存储时,来自 HTML 选择的值不会保存到数据存储中。并且在使用了很长时间寻找答案之后,我仍然没有找到解决方案。我希望他们给我的价值是在 lamptype 中的选择。其他一切工作正常,如果我输入单个值而不是多项选择,它工作正常。我做错了什么或没有做什么?

谁能帮我?

我的 HTML:

<form enctype="multipart/form-data" method="post">
            <label>
                <div>Lampens Mærke(skriv ukendt hvis du ikke kender mærket):</div>
                <input type="text" name="brand" value="{{brand}}">
            </label>
            <label>
                <div>Lampens Pæretype(vælg type):</div>
                <select>
                    <option name="lamptype" value="LED">LED</option>
                    <option name="lamptype" value="gloedepare">glødepære</option>
                    <option name="lamptype" value="halogen">halogen</option>
                    <option name="lamptype" value="anden">anden</option>
                </select>
            </label>
            <label>
                <div>Lampens model(eks nummer, type el. lign.):</div>
                <input type="text" name="model" value="{{model}}">
            </label>
            <label>
                <div>Lampens effekt i Watt:</div>
                <input type="number" name="watt" value="{{watt}}">
            </label>
            <label>
                <div>Beskrivelse af lampen(ikke obligatorisk):</div>
                <textarea name="description">{{description}}</textarea>
            </label>
            <!-- <label>
                <div>Billede af lampen(ikke obligatorisk):</div>
                <input type="file" name="image">
            </label> -->
            <div class="error">{{error}}</div>
            <input type="submit">
        </form>

我的主 python 文件中的 Lamp 类:

class Lamp(db.Model):
"""class that creates the basic database structure for Lamp"""
    owner = db.StringProperty(required=True)
    lamptype = db.StringProperty()
    brand = db.StringProperty(required=True)
    model = db.StringProperty(required=True)
    watt = db.IntegerProperty(required=True)
    description = db.TextProperty()
    image = db.BlobProperty()
    booked = db.BooleanProperty()

我用于检索用户输入并将其放入数据存储区的 Python 代码:

#Class below handles new instances of LAMP.
class NewLamp(Handler):
"""class that renders a page for creating a new Lamp instance"""
    def get(self):
        if self.user:
            self.render("newlamp.html")
        else:
            error = "Du skal vaere logget ind for at oprette ting!"
            self.render('login.html', error=error)

    def post(self):
        if not self.user:
            return self.redirect("/mypage/login")

        owner = self.user.name
        brand = self.request.get("brand")
        lamptype = self.request.get("lamptype")
        model = self.request.get("model")
        watt = int(self.request.get("watt"))
        # image = self.request.get("image")
        description = self.request.get("description")

        if brand and model and watt:
            l = Lamp(parent = blog_key(), owner = owner, brand = brand, lamptype = lamptype,
            model = model, watt = watt, description = description)
            l.put()
            time.sleep(0.1)
            self.redirect("/mypage/mythings")
        else:
            error = "Du skal udfylde alle obligatorisk felter!"
            return self.render("newlamp.html", brand = brand, model = model,
            watt = watt, description = description, error = error)
盖凡

您需要为您的select标签添加一个名称属性,如下所示:

<select name="lamptype">

当您提交表单时,name帖子中仅包含具有属性的数据

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Google App Engine上选择Java vs Python

Google App Engine是否支持Python 3?

Google App Engine-Go与Python建议?

Google App Engine-Java还是Python?

如何在GO,Google App Engine中解析HTML模板

如何在Jinja2模板中访问引用对象的属性(Google App Engine)

从Python Google App Engine连接到Firebase

如何在App Engine中从Google Cloud Storage动态加载Jinja2模板?

如何在Google App Engine中清理python中的html blob上传?

Numpy在Python3 Google App Flexible Engine中失败

如何使用python 3.5在Google App Engine中打开文件?

Google App Engine Python 3.7中的多项服务

Google App Engine中的Python 2 Standard环境是否即将被弃用/关闭?

在Google App Engine Python中检测“加载请求”

Google App Engine中的RSA

在Google App Engine中实现远程Api Shell(Python)

带有Python 2.7的Google App Engine中的线程

使用Google App Engine在python中获取网址

Python部署到Google App Engine

在Google App Engine中安装Unirest python模块

使用Google App Engine在python中获取很多网址

在Google App Engine中通过Python查询Google Bigquery

从for循环中以表格的形式获取第n个项目-python / google app engine / jinja2

Python Google App Engine-获取位置

python中的jinja2编码错误

Google App Engine数据存储区Jinja2查询未显示

如何回滚Google App Engine的Python SDK中的appcfg?

使用Python在Google App Engine中执行Java程序

Jinja 2 For 循环使 Google App Engine 崩溃