<添加debug-toolbar>

django现在1.11是必须这么做:

pip install django-debug-toolbar

设置1:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
'debug_toolbar',
]

设置2:

INTERNAL_IPS = ('127.0.0.1',)

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
]

主要的urls.py里添加:

import debug_toolbar
url(r'^__debug__/', include(debug_toolbar.urls)),

如果还没显示出来,我通过f12看了下,一个jquery-min.js没加载,是要翻墙吗!!!!!!!!!果然翻墙好了。。。。CNM的天朝,CNM的天朝CNM的天朝CNM的天朝CNM的天朝CNM的天朝CNM的天朝

实现:

<body style="background-color: #222222">

<h1 style="text-align: center;background-color: #555555;
color:white;font-family: Source Code Pro;border-radius: 10px"> Questions </h1> {% if latest_question_list %}
<ul type="none">
{% for question in latest_question_list %}
<li style="color: white;;font-family: Source Code Pro">
<a href="/polls/{{ question.id }}/">{{ question.question_text }}</a>
</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %} </body>

index.html

对应的polls/views.py 里的index函数:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals from django.shortcuts import render
from django.http import HttpResponse,Http404
from .models import Question
from django.template import loader
from django.shortcuts import render,get_object_or_404
"""
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)
""" """
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
print latest_question_list
template = loader.get_template('polls/index.html')
context = {
'latest_question_list': latest_question_list,
}
return HttpResponse(template.render(context, request))
""" def index(request):
latest_question_list = Question.objects.all()
context = {'latest_question_list':latest_question_list}
return render(request,'polls/index.html',context)

修改index.html之: 主要用div的layout(参考http://www.w3school.com.cn/html/html_layout.asp)

<head>
<style>
#header {
background-color: black;
color: white;
text-align: center;
padding: 5px;
font-family: Consolas;
} #nav {
line-height: 30px;
background-color: #eeeeee;
height: 300px;
width: 100px;
float: left;
padding: 5px;
font-family: Consolas;
} #section {
width:auto;
float: left;
padding: 10px;
color:white;
text-align: center;
font-family: Consolas;
} #footer {
background-color: black;
color: white;
clear: both;
text-align: center;
padding: 5px;
font-family: Consolas;
}
</style>
</head> <body style="background-color: #222222"> <!-- Header-->
<div id="header">
<h1> Questions </h1>
</div> <!-- nav -->
<div id="nav">
{% if latest_question_list %}
{% for question in latest_question_list %}
<a href="/polls/{{ question.id }}/"> {{ question.question_text }}</a><br>
{% endfor %}
{% else %}
<p>No polls are available.</p>
{% endif %} </div> <!-- section -->
<div id="section">
<p>please select a production get more details</p>
</div> <!-- footer -->
<div id="footer">
<p> copyright gearslogy </p>
</div> </body>

动态的获取跳转页面:

这样做的好处是我们现在是polls/urls.py定义的是这个路径:

url(r'^([0-9]+)/$',views.detail,name='detail'),

index.html里是:

<a href="/polls/{{ question.id }}/">{{ question.question_text }}</a>

其实这个是非常不好的方法,这个会导致你写死/polls/1

但是可以看到在urls.py里有name = 'detail',可以用下面的做法:

<a href="{% url 'detail' question.id %}"> {{question.question_text}}</a>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Questions</title>
</head>
<body> {% for question in latest_question_list %}
<ul>
<a href="{% url 'detail' question.id %}"> {{question.question_text}}</a>
</ul> {% endfor %} </body>
</html>

 

从下面开始将用python3开始项目。受不了文档中他妈的各种部适合

<1>数据库再次尝试:

查看models的api如何工作的:

from django.db import models
import datetime
from django.utils import timezone # Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length= 200,help_text='Question text')
pub_date = models.DateTimeField('data published')
def __str__(self):
return self.question_text def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1) class Choice(models.Model):
question = models.ForeignKey(Question,on_delete=models.CASCADE)
choice_text = models.CharField(max_length =200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text class RelativeStuff(models.Model):
question = models.ForeignKey(Question,on_delete=models.CASCADE)
stuff_name = models.CharField(max_length = 200)

models.py

Choice和RelativeStuff都关联了Question

则Question类会有个动态对象是choice_set , relativestuff_set

如果

q = Question.objects.get(id=1)  # 获取一个Question对象

q.choice_set.create(choice_text = 'Not much', votes = 10)  #直接会插入数据库中

如下执行3次:

数据库直接就插入数据:

<2>1天撸一个表格程序:

实现细节: 静态文件:

要在你的html引用static文件:

{% load staticfiles %}

然后: 

<head>
<meta charset="utf-8">
<title> Gearslogy</title>
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
<script src="{% static 'js/jquery.min.js' %}"></script>
<script src="{% static 'js/bootstrap.min.js' %}"></script> </head>

base.html:

{% load staticfiles %}

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Gearslogy</title>
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
<script src="{% static 'js/jquery.min.js' %}"></script>
<script src="{% static 'js/bootstrap.min.js' %}"></script> </head>
<body> <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation" >
<div class="container-fluid" >
<div class="navbar-header">
<a class="navbar-brand" href="{% url 'polls:index' %}">Home</a>
</div>
<div>
<ul class="nav navbar-nav" >
<li class="active"><a href="">Tables</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Products <b class="caret"></b>
</a>
<ul class="dropdown-menu">
{% for question in question_list %}
<li><a href="{% url 'polls:detail' question.id %}"> {{ question.question_text }} </a></li>
{% endfor %}
</ul>
</li>
</ul>
</div>
</div>
</nav> <br>
<br>
<br>
<div> {% block primary %} {% endblock %}
</div>> </body>
</html>

主页index.html

{% extends "base.html" %}

{% block primary %}
<table class="table table-hover">
<caption>悬停表格布局</caption>
<thead>
<tr>
{% for h in header %}
<td> {{ h }} </td>
{% endfor %}
</tr>
</thead> <tbody>
<tr>
<td>Tanmay</td>
<td>Bangalore</td>
<td>560001</td>
</tr>
<tr>
<td>Sachin</td>
<td>Mumbai</td>
<td>400003</td>
</tr>
<tr>
<td>Uma</td>
<td>Pune</td>
<td>411027</td>
</tr>
{% for sd in data %}
<tr>
{% for d in sd %}
<td>{{ d }}</td>
{% endfor %}
</tr>
{% endfor %} </tbody>
</table>
{% endblock %}

另外一个细节:如果你要漏油一个图片:

views.py

def viewImage(request):
return HttpResponse('<img src="/static/image/test.jpg"> HelloWorld </img>')

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'),
path('view/',views.viewImage,name='viewImage'),
]

