Load multiple functions on main page using Django

Asked

Viewed 50 times

0

I’m having a problem trying to load some data from mysql database using Django, type functions only work if I assign in the file: py. the following attribute:

path('', views.countregistered)

But this attribute is already used for the page index.html as shown below:

path('', views.index, name='home')

If I insert several attributes like this only the first one runs and the rest is ignored, I believe not to be correct, i have several functions to be displayed on the home page that are loaded from the database but if I only type the element with the functions in the html page does not work. Thanks for your help, I’m starting now at Django.

1 answer

0

The path function you are using serves to register Urls, and the first parameter you pass to this function is a string with the URL you want to register. So when a user writes in the browser the path to your site: "www.name-of-your-site.com/", the server will try to locate a URL that matches the one the user entered. As you mentioned, the index page is already registered with an empty string:

path('', views.index, name='home')

However you are trying to register another URL with the same string:

path('', views.countregistered)

So when the server tries to resolve the URL there are two equal ones, so only one of the functions is used. So when you are registering a URL with the path, use different strings, for example:

path('count-registered/', views.countregistered)

Soon each path will direct to its correlated function.

I also suggest you read the documentation to better understand the use of the path function and good practices in its use. https://docs.djangoproject.com/pt-br/3.2/ref/urls/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.