第03章节-Python3.5-今日Django工程的创建 2

用pychram创建Django程序

  1. file->New Project


    image.png
image.png
image.png

python manage.py startapp app01

image.png
  • 配置settings.py 文件


    image.png
image.png

最后一行添加以下:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
image.png
  • 修改urls.py
from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/', views.index),
]
image.png
  • 修改views.py
from django.shortcuts import render,HttpResponse

# Create your views here.


def index(request):
    return HttpResponse('Index')
image.png
image.png
  • 基本程序效果图:


    image.png

@进一步

image.png
  • 修改urls.py
from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/', views.index),
    url(r'^login/', views.login),
]
image.png
  • 修改views.py
from django.shortcuts import render,HttpResponse,redirect

# Create your views here.


def index(request):
    return HttpResponse('Index')


def login(request):
    # 判断用户获取数据方式是GET,就返回什么数据
    if request.method == "GET":
        return render(request, 'login.html')
    # 判断用户获取数据方式是POST,就判断用户提交的数据是否正确
    elif request.method == "POST":
        u = request.POST.get('user')
        p = request.POST.get('pwd')
        if u == 'alex' and p == '123':
            return redirect('/index/')
        else:
            return render(request, 'login.html')
    else:
        # PUT,DELETE,HEAD,OPTION...
        return redirect("/index/")

image.png
  • 修改login.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <form action="/login/" method="post">
        <p>
            <input type="text" name="user" placeholder="用户名">
        </p>
        <p>
            <input type="password" name="pwd" placeholder="密码">
        </p>
        <input type="submit" value="提交">

    </form>

</body>
</html>
image.png
  • 效果图:


    image.png
image.png
上一篇:第06章节-Python3.5-Django模板语言循环字典 5


下一篇:第16 17章节-Python3.5-Django知识点整理 15