【问题标题】:(Udacity + GAE) Creating a form to edit existing ndb entities(Udacity + GAE) 创建一个表单来编辑现有的 ndb 实体
【发布时间】:2013-11-14 18:41:03
【问题描述】:

我正在使用由 Udacity 指导的 Google AppEngine (Python) 开始我的第一个小项目。我曾与 Drupal 合作过。

我想在每个工作实体的页面中创建一个“编辑”链接,这将允许其作者更新实体的字段。我不太确定从哪里或如何开始实现这一目标。任何帮助将不胜感激。

这是我到目前为止所做的:

main.py:

import webapp2
import jinja2
import os
import cgi
import urllib
from google.appengine.ext import ndb


template_dir = os.path.join(os.path.dirname(__file__), 'templates')
env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), 
                        autoescape = True)


def render_template(template, **params):
    t = env.get_template(template)
    return t.render(params)


# B A S E H A N D L E R #
#########################

class BaseHandler(webapp2.RequestHandler):

    def write(self, *a, **kw):
        self.response.write(*a, **kw)

    def render(self, template, **kw):
        self.response.write(render_template(template, **kw))


# M A I N P A G E #
###################

class MainPage(BaseHandler):
    def get(self):
        jobs = Job.query()
        self.render('index.html', jobs = jobs)

# J O B   E N T I T Y #
#######################

class Job(ndb.Model):
    title = ndb.StringProperty(required = True)
    description = ndb.TextProperty(required = True)
    requirements = ndb.TextProperty()
    created = ndb.DateTimeProperty(auto_now_add = True)

class JobHandler(BaseHandler):
    pass
    # Created this empty class to organise its subclasses,
    # because they perform 'Job' functions.
    # Is this a good practice?

class NewJob(JobHandler):

    def get(self):
        self.render('newjob.html')

    def post(self):
        title = self.request.get('title')
        description = self.request.get('description')
        requirements = self.request.get('requirements')

        if title and description:
            j = Job(title = title, 
                    description = description, 
                    requirements = requirements)
            j.put()
            self.redirect('/job/%s' % str(j.key.id()))
        else:
            self.render('newjob.html')


class JobPage(JobHandler):

    def get(self, job_id):
        j = Job.get_by_id(int(job_id))
        self.render('jobpage.html', j = j)

app = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/newjob', NewJob),
    ('/job/([0-9]+)', JobPage),
], debug=True)

newpage.html:

{% extends "base.html" %}

{% block content %}

<form method="post">

    <div>
        <label>Job Title:<br> 
            <input type="text" name="title" value="{{title}}">
        </label>
    </div>

    <div>
        <label>Description:<br> 
            <textarea name="description" col="60">{{description}}</textarea>
        </label>
    </div>

    <div>
        <label>Requirements:<br> 
            <textarea name="requirements" col="60">{{requirements}}</textarea>
        </label>
    </div>

    <input type="submit">

</form>

{% endblock %}

最后是jobpage.html

{% extends "base.html" %}

{% block content %}
    {{j.title}}
    {{j.description}}
    {{j.requirement}}
    {{j.created.strftime("%b %d, %Y")}}
{% endblock %}

【问题讨论】:

    标签: python google-app-engine google-cloud-datastore


    【解决方案1】:

    您可能应该向您的网站添加一个编辑处理程序。 ('/job/([0-9]+)/edit', EditJobPage) 之类的东西应该可以。在确认正确的作者登录后,它可以拉起 Job 并将值放在 Get 请求的文本字段中。然后它的 Post 函数可以接受已编辑的字段并相应地更新 Job。

    然后,您可以在请求时将此链接直接添加到 Job html。

    (显然只有在作者登录的情况下才应该这样做)

    <a href="/job/{{j.key.id()}}/edit}">
    

    确保在允许 get 和(更重要的是)post 请求继续执行之前确认作者已登录!

    编辑: 我实际上并没有回答您有关如何编辑现有 ndb 实体的问题。完成的方法是查询实体,将值重新分配给实体变量,然后放入。

    job_to_edit = Job.get_by_id(job_id)  # or ndb.Key(Job, job_id).get()
    job_to_edit.description = new_description
    job_to_edit.put()
    # Tada!
    

    免责声明:我唯一的经验是参加这个 Udacity 课程。

    【讨论】:

    • 谢谢你。这是有道理的。您如何使用 get() 从 URL 中获取 job_number(当前正在查看的工作)?
    • @haopei Welcome :) 你会像在 JobPage 处理程序的 get 函数中一样得到它。它将作为第二个参数传递。另外,请查看我编辑的答案,了解如何轻松访问工作编号以粘贴到 html 中。
    • @haopei 感谢您的采纳!祝你课程顺利,我从中受益良多。
    • 感谢 帮助我度过了这个难关。非常欣赏。我有a follow up question,如果你愿意的话:)
    猜你喜欢
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多