As the titles, I'm looking for the correct way to "link" views to a homepage, so whenever I press one of the buttons in the homepage, it will redirect me to a view related to a form.
Right now my django app is composed by multiple views where each one of them are forms that have their own .html but I'm having a hard time to understand or find a way to make a homepage where I have links to specific views and whenever I press the submit button inside the respective form, it would redirect me to the homepage.
I was thinking that the correct way to do it was making a new view that will take the role of my "homepage" but I don't know if its possible to have multiple views related to the same view
Note: I understand that coding an example for this would take a while, I am just making this question so I can get a better idea and start looking in the right direction
CodePudding user response:
I am not sure that I fully understand your question. However, hopefully the below information may help you get a better idea and start looking in the right direction.
redirect()
The redirect() function in Django can be used to redirect to another view/URL from within a view function.
Documentation: https://docs.djangoproject.com/en/4.0/topics/http/shortcuts/#redirect.
{% url %}
The url template function can be used to link to other views/web pages from within a Django template. This prevents the need to hard-code URLs into web pages.
Documentation: https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#url.
CodePudding user response:
If I am not wrong you want to redirect to homepage after submitting a form. you can check this link how to add action in form though it's w3school tutorial in HTML form.
So, here I am adding code from django documentation.
<form action="/your-name/" method="post">
<label for="your_name">Your name: </label>
<input id="your_name" type="text" name="your_name" value="{{ current_name }}">
<input type="submit" value="OK">
So, you can add action to homepage. just add the page URL name in the action tag --- suppose you homepage URL is like below ---
path('index/', views.index, name='home'),
Then add home to the action place ---
<form action="{% url 'home' %}" method="post">
<label for="your_name">Your name: </label>
<input id="your_name" type="text" name="your_name" value="{{ current_name }}">
<input type="submit" value="OK">
That will work for sure.
For more information you can go through the below references ---
you will get this in django official documentation as well. check this link
And here is the whole forms documentation of django check this link
Still if am wrong or didn't understand you question then please correct me.
Thanks for your question.
