title: how do i pre fill a form field with a models object in django
body: <p>i have two pages profile and add notes. i want to fill the patient field in the add notes form with the users first name whenever i click add notes on any users profile will i need to change the url path? if yes, how do i get the data from the url and fill it in the form.</p>
  <p>urls.py</p>
  <pre><code>urlpatterns = [
      path('', dashboard, name='dashboard'),
      path('profile/', profile, name='profile'),
      path('profile/&lt;str:slug&gt;/&lt;int:pk&gt;', profile, name='profilepk'),
      path('edit_profile/&lt;int:id&gt;/', edit_profile, name='editprofile'),
      path('addnotes/', addnotes, name='addnote'),
    
  ]
  </code></pre>
  <p>views.py</p>
  <pre><code>def addnotes(request):
      profile = Profile.objects.get(user_id=id)
      form = PatientNotesForm(request.POST or None)
      if form.is_valid():
          form.save()
          return redirect(f'/dashboard/profile/{user.profile.slug}/{user.pk}')
  
      return render(request, 'core/addnotes.html', {'form': form})
  
  def profile(request, slug, pk):
      profil = Profile.objects.get(slug=slug)
      profile = Profile.objects.get(pk=pk)
      context = {'profile': profile, 'profil': profil}
      return render(request, 'dashboard/profile.html', context)
  </code></pre>
  <p>models.py</p>
  <pre><code>class Note(models.Model):
      illness = models.CharField(max_length=1000, blank=True)
      patient = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='patientnote', null=True)
      Doctor = models.CharField(max_length=100, blank=True)
      Description = models.CharField(max_length=10000, blank=True)
      created = models.DateTimeField(auto_now_add=True)
  
  
      def __str__(self):
          return f&quot;{self.illness}&quot;
  </code></pre>
  <p>forms.py</p>
  <pre><code>class PatientNotesForm(ModelForm):
      illness = forms.CharField(max_length=100, help_text='First Name')
      patient = forms.CharField(max_length=100, help_text='Last Name')
      doctor = forms.CharField(max_length=100, help_text='address')
      description =  forms.CharField(max_length=100,widget=forms.Textarea)
  
      def __init__(self, *args, **kwargs):
          super().__init__(*args, **kwargs)
          self.fields['illness'].widget.attrs.update(
              {'placeholder': ('illness')})
          self.fields['doctor'].widget.attrs.update(
              {'placeholder': (&quot;Doctor's name&quot;)})
          self.fields['patient'].widget.attrs.update(
              {'placeholder': (&quot;Patient's name&quot;)})
          self.fields['description'].widget.attrs.update(
              {'placeholder': (&quot;Description&quot;)})
          self.fields['illness'].widget.attrs.update({'class': 'log'})
          self.fields['doctor'].widget.attrs.update({'class': 'log'})
          self.fields['patient'].widget.attrs.update({'class': 'log'})
          self.fields['description'].widget.attrs.update({'class': 'textarea'})
  
      class Meta:
          model = Note
          fields = ('illness', 'patient', 'doctor', 'description')
  </code></pre>
  <p>addnotes.html</p>
  <pre><code>   &lt;form method=&quot;post&quot; class=&quot;fum&quot;&gt; {%csrf_token%}{{form.illness}}{{form.patient}}{{form.doctor}}{{form.description}}
              &lt;li class=&quot;btnn &quot;&gt;&lt;button type=&quot;submit &quot; class=&quot;conf &quot;&gt;Confirm&lt;/button&gt;&lt;/li&gt;
          &lt;/form&gt;
  </code></pre>
  <p>profile.html</p>
  <pre><code>     &lt;ul class=&quot;main-menu&quot;&gt;
              &lt;li&gt;
                  &lt;a href=&quot;#&quot;&gt;&lt;img class=&quot;nav-items&quot; src=&quot;{% static 'images/home.svg'%}&quot; alt=&quot;&quot;&gt;&lt;span&gt;Home&lt;/span&gt;&lt;/a&gt;
              &lt;/li&gt;
              &lt;li&gt;
                  &lt;a href=&quot;#&quot;&gt;&lt;img class=&quot;nav-items&quot; src=&quot;{% static 'images/male.svg'%}&quot; alt=&quot;&quot;&gt;&lt;span&gt;Patients&lt;/span&gt;&lt;/a&gt;
              &lt;/li&gt;
              &lt;li&gt;
                  &lt;a href=&quot;#&quot;&gt;&lt;img class=&quot;nav-items&quot; src=&quot;{% static 'images/doctor.svg'%}&quot; alt=&quot;&quot;&gt;&lt;span&gt;Add notes&lt;/span&gt; &lt;/a&gt;
              &lt;/li&gt;
              &lt;li&gt;
                  &lt;a href=&quot;#&quot;&gt;&lt;img class=&quot;nav-items&quot; src=&quot;{% static 'images/lab.svg'%}&quot; alt=&quot;&quot;&gt;&lt;span&gt;Edit profile&lt;/span&gt;
                  &lt;/a&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
      &lt;/div&gt;
  </code></pre>