django2.0 自己入门记录一些基础url 模板等

本文介绍了Django框架中视图函数的定义与使用,包括时间显示、URL参数处理、表单提交及重定向操作。同时,深入探讨了静态文件的配置与引用方法,如静态资源路径设置及模板中的加载方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

静态文件 首先固定写法

 上面的 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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值