python view not working after deploying django app -
i attempting deploy new app using apache , mod-wsgi. app takes date provided in post request , plots graph , table specific time range. date appears set correctly using python internal (development) web server, fails when using apache/wsgi. issue appears view;
# set initial date custom_date = [1, 1, 2015] def getcustomdata(request): global custom_date form = dateform() if request.method == 'post': custom_date[0] = (str(request.post['custom_date_day'])) custom_date[1] = (str(request.post['custom_date_month'])) custom_date[2] = (str(request.post['custom_date_year'])) date_reformat = datetime.datetime.strptime(str(custom_date[0]+' '+custom_date[1]+' '+custom_date[2]), '%d %m %y') chart_date = date_reformat.strftime('%-d %b %y') return render(request, 'custom_results.html', {'cdate': chart_date}) else: date_reformat = datetime.datetime.strptime(str(custom_date[0]+' '+custom_date[1]+' '+custom_date[2]), '%d %m %y') chart_date = date_reformat.strftime('%-d %b %y') return render(request, 'custom_results.html', {'cdate': chart_date})
when using apache, initial post request works fine, when attempting navigate page 2 of table, "typeerror: cannot concatenate 'str' , 'int' objects" line following "else" statement. assuming way global data variable gets updated between apache , development server different?
you must never use mutable global variables store data this. variable same users, means 1 user able change display format subsequent users. data should stored in session.
however, cause of problem simpler; default data composed of integers, stored data strings. should use string interpolation rather concatenating, here should store single string anyway.
also note last 3 lines of if block same else block; can simplify view considerably.
def getcustomdata(request): if request.method == 'post': request.session['custom_date'] = ' '.join( request.post['custom_date_day'], request.post['custom_date_month'], request.post['custom_date_year'] ) custom_date = request.session.setdefault('custom_date', '1 1 2015') date_reformat = datetime.datetime.strptime(custom_date, '%d %m %y') chart_date = date_reformat.strftime('%-d %b %y') return render(request, 'custom_results.html', {'cdate': chart_date})
Comments
Post a Comment