首页 > 解决方案 > 一个人在Django中预订后,如何使时间段(特定日期和时间)不出现?

问题描述

我正在创建一个网站,其中有一个可以预约(医生)的表格。我面临的问题是,如果选择了特定的时间段(针对特定的日期和时间),它不应该再出现在表格上,但我不知道该怎么做

表单看起来像这样(这是从views.py中的索引函数返回的)

 <section id="appointment" data-stellar-background-ratio="3">
      <div class="container">
           <div class="row">

                <div class="col-md-6 col-sm-6">
                     <img src="{%  static 'images/appointment-image.jpg' %}" class="img-responsive" alt="">
                </div>

                <div class="col-md-6 col-sm-6">
                     <!-- CONTACT FORM HERE -->
                     <form id="appointment-form" role="form" method="post" action="{% url 'test' %}">
                          {% csrf_token %}

                          <!-- SECTION TITLE -->
                          <div class="section-title wow fadeInUp" data-wow-delay="0.4s">
                               <h2>Make an appointment</h2>
                          </div>

                          <div class="wow fadeInUp" data-wow-delay="0.8s">
                               <div class="col-md-6 col-sm-6">
                                    <label for="name">Name</label>
                                    <input type="text" class="form-control" id="name" name="name" placeholder="Full Name">
                               </div>

                               <div class="col-md-6 col-sm-6">
                                    <label for="email">Email</label>
                                    <input type="email" class="form-control" id="email" name="email" placeholder="Your Email">
                               </div>

                               <div class="col-md-6 col-sm-6">
                                    <label for="date">Select Date</label>
                                    <input type="date" name="date" value="" class="form-control">
                               </div>

                               <div class="col-md-6 col-sm-6">
                                    <label for="select">Select Department</label>
                                    <select class="form-control" name="drop">
                                    {% for entry in items %}
                                         <option>{{ entry.category }}</option>
                                    {% endfor %}
                                    </select>
                               </div>

                               <div class="col-md-6 col-sm-6">
                                    <label for="select">Select Time</label>
                                    <select class="form-control" name="drop1">
                                    {% for entry in times %}
                                         <option>{{ entry.time }}</option>
                                    {% endfor %}
                                    </select>
                               </div>

                               <div class="col-md-12 col-sm-12">
                                    <label for="telephone">Phone Number</label>
                                    <input type="tel" class="form-control" id="phone" name="phone" placeholder="Phone">
                                    <label for="Message">Additional Message</label>
                                    <textarea class="form-control" rows="5" id="message" name="message" placeholder="Message"></textarea>
                                    <button type="submit" class="form-control" id="cf-submit" name="submit">Submit Button</button>
                               </div>
                          </div>
                    </form>
                </div>

           </div>
      </div>
 </section>

views.py,测试函数保存表单(到数据库)并发送电子邮件:

from django.shortcuts import render, get_object_or_404
from django.core.mail import send_mail
from .models import Form, categories, Doctors, News, timeslot
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth import authenticate

def index(request):
    time = timeslot.objects.all()
    item  = categories.objects.all() # use filter() when you have sth to filter ;)
    if request.method == 'POST':
        selected_item = get_object_or_404(Item, pk=request.POST.get('item_id'))
        selected_item2 = get_object_or_404(time, pk=request.POST.get('time_id'))
        # get the user you want (connect for example) in the var "user"
        categories.item = selected_item
        categories.save()
        timeslot.time = selected_item2
        timeslot.save()

        # Then, do a redirect for example

    return render(request, 'website/index.html', {'items':item, 'times':time }) 

def test(request):
    if request.method == "POST":
        name=request.POST['name']
        email=request.POST['email']
        date=request.POST['date']
        drop=request.POST.get('drop', False)
        time=request.POST.get('drop1', False)
        phone=request.POST['phone']
        message1=request.POST['message']
        
        message = "Name: "+ name + "\n" + "email: "+ email + "\n" + "Date of appointment: " + date + "\n" + "Time: " + time + "\n" "Service: " + drop + "\n" + "Number: " + phone + "\n" + "Special Message: "+ message1

        #send an email
        send_mail(
            'Make appointment for ' + name, #subject
            message, #the msg
            email, #from email
            ['madhavm2002@gmail.com'] #to email
            )
        #this is used to get the class from the models and then uses the variables assigned here to give value to the variables in models
        form=Form(name = name, email = email, date = date, time= time, drop = drop, phone = phone, msg1= message1)
        #this used the save method to save the object into the database
        form.save()
        return render(request, 'website/test.html', {'name':name})
    else:
        return render(request, 'website/index.html')

模型.py

from django.db import models

class Form(models.Model):
    msg_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=80)
    email = models.CharField(max_length=80)
    date = models.DateField()
    drop = models.CharField(max_length=80)
    time = models.TimeField()
    phone = models.CharField(max_length=80)
    msg1 =  models.CharField(max_length=500)
    status = models.IntegerField(default = 0)


class categories(models.Model):
    category = models.CharField(max_length=80)

class timeslot(models.Model):
    time = models.TimeField(max_length=80)  

PS:时间段看起来像上午 9 点、上午 10 点、上午 11 点...下午 1 点、下午 2 点等。类别给出了“牙科”、“生理”等对象

请告诉如何解决这个问题谢谢

标签: pythondjango

解决方案


本质上,您需要有一些东西来指示可用的时间段,然后将查询加入到您的约会中,您的模板中有一个条件来检查时间段是否已经有约会(或没有)并采取相应的行动。这是一个相当广泛的问题 - 请参阅 Brian 的说明。


推荐阅读