a Doctor Appointment Booking Using Django Forms

my booking

                    <div class="accordion" id="accordionExample">
                        <div class="accordion-item">
                            <h2 class="accordion-header" id="headingOne">
                                <button class="accordion-button" type="button" data-bs-toggle="collapse"
                                        data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                                    نزدیک ترین زمان
                                </button>
                            </h2>
                            <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne"
                                 data-bs-parent="#accordionExample">
                                <div class="accordion-body">
                                    ...
                                </div>
                            </div>
                        </div>
                        <div class="accordion-item">
                            <h2 class="accordion-header" id="headingTwo">
                                <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
                                        data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                                    انتخاب دستی
                                </button>
                            </h2>
                            <div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo"
                                 data-bs-parent="#accordionExample">
                                <div class="accordion-body">
                                    {% for bookable in bookables %}
                                        <p>{{ bookable.day }} - {{ bookable.start_time }} - {{ bookable.end_time }}
                                            - {{ bookable.booking.date }}</p>
                                    {% empty %}
                                        <p>No bookables available.</p>
                                    {% endfor %}
                                </div>
                            </div>
                        </div>
                    </div>

my bookable form

<form class="modal-body" method="post">
    {% csrf_token %}
    <div class="row justify-content-around">
        <div class="col-lg-5">
            <label for="{{ form.start_time.id_for_label }}"
                   class="d-block w-100 text-muted">
                زمان شروع
            </label>
            {{ form.start_time }}
            <label for="{{ form.end_time.id_for_label }}"
                   class="d-block w-100 text-muted">
                زمان پایان
            </label>
            {{ form.end_time }}
        </div>
        <div class="col-lg-5">
            <label for="{{ form.day.id_for_label }}"
                   class="d-block w-100 text-muted">
                روز های ویزیت
            </label>
            {{ form.day }}
            <label for="{{ form.capacity.id_for_label }}"
                   class="d-block w-100 text-muted">
                ظرفیت
            </label>
            {{ form.capacity }}
        </div>
    </div>
    <button class="btn btn-success px-5 p-2 mt-4 rounded-md">
        ذخیره
    </button>
</form>

my form

class BookableForm(forms.ModelForm):
    class Meta:
        model = Bookable
        fields = "__all__"
        widgets = {
            'start_time': forms.TimeInput(
                attrs={'class': 'form-control mb-3', 'data-jdp': 'data-jdp', 'autocomplete': 'off'}
            ),
            'end_time': forms.TimeInput(
                attrs={'class': 'form-control mb-3', 'data-jdp': 'data-jdp', 'autocomplete': 'off'}
            ),
            'capacity': forms.NumberInput(attrs={'class': 'form-control mb-3', 'autocomplete': 'off'}),
            'day': forms.Select(attrs={'class': 'form-control mb-3', 'autocomplete': 'off'}),
        }

    def clean(self):
        cleaned_data = super().clean()
        start_time = cleaned_data.get('start_time')
        end_time = cleaned_data.get('end_time')

        if start_time and end_time and start_time > end_time:
            raise forms.ValidationError("زمان شروع نمی‌تواند بزرگتر تر از زمان پایان باشد.")
        return cleaned_data

my modals

from django.db import models
from accounts.models import User
from django.utils import timezone

days = (
    ('1', 'شنبه'),
    ('2', 'یک‌شنبه'),
    ('3', 'دوشنبه'),
    ('4', 'سه‌شنبه'),
    ('5', 'چهارشنبه'),
    ('6', 'پنج‌شنبه'),
)


class Bookable(models.Model):
    day = models.CharField(max_length=50, choices=days, unique=True,
                           error_messages={'unique': "تایمی برای این روز قبلا ثبت شده است."})
    start_time = models.TimeField()
    end_time = models.TimeField()
    capacity = models.IntegerField()

    @property
    def is_full(self) -> bool:
        return False

    @property
    def reserved_count(self) -> int:
        return 1

    def __str__(self):
        return f"{self.get_day_display()} | بیمار: {self.capacity}"


class Booking(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    bookable = models.ForeignKey(Bookable, on_delete=models.CASCADE)
    date = models.DateField(default=timezone.now)

    def __str__(self):
        return f"{self.user} | {self.bookable.day}"

my view

@login_required
def booking(request):
    if request.method == 'POST':
        form = BookableForm(request.POST)
        if form.is_valid():
            bookable = form.save(commit=False)
            bookable.save()
            booking = Booking(user=request.user, bookable=bookable)
            booking.save()
            return redirect('booking_success')
    else:
        form = BookableForm()

    context = {
        form: 'form'
    }
    return render(request, 'Main/Booking/booking.html', context)


def get_reservation_times():
    return {}


def booking_success(request):
    return render(request, 'Main/Booking/booking_success.html')

help me for write my view currently that we can make a reservation

i tried to write my view but i cant please help Finally, we pass the form and the filtered bookables to the template and render the page.

Note that this is just an example, and you may need to modify it to fit your specific use case. For example, you may want to add additional validation to ensure that the user has selected a valid bookable time slot, or you may want to handle errors differently.

New contributor

ehs4nw ehs4nw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật