标签:closed pwd 显示 success 字符串 site pen coding err
二.
views
三种形式 ,
1.HttpResponse("字符串")
2.ebder("页面")
--读取文件字符串 (在django中本身会自动读取文件)
--渲染变量
1 from django.shortcuts import render 2 3 # Create your views here. 4 def index(request): 5 sp=["好吃的","好玩的","好看的"] 6 name="ales" 7 return render(request,"index.html",{"name":name})
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 <p>{{ name }}</p> 9 10 <p>{{ sp }}</p> 11 </body> 12 </html>
3.redirect() #重定向
比如在登陆成功的时候,会显示success,这样也太客气了,所以重定向即使直接跳转到登陆后的页面
django可以通过redirect来实现 ,
1 from django.shortcuts import render,HttpResponse,redirect 2 3 # Create your views here. 4 def index(request): 5 6 return render(request,"index.html") 7 8 def login(request): 9 print("method", request.method) 10 if request.method=="GET": 11 # with open("templates/login.html","r",encoding="utf8") as f: 12 # data=f.read() 13 # 14 # return HttpResponse(data) 15 16 return render(request, "login.html") 17 18 else: 19 20 print("method",request.method) 21 print(request.POST) # <QueryDict: {‘user‘: [‘root‘], ‘pwd‘: [‘root1234‘]}> 22 user=request.POST.get("user") 23 pwd=request.POST.get("pwd") 24 print(user,pwd) 25 26 if user=="alex" and pwd=="123": 27 return redirect("/index/") 28 return HttpResponse("error")
1 from django.contrib import admin 2 from django.urls import path 3 from app01 import views 4 5 urlpatterns = [ 6 path(‘admin/‘, admin.site.urls), 7 path(‘index/‘, views.index), 8 path(‘login/‘,views.login) 9 ]
三.
templates 模板
模板语法由render渲染
关于
<div class="col-md-9">
{% block content %}
<h3>welcome!</h3>
{% endblock content%}
</div>
----------------------------
{% extends "base.html" %}
{% block content %}
{{ block.super }}
<div class="jumbotron">
<h1>Hello, world!</h1>
<p>...</p>
<p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a></p>
</div>
{% endblock %}
标签:closed pwd 显示 success 字符串 site pen coding err
原文地址:https://www.cnblogs.com/zhangqing979797/p/9833874.html