从python重定向到变量href

时间:2015-08-03 03:06:45

标签: python html5

我正在为Udacity分配编写一个wiki,并希望在导航栏中创建一个“编辑”按钮。基本上我正在寻找的功能是当你点击编辑时,你被重定向到编辑页面,当前内容显示在编辑框中。保存该页面时,内容将在原始页面上更新。

我相信我的问题在html中如下:

<body>
    <div class="navbar">
        {% if not user %}
        <a href="/signup">Signup</a>|<a href="/login">Login</a>
        {% else %}
        <a href="/_edit{{wiki_url}}">Edit</a>|<a href="/logout">Logout</a>
        {% endif %}
    </div>
    <div id="content">
        {% block content %}
        {% endblock %}
    </div>
</body>

相关的python代码是:

class WikiPage(Handler):
    def get(self, wiki_url):
        page = None
        ver = self.request.get("v")
        user = self.user
        print("***** WikiPage(Handler) wiki_url = %s, ver = %r, user = %s" % (wiki_url, ver, user))
        if ver:
            ver = int(ver)
            page = db.GqlQuery("SELECT * FROM Wiki WHERE url = :url AND version = :ver ORDER BY created DESC", url=wiki_url, ver=ver).get()
            if not page:
                self.redirect("%s" % wiki_url)
        else:
            page = db.GqlQuery("SELECT * FROM Wiki WHERE url = :url ORDER BY created DESC LIMIT 1", url = wiki_url).get()
            print("*** No versions *** wiki_url=%s page = %r" % (wiki_url, page))
            if not page:
                self.redirect("/_edit%s" % wiki_url)

        if page:
            id = page.key().id()
            thisPage = Wiki.get_by_id(int(id))
            print("*** WikiPage(Handler) *** wiki_url = %s page = %r" % (wiki_url, page))
            if user:
                self.render('show_page.html', content=thisPage.content, url=wiki_url)
            else:
                self.redirect("/login")

现在发生的事情是,无论我正在尝试编辑哪个页面,我都会被重定向到/ _edit / _edit。

任何建议都将不胜感激。

添加EditPage处理程序类:

class EditPage(Handler):
def render_editwiki(self, user="", title="", content="", error=""):
    self.render("edit_page.html", user=user, title=title, content=content, error=error)

def get(self, wiki_url):
    v = self.request.get('v')
    user = self.user

    if user:
        if v:
            v = int(v)
            thisWiki = db.GqlQuery("SELECT * FROM Wiki WHERE url = :url AND version = :ver ORDER BY created DESC", url=wiki_rul, ver=v).get()
            if not thisWiki:
                self.redirect("/wiki/_edit%s" % wiki_url)
        else:
            thisWiki = db.GqlQuery("SELECT * FROM Wiki WHERE url = :url ORDER BY created DESC LIMIT 1", url=wiki_url).get()
            if not thisWiki:
                self.render_editwiki(user, wiki_url)

        if thisWiki:
            id = thisWiki.key().id()
            w = Wiki.get_by_id(int(id))
            self.render_editwiki(user, w.url, w.content)
    else:
        self.redirect("/wiki/signup")

def post(self, wiki_url):
    content = self.request.get("content")
    user = self.user

    if user:
        if content:
            thisWiki = db.GqlQuery("SELECT * FROM Wiki where url = :url ORDER BY created DESC LIMIT 1", url=wiki_url).get()
            if thisWiki:
                id = thisWiki.key().id()
                previous = Wiki.get_by_id(int(id))
                current = previous.version + 1
                w = Wiki(url=wiki_url, content=content, version=current)
                w.put()
            else:
                w = Wiki(url=wiki_url, content=content,version=1)
                w.put()
            self.redirect('%s' % str(wiki_url))
        else:
            error = "No Content"
            self.render("edit_page.html", content=content, error=error)
    else:
        self.redirect("/wiki/signup")

1 个答案:

答案 0 :(得分:0)

您是否已为编辑网址编写了网址案例?您似乎被重定向到/_edit/_edit,因为wiki_url/_edit。我猜这就是发生的事情:

  • 您访问常规页面(wiki_url = ?
  • 点击修改网址
  • 您访问了修改网址,但没有网页(wiki_url = '/_edit'
  • 您被重定向到(编辑页面的)编辑网址,即/_edit/_edit

这有意义吗?