django学习入门三:讨论view的使用

        视图Django应用程序中Web 页面的“类型”,一般应用中的每个view都有特定的功能,稍微复杂点需要对应特定的模板。

比如在官方提供的示例中,包含的页面有:

  1. 首页 --显示最新投票问题
  2. 投票问题详情页面
  3. 投票结果页面
  4. 投票页面
现在我的目录结构如下:

其中红色标记部分是修改的文件,蓝色标记部分是新添加的文件

其中polls.views.py修改如下:
from django.shortcuts import render,get_object_or_404

# Create your views here.
from django.http import HttpResponse
from .models import Question
from django.template import loader

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))

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/details.html',{'question':question})
   """
    #lowwer method can finish upper try catch function
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})
def results(request,question_id):
    return  HttpResponse("You're looking at the results of question %s." % question_id)
def vote(request,question_id):
    return  HttpResponse("You're voting on question %s." % question_id)
其中results、voite页面只是简单的显示,HttpResponse在这是返回对应的字符串到浏览器前端。
def detail(request,question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})
        detail 方法参数中有一个question_id,其他方法也是,这个参数是前端传过来的参数,参数类型为int,后面将讲到。
       get_object_or_404方法是django.shortcuts模块中的方法,有两个参数,参数一:object,参数二,匹配条件,如果没有找到结果,该方法会捕获异常,并报404错误,如果没有报异常,render方法是对视图的渲染,参数指定要渲染的页面以传递参数到视图。
        本次新添加了一个templates模板目录,templates下创建polls目录,polls目录下创建了两个html文件,暂时只有两个,下一节补全

在此分别给出代码:
details.html
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>
index.html
{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}
   
        polls.urls是最新添加的,其实可以将所有的url都在总的project的urls中配置,也就是在mysite.urls中配置,但是如果app很多的时候就会显得很乱,不利于管理,在这我们创建了一个app单独的urls,并通过include将poll.urls添加到mysite.urls中

       mysite.urls代码如下:
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('polls/',include('polls.urls')),
]
其中include就是将app中的urls添加到project中,可以看到路径是polls,也就是说目录最少是“127.0.0.1:8080/polls/”
       polls.urls代码如下:
from django.urls import path
from . import  views

app_name = 'polls'
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'),
]
url中polls在project中匹配  “path('polls/',include('polls.urls')),“,那么剩下部分将在polls.url中匹配
说明:
  • <int:question_id> 表示匹配的url部分是数字,并将数字绑定到question_id变量上,并传到对应的view中去
  • path中的name是给这个url命名,为其他文件调用使用
上述配置完对应的url分别为
  • 127.0.0.1:8000/polls   //会跳转到index视图,并显示最新的前五条投票话题
  • 127.0.0.1:8000/polls/3   //会跳转到details页面,并将id=3的question对象传到页面中
  • 其他页面待完善

index的请求效果图如下:


点击一个进去,就会显示这个话题的内容:
















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值