视图

BugCalendar/hunter/views.py

  1. from . import models
  2. from django.http import HttpResponseRedirect
  3. from django.shortcuts import render
  4. # 增加白帽子信息
  5. def add_hunter(request):
  6. try:
  7. hunter = models.UserInfo(nick=request.POST['nick'], email=request.POST['email'])
  8. hunter.save()
  9. return HttpResponseRedirect(reverse('show_hunter'))
  10. except:
  11. template_name = 'hunter/add_hunter'
  12. return render(request, template_name)
  13. # 删除白帽子信息
  14. def delete_hunter(request, hunter_id):
  15. hunter = models.UserInfo(id=hunter_id)
  16. hunter.delete()
  17. return HttpResponseRedirect(reverse('show_hunter'))
  18. # 修改白帽子信息
  19. def update_hunter(request, hunter_id):
  20. hunter = models.UserInfo.objects.get(id=hunter_id)
  21. try:
  22. hunter.nick = request.POST['nick']
  23. hunter.email = request.POST['email']
  24. hunter.save()
  25. return HttpResponseRedirect(reverse('show_hunter'))
  26. except:
  27. template_name = 'hunter/modify_hunter'
  28. context = {'hunter', hunter}
  29. return render(reqeust, template_name, context)
  30. # 查看所有白帽子信息
  31. def show_hunter(request):
  32. all_hunter = models.UserInfo.objects.all()
  33. context = {'all_hunter':all_hunter}
  34. template_name = 'hunter/show_hunter'
  35. return render(request, template_name, context)

模板

增加白帽子
BugCalendar/hunter/templates/hunter/add_hunter.html

  1. <form action="{% url 'add_hunter' %}" method="POST">
  2. <input type="text" name="nick"> <br/>
  3. <input type="text" name="email"> <br/>
  4. <input type="submit">
  5. </form>

删除白帽子
#修改白帽子
BugCalendar/hunter/templates/hunter/modify_hunter.html

  1. <form action="{%url 'update_hunter' hunter.id %}" method="POST">
  2. <input type="text", name="nick" value="{{ hunter.nick }}"> <br/>
  3. <input type="text", name="email" value="{{ hunter.email }}"> <br/>
  4. <input type="submit">
  5. </form>

查看白帽子
BugCalendar/hunter/templates/hunter/show_hunter.html

  1. <table>
  2. <tr>
  3. <th>昵称</th>
  4. <th>邮箱</th>
  5. <th>操作</th>
  6. </tr>
  7. {% for hunter in all_hunter %}
  8. <tr>
  9. <td>{{ hunter.nick }}</td>
  10. <td>{{ hunter.email }}</td>
  11. <td>
  12. <a href="{% url 'modify_hunter' hunter.id %}">修改</a> |
  13. <a href="{% url 'delete_hunter' hunter.id %}">删除</a>
  14. </td>
  15. </tr>
  16. {% endfor %}
  17. </table>

路由

BugCalendar/hunter/urls.py

  1. urlpattern = [
  2. path('add_hunter/', views.add_hunter, name='add_hunter'),
  3. path('delete_hunter/', views.delete_hunter, name='delete_hunter'),
  4. path('modify_hunter/', views.modify_hunter, name='modify_hunter'),
  5. path('show_hunter/', views.show_hunter, name='show_hunter'),
  6. ]