按按鈕回答時間
- python datetime
- jQuery+ajax
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>現在幾點啦</title> | |
| <!-- Bootstrap4 --> | |
| <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous"> | |
| <!-- JQuery --> | |
| <script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script> | |
| </head> | |
| <body> | |
| <div class="row"> | |
| <div class="col-4"></div> | |
| <div class="col-4" style="padding:40px;text-align: center;"> | |
| <button id="HourMinute" class="btn btn-warning">現在幾點幾分啦</button> | |
| <button id="weekday" class="btn btn-warning">今天星期幾</button> | |
| <button id="year" class="btn btn-warning">今年西元幾年</button> | |
| <h2 id="output" style="padding: 50px"></h2> | |
| </div> | |
| <div class="col-4"></div> | |
| </div> | |
| <script> | |
| $("#HourMinute").click(function(){ | |
| $.ajax({url: "ajax", success: function(result){ | |
| $("#output").html(result['HourMinute']); | |
| }}); | |
| }); | |
| $("#year").click(function(){ | |
| $.ajax({url: "ajax", success: function(result){ | |
| $("#output").html(result['year']); | |
| }}); | |
| }); | |
| $("#weekday").click(function(){ | |
| $.ajax({url: "ajax", success: function(result){ | |
| $("#output").html(result['weekday']); | |
| }}); | |
| }); | |
| </script> | |
| </body> | |
| </html> |
| from django.shortcuts import render | |
| from django.http import JsonResponse | |
| from datetime import datetime | |
| def index(request): | |
| return render(request, 'whatTime/index.html', {}) | |
| def ajax(request): | |
| now = datetime.now() | |
| data = { | |
| 'HourMinute': now.strftime('%H : %M'), | |
| 'weekday': now.strftime('%A'), | |
| 'year': now.strftime('%Y'), | |
| } | |
| return JsonResponse(data) |