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.
ehs4nw ehs4nw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.