模版1-django

模版1-django

@(python)html

  • 爲了測試效果,經過admin後臺管理添加一些實例python

    +----+-------+------+-----+-------------+
    | id | name | sex | age | email |
    +----+-------+------+-----+-------------+
    | 1 | liton | male | 18 | qq@163.com |
    | 2 | mike | male | 19 | mike@qq.com |
    | 3 | mach | male | 20 | mach@qq.com |
    +----+-------+------+-----+-------------+sql

  • for 標籤
    • 修改users文件夾下的views.py的代碼,以下
    from django.shortcuts import render   
           from django.shortcuts import render_to_response  
           from users.models import Info  
           def show(request):  
              r = Info.objects.all()  
           return render_to_response('show.html', {'user_list':r})
    • 修改urls.py,添加url('^show/$', 'users.views.show'),
    from django.conf.urls import include, url    
        from django.contrib import admin    
        from mysite.views import hello    
        from mysite.views import current_datetime   
        urlpatterns = [   
        # Examples:   
        # url(r'^$', 'mysite.views.home', name='home'),   
        # url(r'^blog/', include('blog.urls')),   
        url(r'^admin/', include(admin.site.urls)),   
        url(r'^hello/$', hello),   
        url('^time/$', current_datetime),   
        url('^info/$', 'mysite.views.info'),   
        url('^show/$', 'users.views.show'),   
           ]
    - 添加模版文件,在templates文件夾下新建show.html
    <html>
          <head>
          <title>測試Django模板</title>
          </head>
          <body>
          {% for item in user_list %}
               <p>{{ forloop.counter }}: {{ item.name }}</p>
          {% endfor %}
         </body>
         </html>
    - 打開瀏覽器http://ip:8000/show/,顯示以下
    1: liton   
        2: mike   
        3: mach
    - forloop.counter 老是一個表示當前循環的執行次數的整數計數器。 這個計數器是從1開始的,因此在第一次循環時 forloop.counter 將會被設置爲1。
    - forloop.counter0 相似於 forloop.counter ,可是它是從0計數的。 第一次執行循環時這個變量會被設置爲0。
    - forloop.revcounter 是表示循環中剩餘項的整型變量。 在循環初次執行時 forloop.revcounter 將被設置爲序列中項的總數。 最後一次循環執行中,這個變量將被置1。
    - forloop.revcounter0 相似於 forloop.revcounter ,但它以0作爲結束索引。在第一次執行循環時,該變量會被置爲序列的項的個數減1。
    - forloop.first 是一個布爾值,若是該迭代是第一次執行,該值爲真True,以後爲False.
    -  forloop.last 是一個布爾值;在最後一次執行循環時被置爲True。
    -  forloop 變量僅僅可以在循環中使用。 在模板解析器碰到`{% endfor %}`標籤後,forloop就不可訪問了
    - locals()

    locals()返回一個包含當前做用域裏面的全部變量和它們的值的字典。不須要手動構造字典。
    for標籤的例子,與下面的代碼獲得的效果是同樣的。django

    from django.shortcuts import render    
         from django.shortcuts import render_to_response    
         from users.models import Info    
         def show(request):     
                user_list = Info.objects.all()       
         return render_to_response('show.html',locals())
  • context標籤
    上下文context標籤用於複用公共的變量,甚至全局變量。
    修改users下的views.py文件
from django.shortcuts import render  
     from django.shortcuts import render_to_response  
     from django.template import RequestContext           
     from users.models import Info   
     def show(request):   
        #r = Info.objects.all()  
        #return render_to_response('show.html', {'user_list':r})   
        user_list = Info.objects.all()   
        return render_to_response('show.html',locals())   
     def custom_proc(request):   
        return {         
                    'title': 'django',   
                    'category': 'context',   
                }   
     def custom_proc_1(request):   
        return {       
                    'section': '1',   
                }   
     def show_context(request):   
        user_list = Info.objects.all()   
        return render_to_response('show_context.html',     {'user_list':user_list},context_instance=RequestContext(request, processors=[custom_proc,custom_proc_1]))

新增模版文件show_context.html瀏覽器

<html>
     <head>
     <title>{{title}}:{{category}}:{{section}}</title>
     </head>
     <body>
     <p>{{title}}:{{category}}:{{section}}</p>
     {% for item in user_list %}
        <p>{{ forloop.counter }}: {{ item.name }}</p>
     {% endfor %}
     </body>
     </html>

同時新增url規則
url('^show_context/$', 'users.views.show_context'),
django自帶context_processor的字典app

def auth(request):
    return {
        'user': user,
        'messages': user.get_and_delete_messages(),
        'perms': PermWrapper(user),
    }
def debug(request):
    context_extras = {}
    context_extras['debug'] = True
    context_extras['sql_queries'] = connection.queries
    return context_extras
def i18n(request):
    context_extras = {}
    context_extras['LANGUAGES'] = settings.LANGUAGES
    context_extras['LANGUAGE_CODE'] = translation.get_language()
    context_extras['LANGUAGE_BIDI'] = translation.get_language_bidi()
    return context_extras
def media(request):
    return {'MEDIA_URL': settings.MEDIA_URL}
def request(request):
    return {'request': request}

參考文獻

http://blog.csdn.net/cain/article/details/6623717oop

相關文章
相關標籤/搜索