[Answered ]-Context Variable will not show up in Django template

2๐Ÿ‘

You need to include the cleaned data in the template context. However, cd is only defined when the form is valid, so you can do something like:

data = {
    'post': post,
    'form': form,
    'sent': sent,
}

if sent:
    data['cd'] = cd
return render(request, 'blog/post/share.html', data)

Since you donโ€™t need the entire cd dictionary in the template, another option would be to pass only the variable you need, cd['to'].

data = {
    'post': post,
    'form': form,
    'sent': sent,
}

if sent:
    data['sent_to'] = cd['to']
return render(request, 'blog/post/share.html', data)

Then in your template, you would use {{ sent_to }} instead of {{ cd.to }}

0๐Ÿ‘

cd is only used within your view, but it is not a context variable.

To make it a context variable and accessible in the template you have to add it to your existing context variables:

return render(request, 'blog/post/share.html', {'post': post,
                                            'form': form,
                                            'sent': sent,
                                            'cd': cd})

0๐Ÿ‘

You need to add to or cd to your context:

return render(request, 'blog/post/share.html', {'post': post,
                                                'form': form,
                                                'sent': sent})

For example:

{'cd': cd,
 'post': post,
 'form': form,
  'sent': sent}

Leave a comment