欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

環(huán)境

ubuntu16.04 + python3 + django1.11

1、創(chuàng)建項(xiàng)目

django-admin.py startproject message

進(jìn)入項(xiàng)目message

2、創(chuàng)建APP

python manager.py startapp guestbook

項(xiàng)目結(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

需要做的事:

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

項(xiàng)目配置

打開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,加入到此項(xiàng)目中
]
#數(shù)據(jù)庫默認(rèn)用sqlite3,后期可以換成MySQL或者SQL Server等
TIME_ZONE = 'PRC' #時(shí)區(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è)置項(xiàng)目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來處理,后面的逗號(hào)不要省略
]

設(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'),  #不要忘了逗號(hào)
]

編寫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>留言時(shí)間</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'),  #不要忘了逗號(hào)
  url(r'^create/$', views.create, name='create'),
  url(r'^save/$', views.save, name='save'), 
]

編輯views.py

#先導(dǎo)入時(shí)間模塊
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,再次運(yùn)行,enjoy it!

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

相關(guān)文章

  • Python 把兩層列表展開平鋪成一層(5種實(shí)現(xiàn)方式)

    Python 把兩層列表展開平鋪成一層(5種實(shí)現(xiàn)方式)

    這篇文章主要介紹了Python 把兩層列表展開平鋪成一層(5種實(shí)現(xiàn)方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 淺談python之新式類

    淺談python之新式類

    這篇文章主要介紹了淺談python之新式類,詳細(xì)的介紹了如何使用新式類和經(jīng)典類的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • python基礎(chǔ)之文件操作

    python基礎(chǔ)之文件操作

    這篇文章主要介紹了python基礎(chǔ)之文件操作,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有一定的幫助,需要的朋友可以參考下
    2021-04-04
  • Python?os和os.path模塊詳情

    Python?os和os.path模塊詳情

    這篇文章主要介紹了Python?os和os.path模塊詳情,文章圍繞主題展開詳細(xì)的相關(guān)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助
    2022-04-04
  • pytorch深度神經(jīng)網(wǎng)絡(luò)入門準(zhǔn)備自己的圖片數(shù)據(jù)

    pytorch深度神經(jīng)網(wǎng)絡(luò)入門準(zhǔn)備自己的圖片數(shù)據(jù)

    這篇文章主要為大家介紹了pytorch深度神經(jīng)網(wǎng)絡(luò)入門準(zhǔn)備自己的圖片數(shù)據(jù)示例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Flask的圖形化管理界面搭建框架Flask-Admin的使用教程

    Flask的圖形化管理界面搭建框架Flask-Admin的使用教程

    Flask-Admin是一個(gè)為Python的Flask框架服務(wù)的微型框架,可以像Django-Admin那樣為用戶生成Model層面的數(shù)據(jù)管理界面,接下來就一起來看一下Flask的圖形化管理界面搭建框架Flask-Admin的使用教程
    2016-06-06
  • Python中str.format()方法的具體使用

    Python中str.format()方法的具體使用

    本文主要介紹了Python中str.format()方法的具體使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 用python畫一只帥氣的皮卡丘

    用python畫一只帥氣的皮卡丘

    大家好,本篇文章主要講的是用python畫一只帥氣的皮卡丘,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • 一文秒懂python讀寫csv xml json文件各種騷操作

    一文秒懂python讀寫csv xml json文件各種騷操作

    多年來,數(shù)據(jù)存儲(chǔ)的可能格式顯著增加,但是,在日常使用中,還是以 CSV 、 JSON 和 XML 占主導(dǎo)地位。 在本文中,我將與你分享在Python中使用這三種流行數(shù)據(jù)格式及其之間相互轉(zhuǎn)換的最簡單方法,需要的朋友可以參考下
    2019-07-07
  • windows下Python實(shí)現(xiàn)將pdf文件轉(zhuǎn)化為png格式圖片的方法

    windows下Python實(shí)現(xiàn)將pdf文件轉(zhuǎn)化為png格式圖片的方法

    這篇文章主要介紹了windows下Python實(shí)現(xiàn)將pdf文件轉(zhuǎn)化為png格式圖片的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python實(shí)現(xiàn)將pdf轉(zhuǎn)換為png格式的相關(guān)模塊、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-07-07

最新評(píng)論