1

html ファイルに ; として定義されたボタンがあります。

<input type="submit" value="Log In" name="login_button" style="width: 109px; "/>

このボタンは sample.com にあります。ユーザーが来てこのボタンをクリックすると、現在の Web ページが login.html で定義された にhttp://127.0.0.1:8000/sample変更されます。http://127.0.0.1:8000/sample2このため、私はやった;

def auth(request):
  if 'login_button' in request.POST:
    // redirect the web page to the sample2.com
  return render_to_response('login.html', {} )

)しようとしましredirect("http://127.0.0.1:8000/sample2たが、うまくいきません。別のページに移動するにはどうすればよいですか。

この関数で定義された他の Web ページ

  def message(request):
          current_date_T = Template ("<p style=\"text-align: right;\">\
                                {{Date|truncatewords:\"8\"}}\
                            </p>")

          # --
          return HttpResponse(html)

URLファイル

  urlpatterns = patterns('',
            ('^sample2/$', message),
            ('^sample/$', auth),
  )

最初に開いたページはサンプルで、その中にボタンがあります。サンプルのボタンがクリックされると、Sample2 が呼び出されます。

4

2 に答える 2

2

redirectの代わりに必要ですrender_to_response。次のことを試してください。

def auth(request):
    if 'login_button' in request.POST:
        # redirect the web page to the /sample2
        return redirect('/sample2/')
    else:
        # Do something else
于 2013-02-04T08:40:12.360 に答える
1

まず、URLに名前を定義します。

urlpatterns = patterns('',
    url('^sample2/$', message, name="message_view"),
    url('^sample/$', authentication, name="auth_view"),
)

次に、URL逆解像度技術を使用して、URLの名前を使用してビューのURLを取得します。

def auth(request):
    if request.method == 'POST':
        // redirect the web page to the sample2.com
        return redirect(reverse('message_view'))
    return render_to_response('login.html', {} )

ここでリダイレクトの目的はわかりません。しかし、それはそれがどのように機能するかです。

于 2013-02-04T08:39:26.883 に答える