I am building an application where an API searches for some information you inputted into a form. When I click on refresh there is an annoying popup in Chrome asking to resent the information. After some time googeling I found out that you have to use HttpResponseRedirect to avoid this. Unfortunately, it kills all the variables and I cannot derive any of them. Additionally, I had to instantiate the local variables to avoid errors where putting then into the “context”, as the variables were not yet defined without pushing the “Submit” button in the form. The instantiation also will kill the variables. Also tried to use global variables, but then the data can be seen across users. Now I have no ideas left. It would be nice to retain the information in the variables.
Code:
def realestate(request):
context = {}
json = {}
coordinates = {}
pages = 0
rent = 0
address_save = ""
radius_save = ""
address = ""
if request.method == 'POST':
address = request.POST.get('address')
radius = request.POST.get('radius')
# call the api only if adress or radius changed
if address != address_save or radius != radius_save:
pageNumber = request.POST.get('pageNumber')
# receive coordinates from goodle
coordinates = googleGeoApi(address)
# search real estate with coordinates from immoscout24
json, pages = search_api(pageNumber, coordinates, radius)
# calculate rent with search from immoscout24
rent = calculateRent(coordinates, radius)
address_save = address
radius_save = radius
return HttpResponseRedirect(request.path_info)
context = {'json': json, 'coordinates': coordinates, 'pages': pages, "rent": rent, "address": address}
return render(request=request, template_name='realestate/realestate.html', context = context)