Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Alternative to hardcoding urlpatterns in django

I am making a django website in which the homepage shows links of domains… upon clicking on each domain, you links of respective topics and upon clicking each topic you get links of respective subtopics…

The problem is that I want to extract the domains, topics, and subtopics from database using queries…
which means that I cannot hardcode urlpatterns into my program…

How can I make this work so that in the homepage, I extract the list of domains… display them… and upon clicking them, extract the list of topics under them and display them on a new link…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can include parameters in your paths. Indeed, for example:

urlpatterns = [
    path('<str:domain>/<str:topic>/<str:subtopic>/', some_view)
]

and define your view as:

def some_view(request, domain, topic, subtopic):
    # …

If you then visit /science/physics/quantum/, it will call the some_view with domain set to 'science', topic set to 'physics' and subtopic set to 'quantum'.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading