Reputation: 177
i'm trying to create an appointment app with multiple condition
in theory i find a way to make it happend but it dose not save the data an give me an error message the first part of the code work perfectly the 2nd part dose not save any data
i try a lot of things but nothing work
this is my code mybe some one see where the error is and give a hint to fix this
this is my views.py
@login_required
def create_appointement(request):
user = User()
#################################this the first part#####################"this part work perfectly
if request.user.is_patient():
form_appointement = AppointmentForm(request.POST or None)
if request.method=='POST':
form_appointement = AppointmentForm(request.POST or None)
if form_appointement.is_valid():
form_app = form_appointement.save(commit=False)
form_app.user_ho_add = request.user
form_app.patient = request.user
start_time = form_app.start_time
future_time = dt.datetime(1970, 1, 1, start_time.hour, start_time.minute, start_time.second, start_time.microsecond) + timedelta(minutes=30)
form_app.end_time = dt.time(future_time.hour, future_time.minute, future_time.second, future_time.microsecond)
form_app.save()
messages.success(request, 'appointment added')
else:
messages.error(request, 'Error')
return render(request,'appointement/add_appointement1.html',{'form':form_appointement})
#################this is the 2nd part##################### this the part that give me always the error message and dont save any data
else:
if request.method=='POST' and request.POST.get("type") == "register patient":
form_appointment_2 = AppointmentForm_2(request.POST or None)
user = User()
user2 = get_user_model()
patients = user2.objects.filter(type_of_user=TypeOfUser.PATIENT)
if form_appointment_2.is_valid():
form_appointment_2.save(commit=False)
form_appointment_2.user_ho_add = request.user
form_appointment.end_time = form_appointment.start_time + timedelta(minutes=30)
start_time = form_appointment_2.start_time
future_time = dt.datetime(1970, 1, 1, start_time.hour, start_time.minute, start_time.second, start_time.microsecond) + timedelta(minutes=30)
form_appointment_2.end_time = dt.time(future_time.hour, future_time.minute, future_time.second, future_time.microsecond)
form_appointment_2.save()
messages.success(request, 'appointment added')
else:
messages.error(request, 'Error')
return render(request, 'appointement/add_appointement2.html', {'user_form':form_appointment_2,'patients':patients })
elif request.method=='POST' and request.POST.get("type") == "non register patient":
form_appointment = AppointmentForm_3(request.POST or None)
if form_appointment.is_valid():
form_app = form_appointment.save(commit=False)
form_app.user_ho_add = request.user
form_appointment.end_time = form_appointment.start_time + timedelta(minutes=30)
start_time = form_appointment_2.start_time
future_time = dt.datetime(1970, 1, 1, start_time.hour, start_time.minute, start_time.second, start_time.microsecond) + timedelta(minutes=30)
form_appointment_2.end_time = dt.time(future_time.hour, future_time.minute, future_time.second, future_time.microsecond)
form_app.save()
messages.success(request, 'appointment added')
else:
messages.error(request, 'Error')
return render(request,'appointement/add_appointement3.html',{'user_form':form_appointment, })
else:
return render(request, 'appointement/add_appointement_options.html', )
return render(request, 'appointement/add_appointement_options.html', )
this is my forms.py
class AppointmentForm_2(forms.ModelForm):
doctor = forms.ModelChoiceField(queryset=User.objects.filter(type_of_user=TypeOfUser.DOCTOR))
# patient = forms.ModelChoiceField(queryset=User.objects.filter(type_of_user=TypeOfUser.PATIENT))
date = forms.DateField(widget=forms.DateInput(attrs={'type': 'date'}), input_formats=settings.DATE_INPUT_FORMATS)
start_time = forms.TimeField(widget=forms.DateInput(attrs={'type': 'time'}), input_formats=settings.TIME_INPUT_FORMATS)
class Meta:
model = Appointment
fields = ('patient', 'doctor', 'date', 'start_time')
class AppointmentForm_3(forms.ModelForm):
doctor = forms.ModelChoiceField(queryset=User.objects.filter(type_of_user=TypeOfUser.DOCTOR))
date = forms.DateField(widget=forms.DateInput(attrs={'type': 'date'}))
start_time = forms.DateField(widget=forms.DateInput(attrs={'type': 'time'}))
class Meta:
model = Appointment
fields = ('patient', 'doctor', 'date', 'start_time')
and this is my html files
add_appointement_otions.html
<body>
<div class="container">
<form action="" method="POST">
{% csrf_token %}
<div class="from-group">
<label for="">select the type of appointment</label>
<select name="type" id="">
<option value="">-- select type --</option>
<option value="register patient">register patient</option>
<option value="non register patient">non register patient</option>
</select>
</div>
<button type="submit" value="create_appointment">
ok
</button>
</form>
</div>
</body>
and the add_appointement3.html
<body>
{% if user_form.errors %} {% for field in user_form %} {% for error in field.errors %}
<div class="alert alert-danger">
<strong>{{ field.label }}</strong><span>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in user_form.non_field_errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}
<div class="container">
<form method="post">
{{ user_form.media }} {{ user_form.as_p }} {% csrf_token %}
<button type="submit" value="create_appointment">
ok
</button>
</form>
</div>
</body>
Upvotes: 0
Views: 95
Reputation: 2006
I believe this happens because you try to pass an additional keyword argument which the form is not supposed to handle (type). Can you try the following:
# in your second part
else:
request.POST._mutable = True
type = request.POST.pop("type")
request.POST._mutable = False
if request.method=='POST' and type == "register patient":
### rest of this part looks ok
elif request.method=='POST' and type == "non register patient":
### rest of this part looks ok
Upvotes: 1
Reputation:
I would recommend to write for every form a own view, checked the code and did adjust it a little, you can give it a try and share the responses:
@login_required
def create_appointement(request):
# Giving user a variable
user = User()
# Check if user is patient and if there is a post request
#POST REQUESTS
if request.user.is_patient() and if request.method=='POST':
#Retrieving data from form
form_appointement = AppointmentForm(request.POST or None)
#Validating data
if form_appointement.is_valid():
# manipulate data
form_app = form_appointement.save(commit=False)
form_app.user_ho_add = request.user
form_app.patient = request.user
start_time = form_app.start_time
future_time = dt.datetime(1970, 1, 1, start_time.hour, start_time.minute, start_time.second, start_time.microsecond) + timedelta(minutes=30)
form_app.end_time = dt.time(future_time.hour, future_time.minute, future_time.second, future_time.microsecond)
# Saving data
form_app.save()
#Returning message
messages.success(request, 'appointment added')
# if form is not valid
else:
#returning message
messages.error(request, 'Error')
return render(request,'appointement/add_appointement1.html',{'form':form_appointement})
# POST request with other kind of patient
elif request.method=='POST' and request.POST.get("type") == "register patient":
# Giving form a new variable and retrieving data
form_appointment_2 = AppointmentForm_2(request.POST or None)
#Getting second user
user2 = get_user_model()
#Getting patients out of user 2
patients = user2.objects.filter(type_of_user=TypeOfUser.PATIENT)
#Validating data form new for
if form_appointment_2.is_valid():
#manipulating form data
form_appointment_2.save(commit=False)
form_appointment_2.user_ho_add = request.user
form_appointment.end_time = form_appointment.start_time + timedelta(minutes=30)
start_time = form_appointment_2.start_time
future_time = dt.datetime(1970, 1, 1, start_time.hour, start_time.minute, start_time.second, start_time.microsecond) + timedelta(minutes=30)
form_appointment_2.end_time = dt.time(future_time.hour, future_time.minute, future_time.second, future_time.microsecond)
#Saving form
form_appointment_2.save()
#returning message
messages.success(request, 'appointment added')
else:
#returning message
messages.error(request, 'Error')
return render(request, 'appointement/add_appointement2.html', {'user_form':form_appointment_2,'patients':patients })
elif request.method=='POST' and request.POST.get("type") == "non register patient":
# Giving form a new variable and retrieving data
form_appointment = AppointmentForm_3(request.POST or None)
#Validating data form new for
if form_appointment.is_valid():
#manipulating form data
form_app = form_appointment.save(commit=False)
form_app.user_ho_add = request.user
form_appointment.end_time = form_appointment.start_time + timedelta(minutes=30)
start_time = form_appointment_2.start_time
future_time = dt.datetime(1970, 1, 1, start_time.hour, start_time.minute, start_time.second, start_time.microsecond) + timedelta(minutes=30)
form_appointment_2.end_time = dt.time(future_time.hour, future_time.minute, future_time.second, future_time.microsecond)
#Saving form
form_app.save()
#returning message
messages.success(request, 'appointment added')
else:
#returning message
messages.error(request, 'Error')
return render(request,'appointement/add_appointement3.html',{'user_form':form_appointment})
else:
return render(request, 'appointement/add_appointement_options.html', )
Upvotes: 1