title: Getting a foreign key into a form
body: <p>I'm new to Django and Python, and have spent a couple days trying to solve this problem. I've tried several approaches in other threads here, but I think I'm missing some basic concept because none of them have worked for me. Here's what I'm trying to do:</p>
  
  <p>I'm using ModelForm, and I have created a form for adding records that includes a foreign key. I want this value to be set by default to match the record that was displayed on the page I linked from. [Code shown below edited to show suggested corrections].</p>
  
  <p>Here's the relevant code from views.py:</p>
  
  <pre><code>def add_sighting(request):
  unit = request.GET.get('unit')
  form = SightingForm(request.POST or None, initial={'unit':unit})
  if form.is_valid():
      sighting = form.save(commit=False)
      sighting.save()
      return redirect(index)
  return render_to_response('trainapp/add_sighting_form.html',
                            {'unit': unit, 'sighting_form': form},
                            context_instance=RequestContext(request))
  </code></pre>
  
  <p>Here's the relevant code from add_sighting_form.html:</p>
  
  <pre><code>&lt;form action="/trainapp/add_sighting/" method="post"&gt;
  {% csrf_token %}
  &lt;table&gt;
  {{ sighting_form.as_table }}
  &lt;/table&gt;
  &lt;input type="submit" value="Save" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Here's the relevant code from the template I linked from:</p>
  
  <pre><code>&lt;p&gt;&lt;a href="/trainapp/add_sighting/?unit={{ unit.id }}"&gt;Add sighting&lt;/a&gt;&lt;/p&gt;
  </code></pre>