1👍
✅
You can set the code
variable in the session. Then access it using request.session.code
in your template.
def home(request):
if request.method == 'POST':
form = New_mini_url_form(request.POST)
if form.is_valid():
if (MiniUrl.objects.filter(url=form.cleaned_data['url']).count() == 0):
code = form.cleaned_data['code']
request.session['code'] = code # set code in the session
[... stuff ...]
return HttpResponseRedirect(reverse(home))
else:
code = form.cleaned_data['code']
request.session['code'] = code # set code in the session
[... stuff ...]
return HttpResponseRedirect(reverse(home))
else:
return render(request, 'mini_url/home.html', locals())
else:
form = New_mini_url_form()
return render(request, 'mini_url/home.html', locals())
Then in your template, you can access code
by:
{{request.session.code}} # display code in the template
You must include django.core.context_processors.request
in the settings.
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.request', # will pass request in the template
...
)
From Django 1.7 docs on request context processor:
django.core.context_processors.request
If TEMPLATE_CONTEXT_PROCESSORS contains this processor, every
RequestContext will contain a variable request, which is the current
HttpRequest. Note that this processor is not enabled by default;
you’ll have to activate it.
Source:stackexchange.com