Django-编写Views.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django-编写Views.py相关的知识,希望对你有一定的参考价值。

polls/views.py 

views中定义三个方法

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)

polls/urls.py

urls触发views定义三个方法

 

from django.conf.urls import url
from . import views

urlpatterns = [
# ex: /polls/
url(r^$, views.index, name=index),
# ex: /polls/5/
url(r^(?P<question_id>[0-9]+)/$, views.detail, name=detail),
# ex: /polls/5/results/
url(r^(?P<question_id>[0-9]+)/results/$, views.results, name=results),
# ex: /polls/5/vote/
url(r^(?P<question_id>[0-9]+)/vote/$, views.vote, name=vote),
]

 



以上是关于Django-编写Views.py的主要内容,如果未能解决你的问题,请参考以下文章

Django 的views.py 中应该在哪里导入? [复制]

Django之视图Views

如何在 django views.py 中随机播放列表?

6Django实战第6天:用户登录

[Django笔记] views.py 深入学习

Django之views.py视图函数学习