在Django中实现足球场预订系统

时间:2019-02-01 15:47:51

标签: python django django-models view

我有两个应用程序;草坪和预订。 turfs app models.py中有一个Turf模型,其中包含有关地面的基本信息。预订应用程序包含具有信息的模型预订。关于预订场地。

我坚持写观点部分,以便在排除已经预订的理由之后显示可用的理由。

HTML表单:

<form>
    <div class="col-md-3 mb-3">
                    <div class="input-group date" id="datepicker1">
                            <label class="sr-only">Date From:</label>
                            <input type="text" class="form-control rounded" required name="datein" placeholder="Date From"/>
                            <span class="input-group-addon">
                                <i class="#"></i>
                            </span>
                    </div>
                </div>
                <div class="col-md-3 mb-3">
                    <div class="input-group date" id="datepicker2">
                            <label class="sr-only">Date To:</label>
                            <input type="text" class="form-control rounded" required name="dateout" placeholder="Date To"/>
                            <span class="input-group-addon">
                                <i class="#"></i>
                            </span>
                    </div>
                </div>

              </div>
              <!-- Form Row 2 -->
              <div class="form-row">
                <div class="col-md-6 mb-3">
                        <label class="sr-only">TimeIN</label>
                        <span style="font-family: Titillium Web, sans-serif;">Time in:</span>
                        <input type="time" required name="timein" class="form-control">
                </div>
                <div class="col-md-6 mb-3">
                  <label class="sr-only">TimeOUT</label>
                    <span style="font-family: Titillium Web, sans-serif;">Time out:</span>
                  <input type="time" required name="timeout" class="form-control">
                </div>
              </div>
              <button class="btn btn-secondary btn-block mt-4" type="submit"> Check Availability</button>
            </form>

草皮模型:

from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
# Create your models here.


class Turf(models.Model):
    name = models.CharField(max_length=200, unique=True)
    address = models.TextField()
    city = models.CharField(max_length=100)
    district = models.CharField(max_length=100)
    description = models.TextField(blank=True)
    photo_main = models.ImageField(upload_to='images/')
    photo_1 = models.ImageField(upload_to='images/', blank=True)
    photo_2 = models.ImageField(upload_to='images/', blank=True)
    starting_time = models.CharField(max_length=50)
    closing_time = models.CharField(max_length=50)
    is_24hr = models.BooleanField(default=False)
    is_featured = models.BooleanField(default=False)
    is_published = models.BooleanField(default=True)
    has_display = models.BooleanField(default=False)
    has_parking = models.BooleanField(default=True)
    sqft = models.IntegerField(default=20)
    state = models.CharField(max_length=100, default='hi')
    zipcode = models.CharField(max_length=20, default='hi')
    price_per_hour = models.IntegerField()
    pub_date = models.DateTimeField(default=datetime.now)
    built = models.CharField(max_length=5)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

预订模式:

from django.db import models
from turfs.models import Turf
# Create your models here.

class Booking(models.Model):
    datein = models.DateField()
    dateout = models.DateField()
    time_in = models.CharField(max_length=50)
    time_out = models.CharField(max_length=50)
    name = models.CharField(max_length=100)
    address = models.TextField()
    phone = models.CharField(max_length=20)
    email = models.CharField(max_length=50)
    turf_id = models.ForeignKey(Turf, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

1 个答案:

答案 0 :(得分:0)

您应该将Booking模型更改为使用日期时间字段,而不要使用日期和两个字符串。

class Booking(models.Model):
    start = models.DateTimeField()
    end = models.DateTimeField()

然后您的视图将如下所示:

requested_start # Some datetime
requested_end # Some datetime
is_available = not Booking.objects.filter(
    Q(start__range=[requested_start, requested_end])
    | Q(end__range=[requested_start, requested_end])
).exists()

您需要在请求的预订窗口中检查是否有任何开始或结束着陆,因此对Q个对象进行了或运算。