Home > Enterprise >  Django HttpResponseRedirect() function
Django HttpResponseRedirect() function

Time:09-20

flag = 0
folder_id = 0
if FolderEntries.objects.filter(entry = entry_id).exists():
   print("=======================yes in folder")
   flag = 1
   folder_object = FolderEntries.objects.get(entry = entry_id)
   folder_entries_object = model_to_dict(folder_object)
            
   print("-------------------------", folder_entries_object)
   folder_id = folder_entries_object['folder']
else:
   flag = 0
        
        
if flag == 1:
           
   returnHttpResponseRedirect('folder/' str(folder_id) '/') 
else:
   if request.session.get('HTTP_REFERER', None):
       HTTP_REFERER = request.session.get('HTTP_REFERER')
       del request.session['HTTP_REFERER']
       return HttpResponseRedirect(HTTP_REFERER)
   return HttpResponseRedirect('editentry/' str(entry_id))

In this case if flag = 1, this is redirecting me to 2 urls these are:- http://127.0.0.1:8000/admin/form-entries/edit-entry/folder/174/

url "folder/174" is url that i need. But i am getting unwanted url "admin/form-entries/edit-entry/"
What i am doing wrong please guide me. I am a entry level django developer

Thanks and Regards Abhishek

CodePudding user response:

Just add a forward slash (/) in your HttpResponseRedirect at the beginning of url string

return HttpResponseRedirect('/folder/' str(folder_id) '/')

The url must start with a slash, otherwise it will be treated as a relative url.

CodePudding user response:

To add to @ilyasbbu's answer, if you're using python 3.6 consider using f-string as a more elegant way to work with strings.

return HttpResponseRedirect(f'/folder/{folder_id}/')
  • Related