zoukankan      html  css  js  c++  java
  • Django3 编写views/使用模版/设置404/URL命名空间=3

    一、完善项目

    1.1 编写更多的Views

    编写polls/views.py

    def detail(request, question_id):
        return HttpResponse("You're looking at question %s." % question_id)
    
    def results(request, question_id):
        response = "You're looking at the results of question %s."
        return HttpResponse(response % question_id)
    
    def vote(request, question_id):
        return HttpResponse("You're voting on question %s." % question_id)
    

    将上述views加入到路由:

    from django.urls import path
    
    from . import views
    
    urlpatterns = [
        # ex: /polls/
        path('', views.index, name='index'),
        # ex: /polls/5/
        path('<int:question_id>/', views.detail, name='detail'),
        # ex: /polls/5/results/
        path('<int:question_id>/results/', views.results, name='results'),
        # ex: /polls/5/vote/
        path('<int:question_id>/vote/', views.vote, name='vote'),
    ]
    

    再改变一下views让主页返回最后5个poll:

    from django.http import HttpResponse
    
    from .models import Question
    
    
    def index(request):
        latest_question_list = Question.objects.order_by('-pub_date')[:5]
        output = ', '.join([q.question_text for q in latest_question_list])
        return HttpResponse(output)
    
    # Leave the rest of the views (detail, results, vote) unchanged
    


    由于这里只添加了3个问题,所以只显示了3个。

    1.2 编写/使用模版

    1、首先在polls目录添加templates文件夹。templates描述了Django如何加载和渲染模版。
    默认设置APP_DIRSTrue。django会扫描INSTALLED_APPS下所有templates目录。
    2、在templates中创建一个polls,在polls里面创建一个index.html,写入以下内容:

    {% 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 %}
    

    3、然后更新polls/views.py

    from django.http import HttpResponse
    from django.template import loader
    
    from .models import Question
    
    
    def index(request):
        latest_question_list = Question.objects.order_by('-pub_date')[:5]
        template = loader.get_template('polls/index.html')
        context = {
            'latest_question_list': latest_question_list,
        }
        return HttpResponse(template.render(context, request))
    

    访问polls如下图:

    4、再次更新polls/views.py

    from django.shortcuts import render
    
    from .models import Question
    
    
    def index(request):
        latest_question_list = Question.objects.order_by('-pub_date')[:5]
        context = {'latest_question_list': latest_question_list}
        return render(request, 'polls/index.html', context)
    

    1.3 返回404

    更新polls/views.py

    from django.http import Http404
    from django.shortcuts import render
    
    from .models import Question
    # ...
    def detail(request, question_id):
        try:
            question = Question.objects.get(pk=question_id)
        except Question.DoesNotExist:
            raise Http404("Question does not exist")
        return render(request, 'polls/detail.html', {'question': question})
    

    更新polls/templates/polls/detail.html

    {{ question }}
    

    访问具体的问题即可看到内容:

    如果访问不存在的id将返回404:

    我们可以看到,如果需要从views往templates传值,需要指定字典形式的值传送过去。
    当我们需要传送的值非常多的时候,会非常麻烦。
    我们可以使用`locals()函数来简化我们的传值工作。

    # 使用locals()函数可以将我们本地所有的变量,以字典形式传送过去
    return render(request, 'index.html', locals())
    

    返回404的便捷写法:
    更新polls/views.py

    from django.shortcuts import get_object_or_404, render
    
    from .models import Question
    # ...
    def detail(request, question_id):
        question = get_object_or_404(Question, pk=question_id)
        return render(request, 'polls/detail.html', {'question': question})
    

    1.4 继续使用模版系统

    更新polls/templates/polls/detail.html

    <h1>{{ question.question_text }}</h1>
    <ul>
    {% for choice in question.choice_set.all %}
        <li>{{ choice.choice_text }}</li>
    {% endfor %}
    </ul>
    

    模版系统使用点查询方式来获取元素的属性。
    接下来,我们改变polls/index.html模版中的地址引用:

    <!-- 上面一行是之前的,下面一行是相对地址引用 -->
    <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
    <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
    

    这里我们将绝对地址引用改为相对地址引用。
    这样的话,当我们在路由配置中改地址的时候,可以不用改模版。
    比如我们将polls/urls.py其中一条地址路由匹配改为:

    path('okokk/<int:question_id>/', views.detail, name='detail'),
    

    这样的话,我们访问问题具体页面的时候,地址就会变成:
    polls/okokk/.....

    1.5 URL命名空间

    当有多个项目时,views可能相同,URL可能会冲突,这个时候就需要命名空间来避免了。

    我们在相应应用的urls.py中添加相应应用名即可:

    from django.urls import path
    
    from . import views
    
    app_name = 'polls'
    urlpatterns = [
        path('', views.index, name='index'),
        path('<int:question_id>/', views.detail, name='detail'),
        path('<int:question_id>/results/', views.results, name='results'),
        path('<int:question_id>/vote/', views.vote, name='vote'),
    ]
    

    然后在模版中引用的时候加上应用名即可:

    <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    
  • 相关阅读:
    【RS】Automatic recommendation technology for learning resources with convolutional neural network
    卷积神经网络的入门
    Debug 路漫漫-10:AttributeError: 'Embedding' object has no attribute 'get_shape'
    Debug 路漫漫-09:构建CNN时维度不一致问题
    Debug 路漫漫-08:Keras 版本升级函数变换导致的问题
    springboot 过滤器、拦截器、消息转换器、切片执行顺序 及区别
    java InputStream读取数据问题
    Springboot 2-OAuth 2修改登录加密方式
    Spring Boot Security Oauth2之客户端模式及密码模式实现
    oauth2.0通过JdbcClientDetailsService从数据库读取相应的配置
  • 原文地址:https://www.cnblogs.com/amnotgcs/p/13784783.html
Copyright © 2011-2022 走看看