嘉宾管理及签到功能(十二)

1.嘉宾管理列表页面,新建../templates/guest_manage.html页面。

<html lang="zh-CN">
<head>
    {% load bootstrap3 %}
    {% bootstrap_css %}
    {% bootstrap_javascript %}
    <title>Guest Managetitle>
    }
head>
<body>
    

    <nav class="navbar navbar-inverse navbar-fixed-top">

      <div class="container">

        <div class="navbar-header">

          <a class="navbar-brand" href="/guest_manage/">Guest Manage Systema>

        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="#about">嘉宾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">

          <table class="table table-striped">

            <thead><tr>

                <th>idth>

                <th>名称th>

                <th>手机th>

                <th>Emailth>

                <th>签到th>

                <th>发布会idth>

              tr>

            thead>

            <tbody>

              {% for guest in guests %}

                <tr>

                  <td>{{ guest.id }}td>

                  <td>{{ guest.realname }}td>

                  <td>{{ guest.phone }}td>

                  <td>{{ guest.email }}td>

                  <td>{{ guest.sign }}td>

                  <td>{{ guest.event }}td>

                tr>

             {% endfor %}

            tbody>

          table>

        div>

      div>
body>
html>

2.在../FirstProject/urls.py文件中添加嘉宾路径的路由。

from django.conf.urls import url
from django.contrib import admin
from FirstApp import views

urlpatterns = [
    url(r'^$', views.index),
    url(r'^admin/', admin.site.urls),
    url(r'^index/$', views.index),
    url(r'^login_action/$', views.login_action),
    url(r'^event_manage/$', views.event_manage),
    url(r'^accounts/login/$', views.index),
    url(r'^search_name/$', views.search_name),
    url(r'^guest_manage/$', views.guest_manage),
]

3.打开../FirstApp/views.py文件,创建guest_manage()视图函数。

# 嘉宾管理
@login_required
def guest_manage(request):
    username = request.session.get('user', '')
    guest_list = Guest.objects.all()
    return render(request, "guest_manage.html", {"user":username, "guests":guest_list})

导入model中的guest类,通过guest.objects.all()查询所有嘉宾对象(数据),并通过render()方法附加在guest_manage.html页面,并返回给客户端。
到此,嘉宾列表页面完成。

4.分页器

Django提供了Paginator类来实现分页功能。打开../FirstApp/views.py文件,修改guest_manage()视图函数。

@login_required

def guest_manage(request):

    guest_list = Guest.objects.all()

    username = request.session.get('username', '')

    paginator = Paginator(guest_list, 2)

    page = request.GET.get('page')

    try:

        contacts = paginator.page(page)

    except PageNotAnInteger:

        # 如果页数不是整型, 取第一页.

        contacts = paginator.page(1)

    except EmptyPage:

        # 如果页数超出查询范围,取最后一页

        contacts = paginator.page(paginator.num_pages)

    return render(request, "guest_manage.html", {"user": username, "guests": contacts})
paginator = Paginator(guest_list, 2)

把查询出来的所有嘉宾列表guest_list放到Paginator类中,划分每页显示2条数据。

page = request.GET.get(‘page’)

通过GET请求得到当前要显示第几页的数据。
获取第page页的数据,如果没有第page页,抛出PageNotAnInteger异常,返回第一页的数据。如果超出页数范围,则抛出EmptyPage异常,返回最后一页的数据。

5.将得到的某页数据返回到嘉宾管理页面上,在../templates/guest_manage.html文件中添加分页器的代码。添加在嘉宾列表下面



    <div class="pagination">

      <span class="step-links">

        {% if guests.has_previous %}

          <a href="?phone={{ phone }}&page={{ guests.previous_page_number }}">previousa>

        {% endif %}

          <span class="current">

            Page {{ guests.number }} of {{ guests.paginator.num_pages }}.

          span>

        {% if guests.has_next %}

          {% if phone %}

            <a href="?phone={{ phone }}&page={{ guests.next_page_number }}">nexta>

          {% else %}

           <a href="?page={{ guests.next_page_number }}">nexta>

          {% endif %}

        {% endif %}

      span>

    div>

6.签到功能,添加签到链接,在../templates/event_manage.html页面,增加一列签到链接。

 

      <div class="page-header">

        <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>



      <div class="row">

        <div class="col-md-6">

          <table class="table table-striped">

            <thead>

              <tr>

                <th>idth>

                <th>名称th>

                <th>状态th>

                <th>地址th>

                <th>时间th>

                <th style="width: 45px;">签到th>

                <th>签到正式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">signa>td>

                  <td><a href="/sign_index2/{{ event.id }}/"  target="{{ event.id }}_blank">sign_weba>td>

                tr>

             {% endfor %}

            tbody>

          table>

        div>

      div>

