python+djiago博客開發1

 1.新建項目,專業版纔有django。社區版需要通過命令。

 2.生成項目後,相關文件用途

 

 3.views.py修改此文件,編寫業務邏輯

from django.shortcuts import HttpResponse #導入HttpResponse模塊
 
def index(request):#request是必須帶的實例。類似class下方法必須帶self一樣
    return HttpResponse("Hello World!!")#通過HttpResponse模塊直接返回字符串到前端頁面

4.配置url路由

from laomomo import views#導入views模塊
from django.conf.urls import url
 
urlpatterns=[
    url(r'^index/',views.index)#配置當訪問index/時去調用views下的index方法
]

5.

terminal下執行 python manage.py runserver   這樣執行默認的路徑是127.0.0.1:8000

指定端口或地址就再後面寫上,如:python manage.py runserver 127.0.0.1:8888

然後瀏覽器訪問http://127.0.0.1:8000/index

頁面顯示Hello World!!

------------------------------------------------------------------------------------------------

6.

新建成功工程後templates文件目錄下是空的,我們需要在該目錄下新建一個html文件來把內容展示到前端

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>test</title>
    </head>
    <body>
    <table border="1">
        <thead>
        <tr>
            <td>用戶名</td>
            <td>密碼</td>
        </tr>
        </thead>
        {%for line in form%}
            <tr>
                <td>{{line.name}}</td>
                <td>{{line.password}}</td>
            </tr>
        {% endfor %}
    </table>
    </body>

</html>

 

7.修改views.py裏的index方法如下

from django.shortcuts import render

# Create your views here.
list = [{"name": 'good', "password": 'python'}, {"name": 'learning', "password": 'django'}]

def index(request):
    return render(request, "index.html", {'form': list})

 

8.運行結果如下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章