1

私はこのDjangoアプリケーションを持っており、ユーザーが「localhost:8000 / time /」を要求すると、input.htmlからhtmlが表示されます。

<head>

<!-- paste this in your footer -->
<script type="text/javascript"> 
$(document).ready(function() {
        $('#myForm').submit(function() { // catch the form's submit event
            $.ajax({ // create an AJAX call...
                data: $(this).serialize(),
                type: $(this).attr('POST'), // "post"
                url: $(this).attr('/poi/'), //  "/poi/"
                success: function(response) { 
                    // do something here
                }
            });
        return false;
    });
});
</script>
<!-- add jquery or it won't work -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>

<body>
        <form action="/poi/" method = "post">

        <!---------
        first name :<input type ="text" name ="fname">
        last name:<input type ="text" name ="lname">
        ------>

        enter a days :<input type ="text" name ="days">
        <br>
        <input type=submit name="submit" onclick="submit_function(); return false;">
        </form>

</body>

フォームが送信されると、ユーザーにはresponse.htmlページが表示され、URLが「localhost:8000 /poi/」に変更されます。

urls.py

urlpatterns = patterns('',
                            (r'^hello/$', hello),   
                            ('^time/$', current_datetime),
                            ('^poi/$', next_datetime),

                        )

views.py

def current_datetime(request):  
    return render_to_response('input.html')


def next_datetime(request):

    now = datetime.datetime.now()
    now_day = now.day
    now_month = now.month
    now_year = now.year

    return render_to_response('response.html', {'now_day': now_day, 'now_month'}}

今、私は同じことをしなければなりませんが、URLは「localhost:8000 / time /」から「localhost:8000 / poi /」に変更されるべきではありませんが、「localhost:8000 /time/」である必要があります

それを達成する方法は?

4

1 に答える 1

1

これを実現するには、ajaxを使用する必要があります。jqueryを使用すると、次のようになります。

<form id="myForm"> ... </form> <!-- add an id to your form -->

<!-- paste this in your footer -->
<script type="text/javascript"> 
$(document).ready(function() {
        $('#myForm').submit(function() { // catch the form's submit event
            $.ajax({ // create an AJAX call...
                data: $(this).serialize(),
                type: $(this).attr('method'), // "post"
                url: $(this).attr('action'), //  "/poi/"
                success: function(response) { 
                    // do something here
                }
            });
        return false;
    });
});
</script>
<!-- add jquery or it won't work -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

箱から出して動作するはずです

これは無名関数を使用しますが、名前付きのコード内にコードを配置して、ボタンに追加することができます。

<input type="submit" onclick="submit_function(); return false;" />

基本的に同じです

于 2012-10-15T13:16:56.857 に答える