Django: How to put existing fields when displaying form for UpdateView?
<p>I'm making an episode tracker website and when I want the user to edit a show, the form provided starts with empty fields. How do I fill the form with already existing fields? For example, when a user is watching a show and is originally at episode 5, how do I call the update form with the Episode field already at 5 instead of it being empty?</p>

<p><strong>views.py</strong></p>

<pre><code>class ShowUpdate(UpdateView):
    model = Show
    slug_field = 'title'
    slug_url_kwarg = 'show'
    fields = ['description', 'season', 'episode']
</code></pre>

<p><strong>show-detail.html</strong></p>

<pre><code>&lt;form action="{% url 'show:show-update' show=show.title %}" method="post"&gt;
{% csrf_token %}
&lt;button type="submit"&gt;Edit&lt;/button&gt;
&lt;/form&gt;
</code></pre>

<p>You can fill a form with the values in an UpdateView with</p>

<pre><code>def get_initial(self):
    return { 'field1': 'something', 'field2': 'more stuff' }
</code></pre>

<p>Also the UpdateView inherits from the SingleObjectMixin, which provides <code>get_object</code> and <code>get_queryset</code>, so you could use either to get the object data you want to populate the form with. Something like:</p>

<pre><code>def get_object(self):
        return Show.objects.get(pk=self.request.GET.get('pk')) 
</code></pre>

<p><a href="https://docs.djangoproject.com/en/1.10/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_object" rel="nofollow">Check out the docs for more info</a></p>

<p>Also, if you have a pk or slug as a parameter in your url, it should pick it up as well. Such as:</p>

<pre><code>url(r'^shows/update/(?P&lt;pk&gt;\d+)/$',ShowUpdate.as_view())
</code></pre>

python django