Django Redirect/Reverse/Render in View Django.How

Author avatar wrote on 03/06/2022

1. Redirect (will prevent resubmitting the form, no context to update)


from django.shortcuts import redirect
return redirect('glossaries')


Pass parameters

return redirect(f'/customer/{pk}')

return redirect(to, permanent=False, *args, **kwargs)

Pass parameters to JavaScript

# In view
return redirect(f'/table/{record_id}?tab=tab2')

# In template JS
var tab = new URL(location.href).searchParams.get("tab") || ''
    if (tab !='' &&  document.getElementById(tab)){
    document.getElementById(tab).checked=true
}

Redirect and go to a section

return redirect(f'/project/{project_id}#sourceDocumentsSection')

2) Render (renders the context dictionary in to the template)

Always use with GET request, do not use with POST request

return render(request, template_name, context=None, content_type=None, status=None, using=None)

return render(request,'/result.html',{'foo':'bar'})

There is also render_to_response, the only difference is that it does not pass the request to the context.

3. Reverse (to a view)

If you need to use something similar to the url template tag in your code

from django.urls import reverse

From django.core.urlresolvers import reverse

return reverse('projects')

Pass args

return reverse('edit_project', kwargs={'project_id':4})

Or

project_id = 4
return reverse('edit_project', args=(project_id,))

In urls.py we can add app name

app_name = 'wordcount'

In view

again_url = reverse('wordcount:index')
return HttpResponse(
    f'Uploaded. Upload another')

4. Reverse_lazy()

(It is useful for when you need to use a URL reversal before your project’s URLConf is loaded)

5. resolve_url (Resolves URL paths to the corresponding view functions)

To View

from django.shortcuts import resolve_url
return resolve_url('edit_project', project_id=4)

6. HttpResponseRedirect (to a url)

To URL

from django.http import HttpResponseRedirect
return HttpResponseRedirect('/url-name-here/')

Redirect to the previous page

from django.http import HttpResponseRedirect

def someview(request):
   return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

Or

from django.shortcuts import redirect

    redirect(request.path_info) # No query parameters
    redirect(request.build_absolute_uri()) # Keeps query parameters
    redirect(request.get_full_path()) # Keeps query parameters

And keep the scroll position

https://stackoverflow.com/questions/49569913/django-redirect-to-the-previous-page-and-keep-the-scroll-position/49569914#49569914

Back to section Ids in Django

With redirect

return redirect(f'/project/{project_id}#infoSection')

Or with custom_tags.py

from django import template
from django.urls import reverse

register = template.Library()

@register.simple_tag
def anchor(url_name, section_id):
    return reverse(url_name) + '#' + section_id

In template

Resources
  • https://docs.djangoproject.com/en/3.2/topics/http/shortcuts/
  • https://docs.djangoproject.com/en/3.2/ref/urlresolvers
  • https://simpleisbetterthancomplex.com/article/2016/09/12/shortcuts.html
  • https://engineertodeveloper.com/a-better-way-to-route-back-to-a-section-ids-in-django
  • CRUDgen Django CRUD generator