Web从入门到放弃<2>的更多相关文章

  1. Web从入门到放弃<8>

    Ref: Cameron D. - HTML5, JavaScript and jQuery (Programmer to Programmer) - 2015 http://www.runoob.c ...

  2. Web从入门到放弃<7>

    从这章开始读<javascript高级程序设计> <1>typeof 返回字符串 / 类型 未定义:undefined 布尔:boolean 字符串:string 数值:num ...

  3. Web从入门到放弃<5>

    <1> CSS_DOM 1,structural layer 2,presentation layer 3,behavior layer style也是一个属性 <!DOCTYPE ...

  4. Web从入门到放弃<1>

    HTML大法: <01> <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  5. Web从入门到放弃<6>

     <1> Canvas. 1,灰度图: js: function showAsGray() { var imgNode = document.getElementById('img'); ...

  6. Web从入门到放弃<4>

    1,插入 如下html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  7. Web从入门到放弃<3>

    UI简单的美化全部来源于Bootstrap 知识来自<javascript dom编程艺术第二版> <1> 点击列表 页面不跳转图片刷新:  主要点: href如何点击完如何不 ...

  8. 后端API入门到放弃指北

    后端API入门学习指北 了解一下一下概念. RESTful API标准] 所有的API都遵循[RESTful API标准]. 建议大家都简单了解一下HTTP协议和RESTful API相关资料. 阮一 ...

  9. OpenStack从入门到放弃

    OpenStack从入门到放弃 目录: 为何选择云计算/云计算之前遇到的问题 什么是云计算 云服务模式 云应用形式 传统应用与云感知应用 openstack及其相关组件介绍 flat/vlan/gre ...

随机推荐

  1. 多数据库有序GUID

    背景 常见的一种数据库设计是使用连续的整数为做主键,当新的数据插入到数据库时,由数据库自动生成.但这种设计不一定适合所有场景. 随着越来越多的使用Nhibernate.EntityFramework等 ...

  2. Cesium如何通过addImageryProvider方法加载SkylineGlobe Server发布的WMS服务

    某某某单位用SkylineGlobeServer7版本发布了好些服务,然后让我们在Cesium里都加载进来展示. 其实只要符合OGC标准的,加进来还是很容易的. 示例代码如下: function te ...

  3. 记一次Maven编译IKAnalyzer失败及解决办法

    下载了一个开源项目,maven形式组织的,其中有一个依赖包是IKAnalyzer. 由于mvnrepository中不存在IKAnalyzer的坐标,因此该依赖包需要自己下载安装到本地maven仓库才 ...

  4. 基于 HTML5 WebGL 的 3D 工控裙房系统

    前言 工业物联网在中国的发展如火如荼,网络基础设施建设,以及工业升级的迫切需要都为工业物联网发展提供了很大的机遇.中国工业物联网企业目前呈现两种发展形式并存状况:一方面是大型通讯.IT企业的布局:一方 ...

  5. 开放数据接口 API 简介与使用场景、调用方法

    此文章对开放数据接口 API 进行了功能介绍.使用场景介绍以及调用方法的说明,供用户在使用数据接口时参考之用. 在给大家分享的一系列软件开发视频课程中,以及在我们的社区微信群聊天中,都积极地鼓励大家开 ...

  6. 酷炫的loading

    今天分享一下,怎么通过用css写出一个酷炫的loading. meta: <meta name="viewport" content="width=device-w ...

  7. 数据标记系列——图像分割 & PolygonRNN++(二)

    实践 1.export PATH=~/anaconda3/bin:$PATH 2.Anaconda3 中创建新环境 Conda create –name=labelme_polyrnn_pp pyth ...

  8. ;。【】DAY14、递归,匿名函数

    一.三元运算符 三元运算符也称三目运算符,就是if .....else.....语法糖 前提:if 和 else 只有一条语句 例:a = 20 b = 30 res = a if a > b ...

  9. [模板] BSGS/扩展BSGS

    简介 前置知识: 快速幂&&O(1)快速乘 [模板] 数学基础:快速幂/乘/逆元/exGCD/(ex)CRT/(ex)Lucas定理

  10. Flask 构建微电影视频网站(四)

    后台管理 实现后台管理系统使用flask sqlalchemy结合mysql数据库进行增删改查操作.分页的使用.路由装饰器定义.模板中变量调用.登录会话机制.上传文件.flask wtforms表单使 ...