zoukankan      html  css  js  c++  java
  • django表单

    视图函数 views.py

    # -*-coding:utf-8-*-
    from django.http import HttpResponse, Http404
    from django.template.loader import get_template
    from django.template import Template,Context
    from django.shortcuts import render_to_response
    from books.models import Book
    
    
    def request_meta(request):
        item_list = request.META.items()
        item_list.sort()
        return render_to_response('request_meta.html',{'item_list': item_list})
    
    
    def request_meta_2(request):
        item_list = request.META.items()
        item_list.sort()
    
        t = get_template('request_meta.html')
        cxt = Context({'item_list': item_list})
        return HttpResponse(t.render(cxt))
    
    
    def search_form(request):
        return render_to_response('search_form.html')
    
    
    def search(request):
        if 'q' in request.GET and request.GET['q']:   #检查是否为空
            q =  request.GET['q']
            books = Book.objects.filter(title__contains=q)
            return render_to_response('search_results.html',{'books': books, 'query': q})
        else:
            return HttpResponse('Please submit a search term.')

    """
    def search(request):
    error = False
    if 'q' in request.GET:
    q = request.GET['q']
    if not q:
    error = True
    else:
    books = Book.objects.filter(title__icontains=q) #不区分大小写对title进行过滤
    return render_to_response('search_results.html',{'books':books, 'query':q})
    return render_to_response('search_form.html', {'error': error})
    """

    模板文件:request_meta.html,search_form.html,search_result.html

    <html>
    <head>
        <title>this is a django app</title>
    </head>
    <body>
        <table>
            {% for key,value in item_list %}
                {% if forloop.first %}
                        <tr><td>I am first row</td><td>{{ key }}</td><td>{{ value }}</td></tr>
                {% endif %}
    
                {% comment %}
                    {% if not forloop.first %}
                        {% if not forloop.last %}
                            <tr><td>{{ forloop.counter }}</td><td>{{ key }}</td><td>{{ value }}</td></tr>
                        {% endif %}
                    {% endif %}
                {% endcomment %}
    
                {% if not forloop.first or not forloop.last %}
                    <tr><td>{{ forloop.counter }}</td><td>{{ key }}</td><td>{{ value }}</td></tr>
                {% endif %}
    
                {% if forloop.last %}
                        <tr><td>I am last row</td><td>{{ key }}</td><td>{{ value }}</td></tr>
                {% endif %}
            {% endfor %}
        </table>
    </body>
    </html>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Search</title>
    </head>
    <body>
        <form action="/search/" method="get">
            <input type="text" name="q">
            <input type="submit" value="Search">
        </form>
    </body>
    </html>
    <!--此处action可以为空,eg. action="" 意味着表单将提交给与当前页面相同的URL-->
    <!DOCTYPE html>
    <html>
    <head>
        <title>Search</title>
    </head>
    <body>
      {# pluralize在合适的时候显示s #}
       Query String is: {{ query }}
           {% if books %}
                <p>Found {{ books|length }} book{{ book|pluralize }}</p>
               {% for book in books %}
                   <li>{{ book.title}}</li>
               {% endfor %}
           {% else %}
                <p>No books matched your search criteria.</p>
           {% endif %}
    
    </body>
    </html>

    备注:启用django模板系统许要在settings.py中进行模板配置

    TEMPLATE_DIRS = (
        # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
        # Always use forward slashes, even on Windows.
        # Don't forget to use absolute paths, not relative paths.
        os.path.join(os.path.dirname(__file__), 'templates').replace('\', '/'),
    )
  • 相关阅读:
    内存 : CL设置
    联通积分兑换的Q币怎么兑换到QQ上
    DB2数据库表追加字段
    显示菜单项与按钮项的关联关系
    如何将Windows8系统的磁盘格式(GPT格式)转换成Windows 7系统的磁盘格式(MBR格式)
    索尼(SONY) SVE1512S7C 把WIN8降成WIN7图文教程
    SqlServer之数据库三大范式
    Python并发编程-Redis
    Python并发编程-Memcached (分布式内存对象缓存系统)
    Python并发编程-RabbitMQ消息队列
  • 原文地址:https://www.cnblogs.com/506740640zl/p/5930253.html
Copyright © 2011-2022 走看看