1、创建模型

BugCalendar > hunter > models.py

  1. from django.db import models
  2. # 白帽子的基本信息
  3. class UserInfo(models.Model):
  4. # 白帽子昵称
  5. nick = models.CharField(max_length=32, blank=False, verbose_name="白帽子内容")
  6. # 白帽子邮箱
  7. email = models.EmailField(verbose_name="白帽子邮箱")
  8. # 活动信息
  9. class Activity(models.Model):
  10. # 活动标题
  11. title = models.CharField(max_length=64, blank=False, verbose_name='活动标题')
  12. # 活动内容
  13. context = models.TextField(verbose_name='活动内容')

2、激活模型

BugCalendar > settings.py

  1. INSTALLED_APPS = [
  2. 'hunter.apps.HunterConfig',
  3. ...
  4. ]
  1. python manage.py makemigrations
  1. python manage.py migrate hunter 0001

3、测试数据

  1. python manage.py shell

image.png

4、视图

BugCalendar > hunter > views.py

  1. from . import models
  2. def show_hunter(request):
  3. all_hunter = models.UserInfo.objects.all()
  4. context = {'all_hunter':all_hunter}
  5. template_name = 'hunter/show_hunter.html'
  6. return render(request, template_name, context)

5、模型

BugCalendar > hunter > templates > hunter > show_hunter.html

  1. <table>
  2. <tr>
  3. <th>昵称</th>
  4. <th>邮箱</th>
  5. </tr>
  6. <tr>
  7. {% for hunter in all_hunter %}
  8. <td> {{hunter.nick}} </td>
  9. <td> {{hunter.email}} </td>
  10. {% endfor %}
  11. </tr>
  12. </table>

6、路由

BugCalendar > BugCalendar > urls.py

  1. from django.urls import path
  2. urlpatterns = [
  3. path('calendar/', include('hunter.urls')),
  4. ]

BugCalendar > hunter > urls.py

  1. from django.urls import path
  2. from . import views
  3. urlpattern = [
  4. path('hunter/', views.show_hunter, name='show_hunter')
  5. ]

7、浏览器请求

http://127.0.0.1:8080/calendar/hunter/
image.png