Reputation: 119
Once I submit my form in my home view we are redirected to my download view which has a false response for my session var... why??? I am unsure how to fix this issue and it feels like I've tried everything.
@login_required
def Home(request):
form = TimeForm()
search_times = []
if request.method == "POST":
# clear session data
# for key in request.session.keys():
# test.append(request.session[key])
start = request.POST['start_time']
end = request.POST['end_time']
start = time.mktime(datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S").timetuple())
end = time.mktime(datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S").timetuple())
start = re.split('.[0-9]+$', str(start))[0]
end = re.split('.[0-9]+$', str(end))[0]
search_times.append(int(start))
search_times.append(int(end))
request.session['search_times'] = search_times
request.session.save()
context = {
'form': form,
'search_times': search_times,
}
return render(request, "pcap_app/home.html", context)
def Download(request, path):
test = []
access_time = []
search_times = []
search_times = request.session.get('search_times', False)
# testing timestamp directory listing
files = sorted_ls(os.path.join(settings.MEDIA_ROOT, path))
for f in files:
time = os.stat(os.path.join(settings.MEDIA_ROOT, f)).st_mtime
access_time.append(int(re.split('.[0-9]+$', str(time))[0]))
context = {
'sort': files,
'times': access_time,
'search': search_times,
'test': test,
}
return render(request, "pcap_app/downloads.html", context)
home.html
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="{{ STATIC_URL }}css/bootstrap.css" rel="stylesheet" type="text/css"/>
<script src="{{ STATIC_URL }}js/bootstrap.js"></script>
{{ form.media }}
</head>
<body>
<h1>pcap search</h1>
{% if user.is_authenticated %}
<form action="/download/" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input id="submit" type="submit" value="Submit">
</form>
{{ search_times }}
{{ test }}
<a href="/logout/">Logout</a>
{% else %}
<a href="/login/?next={{ request.path }}">Login</a>
{% endif %}
</body>
Upvotes: 1
Views: 759
Reputation: 47906
You have wrongly set the action
attribute in your home.html
template as /download/
.
When you submit the form, the POST
request is submitted to the /download/
view and not the home
view. So, the code for setting the session is never executed.
You need to change the action
attribute to your home
view url. When you do this, in case of POST
requests, the variable search_times
will be set in the session.
<form action="/home/url/" method="POST">
After specifying the action
attribute in home.html
, you will need to redirect to /download/
in home
view. Then after the redirection happens to your download
view, you will be access the search_times
vaiable in the session.
@login_required
def Home(request):
form = TimeForm()
search_times = []
if request.method == "POST":
...
request.session['search_times'] = search_times # set the variable in the session
return HttpResponseRedirect('/download/') # redirect to download page
context = {
'form': form,
'search_times': search_times,
}
return render(request, "pcap_app/home.html", context)
Upvotes: 1