title: Django: Saving a URL request parameter Into Form
body: <p>I'm Using a Form to save data into it. </p>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>class presciptiontemplates(models.Model):
      templateid = models.AutoField(primary_key=True)
      template = tinymce_models.HTMLField()
      draft = models.BooleanField(default=False)
      savedate = models.DateTimeField(default=datetime.now())
      patientid = models.ForeignKey('Patient')
  </code></pre>
  
  <p>I'm Passing the parameter Patient Id From the URL. </p>
  
  <pre><code>url(r'^addprescription/(?P&lt;patid&gt;\d+)/$', views.viewtemplate
  </code></pre>
  
  <p>How do I save this parameter into form </p>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>def viewtemplate(request, patid):
      userid = patid
      form = templateform(request.POST)
      if request.method == "POST":
          if form.is_valid():
              presciptiontemplates.patientid = userid
              form.save()
          return redirect('index')
      else:
          form = templateform()
      return render(request, 'presapp/prescription.html', {'form': form})
  </code></pre>
  
  <p>forms.py </p>
  
  <pre><code>class templateform(forms.ModelForm):
  class Meta:
      model = presciptiontemplates
      fields = ['template', 'draft']
      widgets = {
      'template': TinyMCE(),
      'draft': forms.CheckboxInput()
      }
      labels = {
          "patientid": _("Patient ID"),
          }
      def __init__(self, *args, **kwargs):
          super(ThatForm, self).__init__(*args, **kwargs)
          self.fields['template'].required = True
  </code></pre>
  
  <p>This gives me an error </p>
  
  <blockquote>
    <p>[Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot insert
    the value NULL into column 'patientid'</p>
  </blockquote>
  
  <pre><code>Django  1.10.4
  Python  3.5.2
  Windows 7 
  </code></pre>