Djiango项目从头来day02

  • 路由如何避免重名冲突
from django.contrib import admin
from django.urls import path
from demo.views import show
from demo2.views import haha
urlpatterns = [
    path('admin/', admin.site.urls),
    path('demo/', show),
    path('demo2/', haha),
]

编写视图函数

from django.shortcuts import render

# Create your views here.
def show(request):
    msg = "呵呵"
    return render(request, "index.html", {"msg":msg})

编辑模板




    
    Title


欢迎来到王者峡谷

{{ msg }}

  • 配置静态文件访问路径
    在settings中添加
import os
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

在项目中新建文件夹static, 在static下新建css文件夹,继续新建style.css


.smart-green {
    margin-left: auto;
    margin-right: auto;
    max-width: 500px;
    background: #F8F8F8;
    padding: 30px 30px 20px 30px;
    font: 12px Arial, Helvetica, sans-serif;
    color: #666;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
}

.smart-green h1 {
    font: 24px "Trebuchet MS", Arial, Helvetica, sans-serif;
    padding: 20px 0px 20px 40px;
    display: block;
    margin: -30px -30px 10px -30px;
    color: #FFF;
    background: #9DC45F;
    text-shadow: 1px 1px 1px #949494;
    border-radius: 5px 5px 0px 0px;
    -webkit-border-radius: 5px 5px 0px 0px;
    -moz-border-radius: 5px 5px 0px 0px;
    border-bottom: 1px solid #89AF4C;
}

.smart-green h1 > span {
    display: block;
    font-size: 11px;
    color: #FFF;
}

.smart-green label {
    display: block;
    margin: 0px 0px 5px;
}

.smart-green label > span {
    float: left;
    margin-top: 10px;
    color: #5E5E5E;
}

.smart-green input[type="text"], .smart-green input[type="email"], .smart-green textarea, .smart-green select {
    color: #555;
    height: 30px;
    line-height: 15px;
    width: 100%;
    padding: 0px 0px 0px 10px;
    margin-top: 2px;
    border: 1px solid #E5E5E5;
    background: #FBFBFB;
    outline: 0;
    -webkit-box-shadow: inset 1px 1px 2px rgba(238, 238, 238, 0.2);
    box-shadow: inset 1px 1px 2px rgba(238, 238, 238, 0.2);
    font: normal 14px/14px Arial, Helvetica, sans-serif;
}

.smart-green textarea {
    height: 100px;
    padding-top: 10px;
}


.smart-green .button {
    background-color: #9DC45F;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-border-radius: 5px;
    border: none;
    padding: 10px 25px 10px 25px;
    color: #FFF;
    text-shadow: 1px 1px 1px #949494;
}

.smart-green .button:hover {
    background-color: #80A24A;
}

.error-msg {
    color: red;
    margin-top: 10px;
}

.success-msg {
    color: #80A24A;
    margin-top: 10px;
    margin-bottom: 10px;
}

在index.html中添加css引入


    
    
    


访问http://127.0.0.1:8000/demo/

Djiango项目从头来day02_第1张图片

from django.shortcuts import render
from demo.models import Demo
# Create your views here.
def show(request):
    message_all = Demo.objects.all()
    if message_all:
        message = message_all[0]
        print(message.id)
        print(message.name)
        print(message.email)
        print(message.message)
    return render(request, "index.html", {"message":message})

index.html




    
    
    



留言信息 请留下你的信息.

http://127.0.0.1:8000/demo/

Djiango项目从头来day02_第2张图片

保存留言信息

编写前端post请求


Djiango项目从头来day02_第3张图片

Djiango项目从头来day02_第4张图片

Djiango项目从头来day02_第5张图片

更新views.py

from django.shortcuts import render
from demo.models import Demo
# Create your views here.
def show(request):
    if request.method == 'GET':
        print("get 请求")
        message_all = Demo.objects.all()
        if message_all:
            message = message_all[0]
            print(message.id)
            print(message.name)
            print(message.email)
            print(message.message)
            return render(request, "index.html", {"message": message})
    if request.method == 'POST':
        print("post 请求")
        msg = "保存成功!"
        name = request.POST.get("name")
        email = request.POST.get("email")
        message = request.POST.get("message")
        address = request.POST.get("address")

        demo = Demo()

        demo.name = name
        demo.email = email
        demo.address = address
        demo.message =message
        demo.save()


        return render(request, "index.html",{"msg":msg})

Djiango项目从头来day02_第6张图片

你可能感兴趣的:(Djiango项目从头来day02)