7.在../FirstProject/urls.py文件中添加签到页面路径的路由。

from django.conf.urls import url
from django.contrib import admin
from FirstApp import views

urlpatterns = [
    url(r'^$', views.index),
    url(r'^admin/', admin.site.urls),
    url(r'^index/$', views.index),
    url(r'^login_action/$', views.login_action),
    url(r'^event_manage/$', views.event_manage),
    url(r'^accounts/login/$', views.index),
    url(r'^search_name/$', views.search_name),
    url(r'^guest_manage/$', views.guest_manage),
    url(r'^sign_index/(?P[0-9]+)/$', views.sign_index),
]

8.创建签到页面,打开../FirstApp/views.py文件,创建sign_index()视图函数。

# 签到页面

@login_required

def sign_index(request, event_id):

    event = get_object_or_404(Event, id=event_id)

 return render(request, 'sign_index.html', {'even':event})

9.创建签到页面,打开../templates/sign_index.html.

<html>
<head>
    <title>Sign Managetitle>
head>
<body>
     
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
            <a class="navbar-brand" href="#">{{ event.name }}a>
        div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li><a href="/event_manage/">发布会a>li>
            <li><a href="/guest_manage/">嘉宾a>li>
          ul>
        div>
      div>
    nav>
    
    <div class="container theme-showcase" role="main">
        <div id="navbar" class="navbar-collapse collapse">
            <form class="navbar-form" method="post" action="/sign_index_action/{{event.id}}/">
                <div class="form-group">
                    <input name="phone" type="text" placeholder="输入手机号" class="form-control">input>
                div>
                <button type="submit" class="btn btn-success">签到button>
                <font color="red">
                    <br>{{hint}}
                    <br>{{guest.realname}}
                    <br>{{guest.phone}}
                font>
            form>
        div>
    div> 
body>
html>
<form class="navbar-form" method="post" action="/sign_index_action/{{event.id}}/">

签到表单通过POST请求将签到手机号提交到/sign_index_action /{{event.id}}/路径,{{event.id}}替换为具体的发布会id。

<font color="red">
                    <br>{{hint}}
                    <br>{{guest.realname}}
                    <br>{{guest.phone}}
                font>
{{hint}}用于显示签到成功和失败的提示信息;
当签到成功时,{{guest.realname}}和{{guest.phone}}将显示嘉宾的姓名和手机号。

10.签到动作,打开../FirstProject/urls.py文件,添加签到动作路径的路由。

from FirstApp import views

urlpatterns = [
    .......
    url(r'^sign_index_action/(?P[0-9]+)/$', views.sign_index_action),
]

11.打开../FirstApp/views.py文件,创建sign_index_action()视图函数。

# 签到动作
@login_required
def sign_index_action(request,eid):
    event = get_object_or_404(Event, id=eid)
    phone = request.POST.get(Event, id=eid)
    print(phone)
    result = Guest.objects.filter(phone=phone)
    if not result:
        return render(request, 'sign_index.html', {'event':event, 'hint':'phone error.'})

    result = Guest.objects.filter(phone=phone, event_id = eid)
    if not result:
        return render(request, 'sign_index.html', {'event':event, 'hint':'event id or phone error.'})

    result = Guest.objects.filter(phone=phone, event_id = eid)
    if result.sign:
        return render(request, 'sign_index.html', {'event':event, 'hint':'user has sign in.'})
    else:
        Guest.objects.filter(phone=phone, event_id = eid).update(sign='1')
        return render(request, 'sign_index.html', {'event':event, 'hint':'sign in success!', 'guest':result})

12.退出系统,打开../urls.py文件,添加退出路径的路由。

from FirstApp import views

urlpatterns = [
    ......
    url(r'^logout/$', views.logout),
]

13.打开../FirstApp/views.py文件,创建logout()视图函数。

# 退出登陆
@login_required
def logout(request):
    auth.logout(request) # 退出登陆
    response = HttpResponseRedirect('/index/')
    return response

Django不但提供了auth.login()方法用于验证登陆用户信息,同时也提供了auth.logout()方法用于系统的退出,它可以清除浏览器保存的用户信息,所以,不用再考虑如何删除浏览器cookie的问题。

你可能感兴趣的:(嘉宾管理及签到功能(十二))