1 頁 (共 1 頁)

Templates

發表於 : 週六 8月 23, 2014 7:29 pm
rusli
1. Create a file "helloworld.html"
{/dev/boardgames/templates/helloworld.html}

代碼: 選擇全部

<!DOCTYPE HTML>
<html>
<head>
  <title>Hello, World!</title>
</head>
<body>
   <h1> Hello, World!</h1>
   It is now {{date}}.
</body>
</html>


2. create a views.py
{/dev/boardgames/boardgames/views.py}

代碼: 選擇全部

from django.views.generic.base import View
from django.shortcuts import render_to_response
from datetime import date

class HelloWorldView(View):
    def get(self, request):
        return render_to_response(''helloworld.html',
                       {'date' : date.today()})

add TemplateView

發表於 : 週六 8月 23, 2014 7:32 pm
rusli
2. modify views.py
{/dev/boardgames/boardgames/views.py}

代碼: 選擇全部

from django.views.generic.base import TemplateView
from datetime import date

class HelloWorldView(TemplateView):
      template_name="helloworld.html"   

      def get_context_date(self, **kwargs):
             return {'date':date.today()}