i'm a beginner in Django and i have a problem …. I created an application(name :PQR) with 3 models (Patient / Question / Reply) I want to see a views with the name of the patient at the top, under this, all questions availables and if applicable, the answers next to the question (some patient may have empty reply / no reply). I know how to display the answers for a patient but I would like to display EVERY questions and if available, I want to provide the reply but I don't know how to write this in my view?
This is my model :
Class Patient(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
def listReply(self):
replies = Reply.objects.filter(patient= self.id)
return replies
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question_text
class Reply(models.Model):
question = models.ForeignKey(Question)
patient = models.ForeignKey(Patient)
reply_text = models.CharField(max_length=200)
def __unicode__(self):
return str(self.reply_text)
This is my view :
from django.shortcuts import render, get_object_or_404
from django.template import RequestContext
from .models import Patient, Question, Reply
def list(request):
list_patient = Patient.objects.order_by('name') context = RequestContext(request, {'list_patient':list_patient,'welcome': 'Welcome on my page Django !',
})
return render(request, 'PQR/list.html', context)
def detail(request, patient_id):
patient = get_object_or_404(Patient, pk=patient_id)
list_question = Question.objects.all()
Replies4MyPatient = Reply.objects.filter(patient=patient_id)
return render(request, 'PQR/detail.html', locals())
And this is my urls :
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.list, name ='list_patient'),
url(r'^(?P<patient_id>[0-9]+)/patient/$', views.detail, name ='detail_patient'),
And this is my template :
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'Forum/style.css' %}" />
<div>
<h1>Patient details : {{ patient }}</h1>
</div>
<fieldset>
{% csrf_token %}
{% for reply in Replies4MyPatient %}
</br><abcd>{{ reply.question }}</abcd></br>
</br>
<li>{{ reply }}</li>
{% endfor %}
</fieldset></br>
<a href="{% url 'list_patient' %}"/> <input type="button" value="Look Back"/>
Thanks you for your help !
The function
render
builds the html to return with a template and a context. In your templatePQR/detail.html
, you use{{ patient }}
andReplies4MyPatient
. You need to build a context with these values and to pass it torender