Bootstrap来自Twitter的前端框架,基于HTML、CSS、JavaScript,简洁灵活,使得Web开发更快捷,而Django-bootstrap3是将Bootstrap3集成到了Django中,作为Django的一个应用,从而快速的创建Web前端页面
E:\Programs\Python\Python_Django_Interface>pip install django-bootstrap3
Collecting django-bootstrap3
Downloading https://files.pythonhosted.org/packages/76/af/c1854e4c4e513c27c7bf08d52dadcadd41717562a548189659e4df080464/django-bootstrap3-11.1.0.tar.gz (42kB)
100% |████████████████████████████████| 51kB 40kB/s
Building wheels for collected packages: django-bootstrap3
Building wheel for django-bootstrap3 (setup.py) ... done
Stored in directory: C:\Users\davieyang\AppData\Local\pip\Cache\wheels\63\0a\a5\4ecad44ab63d90133ede95c2e7d100a7a3ed94f5a0a73b924c
Successfully built django-bootstrap3
Installing collected packages: django-bootstrap3
Successfully installed django-bootstrap3-11.1.0
然后修改…/guest/settings.py文件,添加bootstrap3应用
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'sign',
'bootstrap3',
]
修改…/sign/views.py中的event_manage()视图函数
@login_required
def event_manage(request):
#username = request.COOKIES.get('user', '') # 读取浏览器 cookie
username = request.session.get('user', '') # 读取浏览器 session
events = Event.objects.all() # 查询所有发布会数据
return render(request, "event_manage.html", {"user": username, "events": events}) # 通过render附加在event_manage.html页面返回给客户端
修改…/templates/event_manage.html页面文件
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Guest Managetitle>
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript %}
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 Systema>
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>
div>
nav>
<div class="row" style="padding-top: 80px">
<div class="col-md-6" style="width: 900px;">
<table class="table table-striped">
<thead>
<tr>
<th>idth>
<th>名称th>
<th>地址th>
<th>时间th>
<th>人数th>
<th>状态th>
<th>签到th>
tr>
thead>
<tbody>
{% for event in events %}
<tr>
<td>{{ event.id }}td>
<td>{{ event.name }}td>
<td>{{ event.address }}td>
<td>{{ event.start_time | date:"Y-m-d H:i:s" }}td>
<td>{{ event.limit }}td>
<td>{{ event.status }}td>
<td><a href="/sign_index/{{ event.id }}/" target="{{ event.id }}_blank">signa>td>
tr>
{% endfor %}
{{ hint }}
tbody>
table>
div>
div>
body>
html>
重新登陆系统,到http://127.0.0.1:8000/event_manage/页面,如图显示:
继续修改…/templates/event_manage.html文件中添加搜索菜单
<div class="page-header" style="padding-top: 60px;">
<div id="navbar" class="navbar-collapse collapse">
<form class="navbar-form" method="get" action="/search_name/">
<div class="form-group">
<input name="name" type="text" placeholder="名称" class="form-control">
div>
<button type="submit" class="btn btn-success">搜索button>
form>
div>
<div>
<button type="submit" class="btn btn-primary" onclick="window.location.href='/add_event/'" style="margin-top: 8px;">添加button>
div>
div>
刷新页面如下所示:
为搜索添加路由,需改…/guest/urls.py文件
from django.contrib import admin
from django.urls import path
from sign import views
from django.conf.urls import url
urlpatterns = [
path(r'admin/', admin.site.urls),
path(r'index/', views.index), # 添加index/路径配置
url(r'login_action/', views.login_action),
url(r'event_manage/', views.event_manage),
url(r'^$', views.index),
url(r'^index/$', views.index),
url(r'^accounts/login/$', views.index),
url(r'^search_name/$', views.search_name),
]
添加search_name()函数,修改…/sign/views.py文件
# 发布会名称搜索
@login_required
def search_name(request):
username = request.session.get('user', '')
search_name = request.GET.get("name", "")
#search_name_bytes = search_name.encode(encoding="utf-8")
events = Event.objects.filter(name__contains=search_name)
if len(events) == 0:
return render(request, "event_manage.html", {"user": username,
"hint": "根据输入的 `发布会名称` 查询结果为空!"})
return render(request, "event_manage.html", {"user": username, "events": events})