• Django~Views


    In Django, web pages and other content are delivered by views.

    To get from a URL to a view, Django uses what are known as ‘URLconfs’. A URLconf maps URL patterns (described as regular expressions) to views.

    django.core.urlresolvers

    write views

    add urls

    image

    image

    404

    Each view is responsible for doing one of two things: returning an HttpResponse object containing the content for the requested page, or raising an exception such as Http404.

    PDF,.XML,ZIP ^^^^^^^幽灵

    Your view can read records from a database, or not. It can use a template system such as Django’s – or a third-party Python template system – or not. It can generate a PDF file, output XML, create a ZIP file on the fly, anything you want, using whatever Python libraries you want.

    Create Templates

    First, create a directory called templates in your polls directory. Django will look for templates in there

    名字空间

    路径:polls/templates/polls/index.html. .否则

    image

    没有加模板前                                             添加模板后

    image  image 

    templates中内容 

    <ul> for 循环</ul>

    {% if latest_question_list %}
        <ul>
        {% for question in latest_question_list %}
            <li><a href="/polls/{{question.id}}/">{{question.question_text}}</a></li>
        {% endfor %}
        </ul>
    {% else %}
        <p>No polls are available.</p>
    {% endif %}

    省事的render

    image

    return render(request, 'polls/index.html', context)

    The render() function takes the request object as its first argument, a template name as its second argument and a dictionary as its optional third argument. It returns an HttpResponse object of the given template rendered with the given context.

    404问题

    try except                                                     render

    image    image

    image

    加上名字空间

    ulsr.py中app_name=’polls’

    index 中polls:detail

    <li><a href="{% url 'polls:detail' question.id %}">{{question.question_text}}</a></li>
    三年有成,问苍茫~
  • 相关阅读:
    那些创业的艰辛整理
    一个成功的研发团队应具备的9大属性
    如何将 Linux 系统转移至 LVM 卷
    如何在 Linux 上永久挂载一个 Windows 共享
    怎样在 Chromebook 上安装 Linux 系统?
    1087 有多少不同的值 (20 分)C语言
    1052 卖个萌 (20 分)C语言
    1064 朋友数 (20 分)C语言
    1045 快速排序 (25 分)C语言
    1048 数字加密 (20 分)C语言
  • 原文地址:https://www.cnblogs.com/lynclynn/p/5207582.html
Copyright © 2020-2023  润新知