Recover the data already recorded in my Django admin space in my form

154 Views Asked by At

I want to know how to retrieve answers already made in my text fields because there is no ways to retrieve the answers already registered!

This is my models :

class Patient(models.Model):
    name = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name + ' [' + str(self.id) + ']'


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 self.reply_text

This is my template (formDynamic.html) :

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'Forum/style.css' %}" />
<div>
    <h1>Patient details : {{ patient }}</h1>    
</div>
form-included-into-question-template --> 
<form action="success" method="post">

{% csrf_token %}
<fieldset>
{% block content %}
    {% for question in questions %}
        <h2><abcd>{{ question }}</abcd>  <h6>{{ question.pub_date }}</h6></h2>

            {% for reply in form %}
                {{ form }}
            {% endfor %}
    {% endfor %}
{% endblock %}
</fieldset>
<input type="submit" value="Submit"/>
</form>
<a href="{% url 'list_patient' %}"/> <input type="button" value="Look Back"/>

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.save_reply, name ='detail_patient'),
    #url(r'^(?P<patient_id>[0-9]+)/patient/success$', 'success', name ='success'),
 ]

This is my forms.py :

from django.forms import ModelForm
from django import forms
from .models import Reply 

class ReplyForm(ModelForm):
    class Meta:
        model = Reply
        fields = ['reply_text']

This is my views.py :

def save_reply(request, patient_id):
        patient = get_object_or_404(Patient, pk=patient_id)
        questions = Question.objects.all()
        if request.method == 'POST':
            form = Replyform(request.POST)
            if form.is_valid():
                new_obj = form.save(commit=False)
                new_obj.creator = request.user
                u = Question.objects.get(pk=id)
                new_obj.reply_to = u
                new_obj.save()
                return HttpResponseRedirect('/success')
        else:
            form = ReplyForm()

        return render_to_response('PQR/formDynamic.html', {'form': form, 'questions': questions, 'patient': patient,}, context_instance=RequestContext(request))

    def success(request):
        return HttpResponse ('<p>Success</p>')

I can not retrieve the answers already registered and run my post method to save my answers and return me the URL /success. My post method does not work as well as my redirect to my url / success. Thanks you for your help !

1

There are 1 best solutions below

1
On

I don't understand your question, but the reason you can't submit your form is that it is posting to a non-existent destination: the action attribute in the form HTML tag needs to be an actual URL. If you want to use a view name then do so via the {% url %} tag:

<form action="{% url "success" patient_id=patient.id %}" method="post">