this my form.py :
class SchoolForm(forms.Form):
attr_options = forms.MultipleChoiceField(
label='خيارات إضافية',
required=False,
widget=forms.CheckboxSelectMultiple
)
teachers = forms.ModelMultipleChoiceField(
queryset=Services.objects.all(),
label='اختيار المعلمين',
widget=forms.CheckboxSelectMultiple
)
and this my view.py
def schools_dashboard(request):
subscribe_key = 'school'
html_name = 'schools_dashboard.html'
# جلب جميع الخدمات التي تتعلق بـ 'school'
services = Services.objects.filter(category='school')
if request.user.is_authenticated:
try:
subscription = Subscription.objects.get(user=request.user)
except Subscription.DoesNotExist:
return render(request, 'faulty_subscription.html',
{'header': 'غير مشترك', 'text': f'يبدو انك غير مشترك في اي خطة',
'page_link': 'signup/subscription/'})
if subscription.subscription_type != subscribe_key:
subscribe_key = Subscription.objects.get(user=request.user)
return render(request, 'faulty_subscription.html', {'header': 'مشترك و لكن !! ',
'text': f'اشتراكك اشتراك {subscriptions_map(subscription.subscription_type)} و هذه صفحته ',
'page_link': subscription.subscription_type})
if request.method == 'POST':
form = SchoolForm(request.POST)
if form.is_valid():
service = request.POST.get('service','')
attr_options = form.cleaned_data['attr_options']
teachers = form.cleaned_data['teachers']
# معالجة البيانات هنا
return redirect('Schools Dashboard') # تغيير هذا إلى عنوان URL المناسب
else:
form = SchoolForm()
# تحديث خيارات إضافية بناءً على الخدمة المحددة
selected_service_name = request.GET.get('service')
if selected_service_name:
try:
service = Services.objects.get(name=selected_service_name)
attr_choices = [(option.split(',')[1].strip(), option.split(',')[0].strip()) for option in service.attr.split('/')]
form.fields['attr_options'].choices = attr_choices
except Services.DoesNotExist:
pass
return render(request, html_name, {'form': form, 'services': services})
else:
return render(request, template_name='signin.html')
and this my html
<!-- schools_dashboard.html -->
{% load static %}
<!DOCTYPE html>
<html lang="ar">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>نموذج المدرسة</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="{% static 'css/inst.css' %}">
</head>
<body>
<div class="container">
<!-- Banner Section -->
<div class="banner">
<h2>{{ user.username }}</h2>
<a href="{% url 'signout' %}" class="btn btn-custom">تسجيل الخروج</a>
</div>
<!-- Form Section -->
<div class="row justify-content-center">
<div class="col-md-8 form-container">
<h2 class="text-center mb-4">نموذج المدرسة</h2>
<form method="get" action="{% url 'Schools Dashboard' %}">
<div class="form-group">
<label for="service">اختر الخدمة</label>
<select class="form-control" id="service" name="service" onchange="this.form.submit()">
<option value="" disabled selected>اختر خدمة</option>
{% for service in services %}
<option value="{{ service.name }}" {% if request.GET.service == service.name %}selected{% endif %}>
{{ service.name }}
</option>
{% endfor %}
</select>
</div>
</form>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary btn-block">إرسال</button>
</form>
</div>
</div>
</div>
</body>
</html>