首页 > 解决方案 > 预约功能

问题描述

我有一个 Django 项目,它在 model.py 文件中包含两个类,一个是诊所(其中有一个联系号码作为字段),另一个是病人。我已经将所有诊所都呈现在卡片中,每张卡片都有一个按钮预约。

这是我的model.py:

class Clinic(models.Model):
    clinic_name = models.CharField(max_length=200, blank=True, verbose_name='Name')
    poster = models.ImageField(blank=True, null=True, upload_to='uploads/%Y/%m/%d', verbose_name='Picture')
    address = models.TextField(max_length=500, blank= True)
    contact_no = models.CharField(max_length=12, blank=True, verbose_name='Mobile')

    def __str__(self):
        return self.clinic_name

class Patient(models.Model):
    clinic = models.ForeignKey(Clinic, on_delete=CASCADE)
    patient_name = models.CharField(max_length=200, blank=True)
    contact_no = models.CharField(max_length=12, blank=True)
    appointment_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.patient_name

现在我想要的是,当一个在这种情况下有耐心的真实用户点击预约时,应该在诊所电话号码上发送一条消息,说这个人已经在这个日期与你预约了。

这怎么能实现???请指导我。

标签: javascriptpythondjango

解决方案


楷模:

from django import models

class Appointment(models.Model):
    clinic = models.ForeignKey(Clinic, on_delete=models.CASCADE, related_name='appointments')
    patient = models.ForeignKey(Patient, on_delete=models.CASCADE, related_name='appointments')
    time = models.DateTimeField()

    def __str__(self):
        return f'{self.clinic} - {self.patient} - {self.time}'

形式:

from django import forms
from .models import Appointment

class AppointmentForm(forms.ModelForm):
    class Meta:
        model = Appointment
        fields = ('clinic', 'time',)

网址:

from .views import book_appointment_view

app_name = '<appname>'
path('book-appointment/', book_appointment_view, name='book-appointment')

意见:

from django.urls import redirect
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseBadRequest

@login_required
def book_appointment_view(request):
    if request.method == 'POST':
        form = AppointmentForm(request.POST or None)
        user = request.user
        if form.is_valid():
            form.save(commit=False)
            form.instance.patient = user
            form.save()
            messages.success(request, f'Appointment booked {form.instance.patient.patient_name} !')
            return redirect('/')
    else:
        HttpResponseBadRequest('You can not do that')

html:

<form method="post" action="{% url '<appname>:book-appointment' %}">
    {% csrf_token %}
    {{ form }}
    <button type="submit">Send</button>
</form>

至于短信,请查看django-twillio的短信。我听说过好东西。


推荐阅读