最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Django開發(fā)的簡易留言板案例詳解

 更新時間:2018年12月04日 09:35:44   作者:Yort2016  
這篇文章主要介紹了Django開發(fā)的簡易留言板,結(jié)合實例形式詳細(xì)分析了基于Python框架Django開發(fā)留言板的具體文件結(jié)構(gòu)、流程步驟與相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了Django開發(fā)的簡易留言板。分享給大家供大家參考,具體如下:

Django在線留言板小練習(xí)

環(huán)境

ubuntu16.04 + python3 + django1.11

1、創(chuàng)建項目

django-admin.py startproject message

進(jìn)入項目message

2、創(chuàng)建APP

python manager.py startapp guestbook

項目結(jié)構(gòu)

.
├── guestbook
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── manage.py
└── message
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-35.pyc
    │   └── settings.cpython-35.pyc
    ├── settings.py
    ├── urls.py
    └── wsgi.py

4 directories, 14 files

需要做的事:

配置項目setting 、初始化數(shù)據(jù)庫、配置url 、編寫views 、創(chuàng)建HTML文件

項目配置

打開message/settings.py

設(shè)置哪些主機(jī)可以訪問,*代表所有主機(jī)

ALLOWED_HOSTS = ["*"]
INSTALLED_APPS = [
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'guestbook',  #剛剛創(chuàng)建的APP,加入到此項目中
]
#數(shù)據(jù)庫默認(rèn)用sqlite3,后期可以換成MySQL或者SQL Server等
TIME_ZONE = 'PRC' #時區(qū)設(shè)置為中國

創(chuàng)建數(shù)據(jù)庫字段

#encoding: utf-8
from django.db import models
class Message(models.Model):
  username=models.CharField(max_length=256)
  title=models.CharField(max_length=512)
  content=models.TextField(max_length=256)
  publish=models.DateTimeField()
  #為了顯示
  def __str__(self):
    tpl = '<Message:[username={username}, title={title}, content={content}, publish={publish}]>'
    return tpl.format(username=self.username, title=self.title, content=self.content, publish=self.publish)

初始化數(shù)據(jù)庫

# 1. 創(chuàng)建更改的文件
root@python:/online/message# python3 manage.py makemigrations
Migrations for 'guestbook':
 guestbook/migrations/0001_initial.py
  - Create model Message
# 2. 將生成的py文件應(yīng)用到數(shù)據(jù)庫
root@python:/online/message# python3 manage.py migrate
Operations to perform:
 Apply all migrations: admin, auth, contenttypes, guestbook, sessions
Running migrations:
 Applying contenttypes.0001_initial... OK
 Applying auth.0001_initial... OK
 Applying admin.0001_initial... OK
 Applying admin.0002_logentry_remove_auto_add... OK
 Applying contenttypes.0002_remove_content_type_name... OK
 Applying auth.0002_alter_permission_name_max_length... OK
 Applying auth.0003_alter_user_email_max_length... OK
 Applying auth.0004_alter_user_username_opts... OK
 Applying auth.0005_alter_user_last_login_null... OK
 Applying auth.0006_require_contenttypes_0002... OK
 Applying auth.0007_alter_validators_add_error_messages... OK
 Applying auth.0008_alter_user_username_max_length... OK
 Applying guestbook.0001_initial... OK
 Applying sessions.0001_initial... OK

配置url

設(shè)置項目message/urls.py

from django.conf.urls import url,include #添加了include
from django.contrib import admin
urlpatterns = [
  url(r'^admin/', admin.site.urls),
  url(r'^guestbook/', include('guestbook.urls',namespace='guestbook')),  #表示在url地址中所有g(shù)uestbook的都交給guestbook下面的url來處理,后面的逗號不要省略
]

設(shè)置APP的url

如果是初次創(chuàng)建APP,urls.py在APP中一般不存在,創(chuàng)建即可

vim guestbook/urls.py

# 內(nèi)容如下
from django.conf.urls import url
from . import views
urlpatterns = [
  url(r'^index/',views.index,name='index'),  #不要忘了逗號
]

編寫views

編輯APP中的views.py

from django.shortcuts import render
from django.http import HttpResponseRedirect
from . import models
# Create your views here.
def index(request):
  messages = models.Message.objects.all()
  return render(request, 'guestbook/index.html', {'messages' : messages})

編寫HTML文件

創(chuàng)建APP/templates/guestbook/index.html目錄及文件

使用bootstrap美化了

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>留言板</title>
    <link rel="stylesheet"  rel="external nofollow" rel="external nofollow" crossorigin="anonymous">
  </head>
  <body>
    <table class="table table-striped table-bordered table-hover table-condensed">
      <thead>
        <tr class="danger">
          <th>留言時間</th>
          <th>留言者</th>
          <th>標(biāo)題</th>
          <th>內(nèi)容</th>
        </tr>
      </thead>
      <tbody>
        {% if messages %}
          {% for message in messages %}
            <tr class="{% cycle 'active' 'success' 'warning' 'info' %}">
              <td>{{ message.publish|date:'Y-m-d H:i:s' }}</td>
              <td>{{ message.username }}</td>
              <td>{{ message.title }}</td>
              <td>{{ message.content }}</td>
            </tr>
          {% endfor %}
        {% else %}
          <tr>
            <td colspan="4">無數(shù)據(jù)</td>
          </tr>
        {% endif %}
      </tbody>
    </table> 
    <a class="btn btn-xs btn-info" href="/guestbook/create/" rel="external nofollow" >去留言</a>
  </body>
