首页 > Web开发 > 详细

Django 传递额外参数及 URL别名

时间:2019-03-29 20:02:18      阅读:138      评论:0      收藏:0      [点我收藏+]

传递额外参数到视图函数中

在 urls.py 文件中添加下面内容

from django.conf.urls import url

urlpatterns = [
    url(r'index', views.index, {"name":'klvchen'}),
]

在 views.py 文件中添加下面内容

def index(req, name):

    return HttpResponse(name)

定义的 name 变量可直接在 views.py 中调用返回
技术分享图片

Django的URL别名

在 urls.py 上添加,html 中指定路径别名 klvchen 即可

urlpatterns = [
    # ......
    url(r"index", views.index, name="klvchen"),
]

在 templates 文件夹中添加一个 login.html 文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form ation={% url "klvchen" %} method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="submit">
</form>

</body>
</html>

在 views.py 添加方法

def index(req):
    if req.method=="POST":
        username = req.POST.get("username")
        pwd = req.POST.get("password")

        print(username)
        print(pwd)

        if username == "klvchen" and pwd=="123":
            return HttpResponse("登录成功")
    return render(req, "login.html")

Django 传递额外参数及 URL别名

原文:https://www.cnblogs.com/klvchen/p/10622567.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!