title: set initial value in CreateView from ForeignKey (non-self.request.user)
body: <p>I am attempting to access ForeignKeys in Class Based Views CreateView. I would like to be able to dynamically set initial values in CBV from ForeignKeys and also dynamically set template links from ForeignKeys.</p>
  
  <p>These two questions (1. initial values, 2. template links) may be solved in similar methods, or perhaps by different methods... I'm still learning. Perhaps the first question can be solved within <code>views.py</code> and the second question can be solved with template syntax in <code>ingredient_form.html</code>?</p>
  
  <p>I've seen questions on SO setting initial values from users (<code>self.request.user</code>), but not from just a normal foreign key in <code>models.py</code>.</p>
  
  <p>I'm going through <a href="https://django-by-errors.readthedocs.org/en/latest/" rel="noreferrer">django-by-errors</a>, and attempting to add extra features to expand my django knowledge.</p>
  
  <p>My question specifically centers on <code>views.py:IngredientAddView(CreateView)</code>, on 
  <code>ingredient_form.html</code>, and on <code>urls.py:'recipe-detail'</code> &amp; <code>'ingredient-add</code>'.</p>
  
  <p>When I view a <code>'recipe-detail'</code>, I can click a link to <code>'ingredient-add'</code>. I would like <code>'ingredient-add'</code> to "know" which recipe clicked to it, and be able to set this recipe as the initial value (my attempt within <code>views.py:IngredientAddView:get_initials(self)</code> does not work), and also be able to link back to this recipe (my attempt within <code>ingredient_form.html:{% comment %}</code> does not work).</p>
  
  <p>Would appreciate any assistance.</p>
  
  <p>models.py</p>
  
  <pre class="lang-py prettyprint-override"><code>class Food(models.Model):
      name=models.CharField(max_length=20,unique=True)
  
      def __str__(self):
          return self.name
  
      def get_absolute_url(self):
          return reverse('food-detail',kwargs={'pk':self.pk})
  
  class Recipe(models.Model):
      title=models.CharField(max_length=80,unique=True)
      slug=models.SlugField(max_length=80,unique=True)
      description=models.TextField(blank=True)
  
      def __str__(self):
          return self.title
  
      def get_absolute_url(self):
          return reverse('recipe-detail',kwargs={'slug':self.slug})
  
  class Ingredient(models.Model):
      recipe=models.ForeignKey(Recipe)
      food=models.ForeignKey(Food)
  
      def __str__(self):
          return '%s (%s)' % (self.food, self.recipe)
  </code></pre>
  
  <p>views.py</p>
  
  <pre class="lang-py prettyprint-override"><code>class FoodListView(ListView):
      model=Food
  
  class FoodDetailView(DetailView):
      model=Food
  
  class FoodCreateView(CreateView):
      model=Food
  
  class RecipeListView(ListView):
      model=Recipe
  
  class RecipeDetailView(DetailView):
      model=Recipe
  
  class RecipeCreateView(CreateView):
      model=Recipe
  
  class RecipeUpdateView(UpdateView):
      model=Recipe
  
  class IngredientAddView(CreateView):
      model=Ingredient
  
  #    def get_context_data(self,**kwargs):
  #        context=super(IngredientAddView,self).get_context_data(**kwargs)
  #        context['foreign']=self.request.session.get('slug')
  
      def get_initials(self):
          return {
              'recipe':self.request.session.get('recipe')
          }
  </code></pre>
  
  <p>urls.py</p>
  
  <pre class="lang-py prettyprint-override"><code>from .views import FoodListView, FoodDetailView, FoodCreateView, RecipeListView, RecipeDetailView, RecipeCreateView, RecipeUpdateView, IngredientAddView
  
  urlpatterns=patterns('',
                       url(r'^$',RecipeListView.as_view(),name='recipe-list'),
                       url(r'^(?P&lt;slug&gt;[-\w]+)$',RecipeDetailView.as_view(),name='recipe-detail'),
                       url(r'^(?P&lt;slug&gt;[-\w]+)/edit$',RecipeUpdateView.as_view(),name='recipe-edit'),
                       url(r'^(?P&lt;slug&gt;[-\w]+)/add_ingredient/$',IngredientAddView.as_view(),name='ingredient-add'),
                       url(r'^new/$',RecipeCreateView.as_view(),name='recipe-create'),
                       url(r'^food/$',FoodListView.as_view(),name='food-list'),
                       url(r'^food/(?P&lt;pk&gt;[\d]+)$',FoodDetailView.as_view(),name='food-detail'),
                       url(r'^food/create/$',FoodCreateView.as_view(),name='food-create'),
                   )
  </code></pre>
  
  <p>recipe_detail.html</p>
  
  <pre class="lang-html prettyprint-override"><code>{% extends "base_food.html" %}
  
  {% block title %}{{ recipe }} {% endblock %}
  
  {% block content %}
    &lt;h1&gt;{{ recipe }}&lt;/h1&gt;
    &lt;p&gt;{{ recipe.id }}&lt;/p&gt;
    &lt;p&gt;{{ recipe.title }}&lt;/p&gt;
  
    &lt;br&gt;
      &lt;h2&gt;Description&lt;/h2&gt;
    &lt;p&gt;{{ recipe.description|default:'No description' }}&lt;/p&gt;
  
    &lt;h2&gt;Ingredients&lt;/h2&gt;
    &lt;ul&gt;
      {% for ingredient in recipe.ingredient_set.all %}
        &lt;li&gt;{{ ingredient }}&lt;/li&gt;
    {% endfor %}
    &lt;/ul&gt;
    &lt;p&gt;&lt;a href="{% url 'ingredient-add' recipe.slug %}"&gt;Add ingredient&lt;/a&gt;&lt;/p&gt;
    &lt;p&gt;&lt;a href="{% url 'recipe-edit' recipe.slug %}"&gt;Edit recipe&lt;/a&gt;&lt;/p&gt;
    &lt;p&gt;&lt;a href="{% url 'recipe-list' %}"&gt;Back to recipe list&lt;/a&gt;&lt;/p&gt;
  {% endblock %}
  </code></pre>
  
  <p>ingredient_form.html</p>
  
  <pre class="lang-html prettyprint-override"><code>{% extends "base_food.html" %}
  
  {% block title %}Add Ingredient{% endblock %}
  
  {% block content %}
    &lt;h1&gt;Add Ingredient&lt;/h1&gt;
    &lt;form method="POST"&gt;{% csrf_token %}
      {{ form }}
      &lt;button type="submit" class="btn btn-primary"&gt;Save&lt;/button&gt;
    &lt;/form&gt;
  
  {%comment%}  &lt;p&gt;&lt;a href="{% url 'recipe-detail' recipe.slug %}"&gt;Back to detail&lt;/a&gt;&lt;/p&gt; {%endcomment%}
    &lt;p&gt;&lt;a href="{% url 'recipe-list' %}"&gt;Back to recipe list&lt;/a&gt;&lt;/p&gt;
  {% endblock %}
  </code></pre>