3

When I render the form in HTML, I use this view. the patient_id is used to denote what patient the check in is for and for name display and such.

def Checkin(request, patient_id):
    patient = get_object_or_404(PatientInfo, pk=patient_id)
    form = forms.PatientCheckinForm()
    return render(request, 'patientRecords/checkin.html', {'patient': patient, 'form':form})

When I submit the patient form filled out as a POST method, I still need access to the patient_id. Currently this is the view that accepts the filled form:

def CheckinSubmit(request):
if request.method == 'POST':
    form = forms.PatientCheckinForm(request.POST, request.FILES)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.date_time_of_checkin = dt.now()
        instance.patient = patient.patient_id
        instance.save()
        return redirect('patientRecords/index.html')

I want to set the instance.patient to the patient_id that was part of patient from the Checkin view. Is there a way to pass the patient data back along with the POST method or is there another way this can be done?

For reference, here is my template and I am using ModelForm not form.

{% block content %}

<div class="container">
  <h1>Patient Checkin</h1>
  <h2>{{patient.first_name}} {{patient.last_name}}</h2>
</div>
<div class="container">
  <form  action="{% url 'patientRecords:checkinsubmit' %}" method="POST" class="form">
    {% csrf_token %}
    {% bootstrap_form form %}
    {% buttons %}
        <button type="submit" class="btn btn-primary">Submit</button>
    {% endbuttons %}
  </form>
</div>

{% endblock %}

Thanks in advance!

2 Answers 2

9

You should be able to simply add a hidden input to your form to capture the patient ID:

{% block content %}

<div class="container">
  <h1>Patient Checkin</h1>
  <h2>{{patient.first_name}} {{patient.last_name}}</h2>
</div>
<div class="container">
  <form  action="{% url 'patientRecords:checkinsubmit' %}" method="POST" class="form">
    <input type="hidden" name="patient_id" value="{{patient.patient_id}}" />
    {% csrf_token %}
    {% bootstrap_form form %}
    {% buttons %}
        <button type="submit" class="btn btn-primary">Submit</button>
    {% endbuttons %}
  </form>
</div>

{% endblock %}

(Note this assumes that the patient ID is accessible from the patient_id property of the patient object.)

Then, in your CheckinSubmit method, you can access this value via request.POST.get('patient_id')

Alternatively, it appears that your check in form loads with the patient ID in the URL. In your CheckinSubmit method, you should be able to access this URL through the request.META.HTTP_REFERER property. You could then parse the URL (e.g., using request.META.HTTP_REFERER.split('/')[len(request.META.HTTP_REFERER.split('/')) - 1] to pull out the patient ID.

Sign up to request clarification or add additional context in comments.

2 Comments

Getting closer! different error now. I'm getting "ValueError: Cannot assign "'15'": "CheckIn.patient_id" must be a "PatientInfo" instance" error. 'PatientInfo' is the name of a different model than the one I am currently trying to save to. the 15 is going into a 'checkin' model as a foreign key to 'PatientInfo'.
Judging by the error, the patient property is expecting an object (of type PatientInfo), not an integer/ID. Therefore, in your CheckinSubmit method, you'll need to pull that object from the DB using the ID to look it up, then assign that to instance.patient. For example, assuming you're importing your app models into your views file, you might pull it in, and assign it to instance.patient, like so: instance.patient = models.PatientInfo.objects.get(pk=request.POST.get('patient_id'))
1

Example

<form method="post" action = "{% url 'user_search_from_group' %}"> 
      <div class="module-option clearfix">
           <div class="input-append pull-left">
                <input type="hidden" name="groupname" value="{{ gpname }}" />
                {% csrf_token %}
                <input type="text" class="span3" placeholder="Filter by name" id="username3" name="username3" required>
                 <button type="submit" class="btn" name="submit">
                         <i class="icon-search"></i>
                 </button>
           </div>
      </div>
</form>

Here a hidden field is used to pass a value along form.

def user_search_from_group(request):
    if request.method == 'POST':
        username3 = request.POST.get('username3')
        gname = request.POST.get('groupname')

Using request we are use the value inside view

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.