实战项目(一) Django 搭建数据库

2021-02-07  本文已影响0人  巴鶴

参考学习-实战项目(二)-嘉宾发布会签到系统 https://www.jianshu.com/p/5762848974af

引用: Django 提供完善的模型(model)层主要用来创建和存取数据,不需要我们直接对数据库操作。
Django 模型基础知识:
每个模型是一个Python 类,继承django.db.models.model 类。该模型的每个属性表示一个数据库表字段。所有这一切,已经给你一个自动生成的数据库访问的API。

安装bootstrap3

pip install django-bootstrap3


安装bootstrap3.png

event_manage.html 发布会页前端配置

<html lang="zh-CN">
<head>
{% load bootstrap3 %}
{% bootstrap_css %} 
{% bootstrap_javascript %}
<title>Guest Manage</title>
</head>
<body role="document">
 <!-- 导航栏 -->
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="/event_manage/">Guest Manage
                    System</a>
            </div>
            <div id="navbar" class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="#">发布会</a></li>
                    <li><a href="/guest_manage/">嘉宾</a></li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#">{{user}}</a></li>
                    <li><a href="/logout/">退出</a></li>
                </ul>
            </div>
            /.nav-collapse
        </div>
    </nav>
  
    <div class="container theme-showcase" role="main">
  
<!-- 发布会表单--搜索 -->
        <div class="page-header">
            <div id="navbar" class="navbar-collapse collapse">
                <form class="navbar-form" method="get" action="/search_name/"style="padding-top: 30px;">
                    <div class="form-group">
                        <input name="search_name" type="text" placeholder="名称"class="form-control" style="width: 370px;">                            
                    </div>
                    <button type="submit" class="btn btn-success">搜索</button>
                </form>
            </div>
            <!--/.navbar-collapse -->
        </div>
          
<!-- 发布会表单 -->
        <div class="row" style="padding-top: 18px;">
            <div class="col-md-6">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>id</th>
                            <th>名称</th>
                            <th>状态</th>
                            <th>地址</th>
                            <th>时间</th>
                            <th style="width:37px;">签到</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for event in events %}
                        <tr>
                            <td>{{ event.id }}</td>
                            <td>{{event.name }}</td>
                            <td>{{ event.status }}</td>
                            <td>{{ event.address }}</td>
                            <td>{{ event.start_time }}</td>
                            <td><a href="/sign_index/{{ event.id }}/" target="{{ event.id }}_blank">sign</a></td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
</body>
</html>

guest_manage.html 嘉宾页前端配置

<html lang="zh-CN">
<head>
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript %}
<title>Guest Manage</title>
</head>
<body role="document">
    <!-- 导航栏 -->
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <a class="navbar-brand" href="/event_manage/">Guest Manage System</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
          <li><a href="/event_manage/">发布会</a></li>
          <li class="active"><a href="#">嘉宾</a></li>           
          </ul>
          <ul class="nav navbar-nav navbar-right">
           <li><a href="#">{{user}}</a></li>
           <li><a href="/logout/">退出</a></li>
         </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>
<div class="container theme-showcase" role="main">
 
      <!--嘉宾表--搜索-->
      <div class="page-header">
        <div id="navbar" class="navbar-collapse collapse">
          <form class="navbar-form" method="get" action="/search_phone/" style="padding-top: 15px;">
            <div class="form-group">
              <input name="search_phone" type="text" placeholder="手机号" class="form-control" style="width: 330px;">
            </div>
            <button type="submit" class="btn btn-success">搜索</button>
          </form>
          <form class="navbar-form" method="get" action="/search_realname/" style="padding-top: 15px;">
            <div class="form-group">
              <input name="search_realname" type="text" placeholder="姓名" class="form-control" style="width: 330px;">
            </div>
            <button type="submit" class="btn btn-success">搜索</button>
          </form>
        </div><!--/.navbar-collapse -->
      </div>
      <!--嘉宾表-->
      <div class="row" style="padding-top: 20px;">
        <div class="col-md-6" >
          <table class="table table-striped">
            <thead>
              <tr>
                <th>id</th>
                <th>姓名</th>
                <th>手机</th>
                <th>Email</th>
                <th>签到</th>
                <th style="width: 80px;">发布会id</th>
              </tr>
            </thead>
           <tbody>
             {% for guest in guests %}
               <tr>
                 <td>{{guest.id}}</td>
                 <td style="width:300px; height:40px;"> {{guest.realname}}</td>
                 <td> {{guest.phone}}</td>
                 <td >{{ guest.email}}</td>
                 <td > {{guest.sign}}</td>
                 <td style="width:300px; height:40px;">{{ guest.event}}</td>
               </tr>
             {% endfor %}
            </tbody>
            </table>
</div>
</div>
<!--嘉宾表--分页-->
<div class="pagination">
      <span class="step-links">
        {% if guests.has_previous%}
          <a href="?page={{ guests.previous_page_number }}">上一页</a>
        {% endif %}
          <span class="current">
            Page {{ guests.number }} of {{ guests.paginator.num_pages }}.
          </span>
        {% if guests.has_next%}
          <a href="?page={{ guests.next_page_number }}">下一页</a>
        {% endif %}
      </span>
</div>
</body>
</html>

设计系统表

代码脚本参考

from django.db import models
 
# Create your models here.
# 发布会表
class Event(models.Model):
    name = models.CharField(max_length = 100) #发布会标题长度控制
    limit = models.IntegerField() #参加人数
    status = models.BooleanField() #状态
    address = models.CharField(max_length = 200) #地址长度控制
    create_time = models.DateTimeField(auto_now = True) #创建时间 (自动获取当前时间)
    start_time = models.DateTimeField('events time') #发布会时间
    def __str__(self):
        return self.name
     
#嘉宾表
class Guest(models.Model):
    event = models.ForeignKey(Event) #关联发布会id
    realname = models.CharField(max_length = 64) #姓名长度控制
    phone = models.CharField(max_length =16) #手机号长度控制
    email = models.EmailField() #邮箱
    sign = models.BooleanField() # 状态
    create_time = models.DateTimeField(auto_now = True) #创建时间 (自动获取当前时间)
    class Meta:
        unique_together = ('event','phone')
         
    def __str__(self):
        return self.realname
目录结构.jpg
上一篇下一篇

猜你喜欢

热点阅读