</html>

調(diào)試index頁面

python manage.py runserver 0.0.0.0:99

打開瀏覽器訪問http://開發(fā)機(jī)器ip地址:99/guestbook/index/

留言展示頁面成功

創(chuàng)建留言頁面

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>留言</title>
    <link rel="stylesheet"  rel="external nofollow" rel="external nofollow" crossorigin="anonymous">
  </head>
  <body>
    <!-- 我是注釋 -->
    <h3>留言</h3> <!--h1-> h6-->
    <!--method: POST /GET -->
    <form action="/guestbook/save/" method="POST" novalidate="novalidate">
    {% csrf_token %}
      <table class="table table-striped table-bordered table-hover table-condensed">
        <label>用戶名:</label> <input type="text" name="username" placeholder="用戶名" /> <br /><br />
        <label>標(biāo) 題:</label> <input type="text" name="title" placeholder="標(biāo)題" /><br /><br />
        <label>內(nèi) 容:</label> <textarea name="content" placeholder="內(nèi)容"> </textarea><br /><br />
      </table>
      <input class="btn btn-success" type="submit" value="留言"/>
    </form>
  </body>
</html>

配置APP下的url

vim guestbook/urls.py

urlpatterns = [
  url(r'^index/',views.index,name='index'),  #不要忘了逗號
  url(r'^create/$', views.create, name='create'),
  url(r'^save/$', views.save, name='save'), 
]

編輯views.py

#先導(dǎo)入時間模塊
import datetime
#添加create、save
def create(request):
  return render(request, 'guestbook/create.html')
def save(request):
  username = request.POST.get("username")
  title = request.POST.get("title")
  content = request.POST.get("content")
  publish = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  message = models.Message(title=title, content=content, username=username, publish=publish)
  message.save()
  return HttpResponseRedirect('/guestbook/index/')

OK,再次運行,enjoy it!

希望本文所述對大家基于Django框架的Python程序設(shè)計有所幫助。

相關(guān)文章

  • 解決keras加入lambda層時shape的問題

    解決keras加入lambda層時shape的問題

    這篇文章主要介紹了解決keras加入lambda層時shape的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • python實現(xiàn)二維碼掃碼自動登錄淘寶

    python實現(xiàn)二維碼掃碼自動登錄淘寶

    最近做項目,需要用到自動登錄淘寶,正好在學(xué)習(xí)python,整網(wǎng)絡(luò)爬蟲,所以就嘗試著寫一個腳本,自動解決。有相同需求的小伙伴可以參考下
    2016-12-12
  • windows下wxPython開發(fā)環(huán)境安裝與配置方法

    windows下wxPython開發(fā)環(huán)境安裝與配置方法

    這篇文章主要介紹了windows下wxPython開發(fā)環(huán)境安裝與配置方法,需要的朋友可以參考下
    2014-06-06
  • Python流行ORM框架sqlalchemy的簡單使用

    Python流行ORM框架sqlalchemy的簡單使用

    這篇文章主要介紹了Python流行ORM框架sqlalchemy的簡單使用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-07-07
  • 使用Python獲取字典鍵對應(yīng)值的兩種方法

    使用Python獲取字典鍵對應(yīng)值的兩種方法

    對于字典通過鍵獲得值非常簡單,但通過值獲得鍵則需繞些彎子,下面這篇文章主要給大家介紹了關(guān)于如何使用Python獲取字典鍵對應(yīng)值的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • pytorch張量和numpy數(shù)組相互轉(zhuǎn)換

    pytorch張量和numpy數(shù)組相互轉(zhuǎn)換

    在使用pytorch作為深度學(xué)習(xí)的框架時,經(jīng)常會遇到張量tensor和矩陣numpy的類型的相互轉(zhuǎn)化的問題,本文主要介紹了pytorch張量和numpy數(shù)組相互轉(zhuǎn)換,感興趣的可以了解一下
    2024-02-02
  • Python對象與引用的介紹

    Python對象與引用的介紹

    今天小編就為大家分享一篇關(guān)于Python對象與引用的介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • python實現(xiàn)的B站直播錄制工具

    python實現(xiàn)的B站直播錄制工具

    這篇文章主要介紹了python實現(xiàn)的B站直播錄播工具,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-04-04
  • 拿來就用!Python批量合并PDF的示例代碼

    拿來就用!Python批量合并PDF的示例代碼

    這篇文章主要介紹了Python批量合并PDF的示例代碼,幫助大家更好的理解和學(xué)習(xí)Python,感興趣的朋友可以了解下
    2020-08-08
  • 基于Python實現(xiàn)虛假評論檢測可視化系統(tǒng)

    基于Python實現(xiàn)虛假評論檢測可視化系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了如何基于Python實現(xiàn)一個簡單的虛假評論檢測可視化系統(tǒng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-04-04

最新評論

托克托县| 尼勒克县| 晋江市| 繁昌县| 崇信县| 长岛县| 吴江市| 道孚县| 固安县| 泌阳县| 阿克| 吐鲁番市| 从化市| 长海县| 青川县| 南靖县| 孟津县| 高要市| 延寿县| 巢湖市| 抚顺县| 射洪县| 财经| 开封市| 兴安盟| 盘山县| 玛多县| 石楼县| 嘉兴市| 酒泉市| 海阳市| 贵溪市| 青铜峡市| 襄樊市| 河东区| 于田县| 衡阳县| 长葛市| 喀什市| 上杭县| 慈溪市|