猿问

如何在 Django 中创建时间表

所以,我想创建一个 Django 培训计划。我的数据库模型看起来


models.py


from django.db import models

from django.contrib.auth.models import User

from django.utils import timezone



class Coach(models.Model):

    user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name="Пользователь", related_name="coach")

    name = models.CharField(max_length=200)

    phone = models.CharField(max_length=200)

    desc = models.CharField(max_length=500)

    avatar = models.ImageField(upload_to="coach_avatars/", blank=True)


    def __str__(self):

        return self.user.get_full_name()


class TypeOfTraining(models.Model):

    coach = models.ForeignKey(Coach, on_delete=models.CASCADE, related_name="training_type")

    name = models.CharField(max_length=100)

    desc = models.CharField(max_length=500)

    price = models.FloatField(default=0)


    def __str__(self):

        return self.name



class TrainingSchedule(models.Model):

    coach = models.ForeignKey(Coach, on_delete=models.CASCADE)

    training_type = models.ForeignKey(TypeOfTraining, on_delete=models.CASCADE)

    start_at = models.DateTimeField()


    def __str__(self):

        return str(self.id)

如何以看起来像图像的 html 形式显示数据库中的数据? 示例:HTML 页面上的日程安排看起来如何


我已经做了HTML模板。但我不知道如何在模板中显示数据库中的数据。


PS 抱歉我的英语。


凤凰求蛊
浏览 97回答 1
1回答

潇湘沐

Django 基本上遵循 MVC 模式。您在此处定义了模型,视图是通过模板(即dashboard.html)实现的,控制器可以是Form您的项目中的。沿着这些思路:你的dashboard.py:from django import formsfrom .models import Coach, TypeOfTraining, TrainingScheduleclass DashboardForm(forms.Form):&nbsp; &nbsp; def dashboard(request):&nbsp; &nbsp; &nbsp; &nbsp; # Extract data from your models here.&nbsp; &nbsp; &nbsp; &nbsp; return render(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "dashboard.html",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "form": form,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "weight_loss_name": some_name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "weight_loss_amount": some_weight_loss,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })和你的dashboard.html:<!-- header and navigation and what not --><section class="weightloss">&nbsp; <h2>Weight Loss</h2>&nbsp; <div class="person">{{ weight_loss_name }}</div>&nbsp; <div class="amount">{{ some_weight_loss }}</div></section><!-- rest of the page -->
随时随地看视频慕课网APP

相关分类

Html5
我要回答