[Django]-Django #hashtags in url

4👍

RedirectView for this:

views.py

from django.views.generic import RedirectView
from django.urls import reverse

class ViewpostRedirectView(RedirectView):
    def get_redirect_url(*args, **kwargs):
        hash_part = "add_data_Modal"  # the data you want to add to the hash part
        return reverse("createpost") + "#{0}".format(hash_part)

urls.py

path('viewpost/', views.createpost, name='createpost'),
path('viewpost/modal/', views.ViewpostRedirectView.as_view(), name='createpost_modal')

More info : https://www.kite.com/python/docs/django.views.generic.RedirectView

Leave a comment