Django之无名参数和基本POST请求

来源:互联网 发布:淘宝卖家怎么换支付宝 编辑:程序博客网 时间:2024/06/07 11:32


Djon的基本示例    POST提交简单示例    和 无名参数正则传递 


urls文件

"""Dajon_demo URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see:    https://docs.djangoproject.com/en/1.11/topics/http/urls/Examples:Function views    1. Add an import:  from my_app import views    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')Class-based views    1. Add an import:  from other_app.views import Home    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')Including another URLconf    1. Import the include() function: from django.conf.urls import url, include    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))"""from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpatterns = [    url(r'^admin/', admin.site.urls),    url(r'^index/', views.index),    url(r'^login_in/', views.login_in),# 无名分组 传参 ()中为参数   类似re.findall    url(r'^articles/(\d{4})$',views.year),    url(r'^articles/(\d{4})/(\d{2})', views.month),]



viwews文件

from django.shortcuts import render, HttpResponse# Create your views here.import datetimedef index(request):    timer = datetime.datetime.now()    return HttpResponse(timer)def login_in(request):    if request.method == "POST":        # print("=====>", request.POST)        username = request.POST.get("user")        password = request.POST.get("pwd")        if username == "yuan" and password == "123":            return HttpResponse("登陆成功")        return render(request, "lgoin_in.html")def year(request, year):    return HttpResponse(year)def month(request, year, month):    return HttpResponse("year: %s month:%s" % (year, month))



template的html文件

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body><form action="/login_in/" method="post">    <p>姓名: <input type="text" name="user"></p>    <p>密码: <input type="password" name="pwd"></p>    <p><input type="submit"></p></form></body></html>