open another page from html button with the same pk value

hi all,
I have three concatenated pages
1)

path('admin-view-patient', patientView.admin_view_patient_view,name='admin-view-patient'),
path('update-patient/<int:pk>', patientView.update_patient_view,name='update-patient'),
path('add-patient-note/<int:pk>', patientView.add_patient_note_view,name='add-patient-note'),

the first html file contain

<div class="container">
  <div class="panel panel-primary">
    <div class="panel-heading">
      <h6 class="panel-title">Patient</h6>
    </div>
    <table class="table table-hover" id="dev-table">
      <thead>
        <tr>
          <th>Name</th>
          <th>Address</th>
          <th>Update</th>
        </tr>
      </thead>
      {% for p in patients %}
      <tr>
        <td> {{p.get_name}}</td>
        <td>{{p.address}}</td>
        <td><a class="btn btn-primary btn-xs" href="{% url 'update-patient' p.id  %}"><span class="glyphicon glyphicon-edit"></span></a></td>
      </tr>
      {% endfor %}
    </table>
  </div>
</div>

clicking on UPDATE-PATIENT it open second page with link http://127.0.0.1:8000/update-patient/1

<form method="post" enctype="multipart/form-data">
  {% csrf_token %}
  <div class="container register-form">
    <div class="form">
      <div class="note">
        <p>Update Patient Details</p>
      </div>
      <div class="form-content">
        <div class="row">
          <div class="col-md-6">
            <div class="form-group">
              {% render_field userForm.first_name class="form-control" placeholder="First Name" %}
            </div>
            <div class="form-group">
              {% render_field patientForm.address class="form-control" placeholder="Address" %}
            </div>
          </div>
        </div>
        <button type="submit" class="btnSubmit">Update</button>
      </div>
      <a href="{% url 'add-patient-note' p.id  %}">
        <h6 class="m-b-20">Admit Patient</h6>
      </a>
    </div>
  </div>
</form>

clicking on add-patient-note ADD-PATIENT-NOTE i would like open page with the same patient ID in order to create the note with the foreignkey patient to who associate the note.

the note models.py has the patientNotes:

class PatientsNotes(models.Model):
    patient = models.ForeignKey(Patient,on_delete=models.CASCADE)
    title = models.CharField(max_length=20,default=None)
    body = models.TextField(default=None)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    active = models.BooleanField(default=True)
    
    class Meta:
        ordering = ['created']
        indexes = [
                models.Index(fields=['created']), 
                ]
    def __str__(self):
        return f'Comment by {self.name} on {self.post}'

and forms.py have the note form

class PatientNotesForm(forms.ModelForm):
    class Meta:
        model = models.PatientsNotes
        fields = ['title', 'body']

it is possible?
many thanks in advance

Yes.

What is the problem you are facing when trying to do this?

I don’t know how to pass current pk from page to page. I don’t know basically how to do that. I’m sorry :frowning:

You’re doing it here:

and here:

I obtain following error. maybe the id number passed when I open update-patient-view is not passed to add-patient-note page clicking on a button in the update-patient-view webpage

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/update-patient/1

Django Version: 5.1.4
Python Version: 3.12.8
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'hospital',
 'widget_tweaks']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template D:\hospitalmanagement-django5\templates\hospital\admin_update_patient.html, error at line 94
   Reverse for 'add-patient-note' with arguments '('',)' not found. 1 pattern(s) tried: ['add\\-patient\\-note/(?P<pk>[0-9]+)\\Z']
   84 :             <div class="form-group">
   85 :               {% render_field patientForm.mobile class="form-control" placeholder="Mobile" %}
   86 :             </div>
   87 :             <div class="form-group">
   88 :               {% render_field patientForm.assignedDoctorId class="form-control" placeholder="Doctor" %}
   89 :             </div>
   90 :           </div>
   91 :         </div>
   92 :         <button type="submit" class="btnSubmit">Update</button>
   93 :       </div>
   94 :       <a href=" {% url 'add-patient-note' p.id  %} ">
   95 :         <h6 class="m-b-20">Admit Patient</h6>
   96 :       </a>
   97 :     </div>
   98 :   </div>
   99 : </form>
   100 : 
   101 : 
   102 : {% endblock content %}
   103 : 

Traceback (most recent call last):
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\contrib\auth\decorators.py", line 60, in _view_wrapper
    return view_func(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\contrib\auth\decorators.py", line 60, in _view_wrapper
    return view_func(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\hospitalmanagement-django5\hospital\views\patientView.py", line 71, in update_patient_view
    return render(request,'hospital/admin_update_patient.html',context=mydict)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\shortcuts.py", line 25, in render
    content = loader.render_to_string(template_name, context, request, using=using)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\template\backends\django.py", line 107, in render
    return self.template.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\template\base.py", line 1008, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\template\base.py", line 969, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\template\loader_tags.py", line 159, in render
    return compiled_parent._render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\template\base.py", line 1008, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\template\base.py", line 969, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\template\loader_tags.py", line 65, in render
    result = block.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\template\base.py", line 1008, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\template\base.py", line 969, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\template\defaulttags.py", line 480, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\urls\base.py", line 88, in reverse
    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\deniS.sangaletti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\urls\resolvers.py", line 831, in _reverse_with_prefix
    raise NoReverseMatch(msg)
    ^^^^^^^^^^^^^^^^^^^^^^^^^

Exception Type: NoReverseMatch at /update-patient/1
Exception Value: Reverse for 'add-patient-note' with arguments '('',)' not found. 1 pattern(s) tried: ['add\\-patient\\-note/(?P<pk>[0-9]+)\\Z']

You’re trying to render a value here referenced as p.id. That means that you would have to pass some object into your context, with the key in the context of p and having an attribute named id.

Are you doing that?

Data might be supplied to a view through url segments or form data, but it’s always supplied to the template through the context. Everything being rendered must be put into the context.