当前位置: 首页 > news >正文

django第一个app,3

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

增加页面和功能

  • Question “index” page – displays the latest few questions.
  • Question “detail” page – displays a question text, with no results but with a form to vote.
  • Question “results” page – displays results for a particular question.
  • Vote action – handles voting for a particular choice in a particular question.

1,编写views

from django.shortcuts import render
from django.shortcuts import get_object_or_404, render
from .models import Question
from django.http import HttpResponse
# Create your views here.

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)
def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})
def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)
def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)
 

 

2,编写url

from django.urls import path
from . import views

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'),
]
创建文件在  mysite/polls/templates/polls/下创建index.html,detail.html

{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
    <li><a href="{% url 'detail' question.id %}">{{question.question_text}}</a></li>
{% endfor %}
</ul>

{% else %}
    <p>no polls </p>
{% endif %}

detail.html

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

 

 

 

 

转载于:https://my.oschina.net/u/2367514/blog/1609777

相关文章:

  • 坦克大战1.0版
  • 【Html5】-- 塔台管制
  • 国内域名国内服务器,不备案解决80端口不开放方法
  • python
  • Java与Unix时间戳的相互转换
  • Intellij Idea编译项目下的.java文件时的编码问题
  • Windows上SSH服务器的配置以及客户端的连接
  • UIScrollView中使用AutoLayout
  • mysql LINESTRING ,POINT 类型操作
  • 读Git Pro2的笔记
  • Vue.js简介及指令
  • 全面分析 Spring 的编程式事务管理及声明式事务管理
  • jeesite优化
  • 统计数据库大小的方法
  • 带你走进单片机的世界——初始MCU
  • ES6指北【2】—— 箭头函数
  • Google 是如何开发 Web 框架的
  • [case10]使用RSQL实现端到端的动态查询
  • Android 初级面试者拾遗(前台界面篇)之 Activity 和 Fragment
  • Mybatis初体验
  • Python语法速览与机器学习开发环境搭建
  • SpringBoot几种定时任务的实现方式
  • use Google search engine
  • Yeoman_Bower_Grunt
  • 表单中readonly的input等标签,禁止光标进入(focus)的几种方式
  • 基于组件的设计工作流与界面抽象
  • 七牛云 DV OV EV SSL 证书上线,限时折扣低至 6.75 折!
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 赢得Docker挑战最佳实践
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • #Linux杂记--将Python3的源码编译为.so文件方法与Linux环境下的交叉编译方法
  • #stm32驱动外设模块总结w5500模块
  • (2)nginx 安装、启停
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (学习日记)2024.01.19
  • (转)重识new
  • (最简单,详细,直接上手)uniapp/vue中英文多语言切换
  • ./configure,make,make install的作用(转)
  • .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
  • .NET Compact Framework 3.5 支持 WCF 的子集
  • .net core 6 redis操作类
  • .NET MVC之AOP
  • .net操作Excel出错解决
  • .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
  • .net开源工作流引擎ccflow表单数据返回值Pop分组模式和表格模式对比
  • :=
  • ?.的用法
  • @Autowired 与@Resource的区别
  • [ vulhub漏洞复现篇 ] Django SQL注入漏洞复现 CVE-2021-35042
  • [ vulhub漏洞复现篇 ] GhostScript 沙箱绕过(任意命令执行)漏洞CVE-2019-6116
  • [.NET 即时通信SignalR] 认识SignalR (一)
  • [14]内置对象
  • [ajaxupload] - 上传文件同时附件参数值