静态文件 首先固定写法
上面的 STATIC_URL='/static/' 意思是别名 下面的index.html 调用静态的jq.js 就取这个static
在提交表单时 记得 关掉这个
区分开 免得互相受影响
blog下的路由
比较乱 大概就是这样- -
views
from django.shortcuts import render,render_to_response,redirect
from django.shortcuts import HttpResponse
import time
# Create your views here.
def show_time(req):
#return HttpResponse("hello")
t=time.ctime()
d={'time':t}
name='yuan'
sex='man'
return render(req, "index.html", locals()) #变量很多用loclas传递
#return render(req,"index.html",d)
#return render_to_response('index.html',d)
def article_year(req,year,month):
return HttpResponse("简傻屌 出生年月 %s %s" %(year,month))
def article_year_month(req,year1,month):
return HttpResponse("简傻屌 出生年月 %s %s" %(year1,month))
def register(req):
print(req.path)
print(req.get_full_path())
#print(req.GET.get("user"))
if req.method=="POST":
print(req.POST.get("user"))
if req.POST.get("user")=='jd':
return redirect('http://www.baidu.com/s?wd=jsd')
elif req.POST.get("user")=='jian':
return redirect('/login/')
return HttpResponse("OK")
#return render(req,'register.html')
return render_to_response('register.html')
def login(req):
return render(req,'login.html')
register.html
<!DOCTYPE html>
<html lang="en">
<head>
{% load staticfiles %}
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="{% url 'reg' %}" method="post">
<p>姓名<input type="text" name="user"></p>
<p>年龄<input type="text" name="age"></p>
<p>爱好<input type="checkbox" name="hobby" value="1">篮球
<input type="checkbox" name="hobby" value="2">足球
<input type="checkbox" name="hobby" value="3">乒乓球
</p>
<p><input type="submit">提交</p>
</form>
</body>
</html>
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="jq"></script>
<title>Title</title>
<style>
* {
margin: 0;
padding: 0
}
</style>
</head>
<body>
<h1>hello {{ name }}</h1>
<form>
用户名<input type="text">
</form>
</body>
</html>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
{% load staticfiles %}
<title>Title</title>
<script src="/static/jq.js"></script>
<script src="{%static 'jq.js' %}"></script>
</head>
<body>
<h1>我是index.html {{time}}-{{ name }}-{{ sex }}-{{ request.method }}</h1>
<script>
$('h1').css("color","red")
alert($('h1').text())
</script>
</body>
</html>