Представление ___ не возвращало объект HttpResponse. Вместо этого он вернулся - PullRequest
0 голосов
/ 11 апреля 2019

У меня есть приложение для планирования, где пациенты могут записаться на прием. Когда я отправляю форму для новой встречи, я получаю ошибку значения.

views.py

def patient_portal(request):
    appointments = Appointment.objects.filter(patient=request.user.patient.pid)
    data_input = request.GET.get('date')
    selected_date = Appointment.objects.filter(date = data_input).values_list('timeslot', flat=True)
    available_appointments = [(value, time) for value, time in Appointment.TIMESLOT_LIST if value not in selected_date]
    doctor =  Patient.objects.get(doctor=request.user.patient.doctor).doctor
    print(doctor)

    if request.method == 'POST':
        form = AppointmentForm(initial={'doctor': doctor,'patient': request.user.patient}, instance=request.user.patient)
        if form.is_valid():
            form.save()
            return redirect('../home/')
    else:
        form = AppointmentForm(initial={'doctor': doctor,'patient': request.user.patient}, instance=request.user.patient)
        return render(request, 'scheduling/patient.html', {"form" : form, "appointments" : appointments, "available_appointments" : available_appointments, "data_input": data_input, "doctor": doctor})

patient.html:

<form method="post" action="" id="timeslot" enctype="multipart/form-data">
     {% csrf_token %}
     {{ form|crispy }}
     <button type="submit" class="btn btn-primary">Submit</button>
</form>

forms.py:

class AppointmentForm(forms.ModelForm):
    class Meta:
        model = Appointment
        fields = ('doctor','patient','date','timeslot') 

1 Ответ

3 голосов
/ 11 апреля 2019

Ваше представление всегда должно возвращать HTTP-ответ.На данный момент ваш код не обрабатывает случай, когда request.method == 'POST', но форма недействительна.

Вы можете исправить свой код, отменяя отступ в последнем выражении return, чтобы переместить его за пределыблок else:

def patient_portal(request):
    ...
    if request.method == 'POST':
        form = AppointmentForm(initial={'doctor': doctor,'patient': request.user.patient}, instance=request.user.patient)
        if form.is_valid():
            form.save()
            return redirect('../home/')
    else:
        form = AppointmentForm(initial={'doctor': doctor,'patient': request.user.patient}, instance=request.user.patient)
    return render(request, 'scheduling/patient.html', {"form" : form, "appointments" : appointments, "available_appointments" : available_appointments, "data_input": data_input, "doctor": doctor})
...