【问题标题】:Redirecting to a url with an id in it, based on what the user clicks根据用户点击的内容重定向到带有 id 的 url
【发布时间】:2019-05-20 18:50:51
【问题描述】:

我已阅读其他解决方案,但没有一个有帮助。所以这个想法是,用户发送消息,这些消息有回复,查看回复,当用户点击超链接时,它会将用户重定向到/reply/<message_id>。我在下面附上了相关代码。

app.py:

@app.route('/reply/<message_id>')
def reply(message_id):
    return render_template('reply.html', message_id = message_id)

index.html:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.min.js"></script>
        <script type="text/javascript">
          var socket = io.connect('http://' + document.domain + ':' + location.port);
          var message_id = 0 ;
          socket.on( 'connect', function() {
            socket.emit( 'my event', {
              data: 'User Connected'
            } )
            var form = $( 'form' ).on( 'submit', function( e ) {
              message_id++
              e.preventDefault()
              let user_input = $( 'input.message' ).val()
              socket.emit( 'my event', {
                message_id : message_id,
                message : user_input
              } )
              $( 'input.message' ).val( '' ).focus()
            } )
          } )
          console.log("we are before the method,a nd mid is " + mid + " of type " + typeof mid)
          socket.on( 'my response', function( msg ) {
            console.log( msg )
            if( typeof msg.message_id !== 'undefined' ) {
              $( 'h3' ).remove()
              console.log("msg.message_id is " + msg.message_id + typeof msg.message_id)
              var mid = msg.message_id
              console.log("we are before the method,a nd mid is " + mid + " of type " + typeof mid)
              $( 'div.message_holder' ).append( '<div>'+mid+'  '+msg.message+'  <a href="{{ url_for('reply', message_id='X') }}"'.replace('X', mid)'>reply</a></div>' ))
            }
          })

        </script>

回复.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>The reply </title>
  </head>
  <body>
    <h1>Hello, your message id was  {{ message_id }}!</h1>
  </body>
</html>

目前发生的情况是,当我单击reply 时,它会将我重定向到/reply/,然后说找不到。我希望它能够将我重定向到 /reply/2 或我点击的任何消息 ID。感谢您的帮助

【问题讨论】:

  • 尝试在 index.html 中指定的行之前添加console.log("Mid value is: " + mid); 行,我怀疑它未定义
  • 我做到了,它给出了号码,我什至做到了typeof

标签: javascript python jquery html flask


【解决方案1】:

假设mid 是一个javascript 变量,您不能将它与Jinja 代码混合使用。 Jinja 渲染会将mid 解释为 Python 变量,在您的情况下,该变量在渲染时尚未设置。

要解决这个问题,请将变量移出 Jinja 代码。要保持url_for 正常工作,您仍需要为message_id 提供一个值。例如:

...' <a href="{{ url_for('reply', message_id='X') }}"'.replace('X', mid) + '>...'

【讨论】:

  • 为此,我得到“POST / HTTP/1.1” 405 - Method Not Allowed 请求的 URL 不允许该方法。有什么建议吗?
  • 这很混乱,你能在路由定义中添加methods=['GET'],并在你的问题中添加完整的html吗?
  • 将 methods=['GET'] 添加到路由(索引)。它似乎不起作用。我还在上述问题的 html 文件中包含了主要位。谢谢
猜你喜欢
  • 2011-04-10
  • 1970-01-01
  • 1970-01-01
  • 2018-08-27
  • 2020-01-23
  • 1970-01-01
  • 1970-01-01
  • 2017-08-22
  • 2017-11-29
相关资源
最近更新 更多