(Django) How to implement a button in the template that triggers any method?

Asked

Viewed 1,357 times

0

My question is about implementing buttons in any template. I want to click this button and do the same thing, such as display a message on the screen. How can I do that? Here is an image of my application that I want to add an "Exit" button that will say a trivial message like xau. imagem

  • You have two alternatives, make appear the message using JavaScript or link to redirect to another template that has this message, which is the question?

  • Do you mean to implement a button using Javascript? Sorry ignorance is that I have never studied JS. My other question is whether there is any way I can create a button using HTML in conjunction with Django’s template tags that triggers a method I implemented myself?

  • What do you mean? This kind of thing can only be done via Ajax. If what you want is just to show a message, or you redirect to another page or write this message via JavaScript.

  • It’s just that my teacher asked me to research how to implement my own methods to be used in Django and display their operation on a screen. That’s why I wanted to use the button to call a custom action.

  • For this you only have these options I mentioned, with Ajax you make a request to the server, process the request and return what you need without updating the page. The other way is to create a url that when accessing treats what you want and redirects to the successful page. This is at your discretion.

2 answers

1

I understand you want to do this in Django. Well, if in your case you want to register and after the registration display the message, my tip would be this:

And at the end of your view registration function you should add a render request, like this:

def nome_da_sua_view(request):
    # Código de cadastro aqui
    return render(request, 'thanks.html', {})

Remember that it should stay within the validation if it is a POST method, something like this:

 if request.method == 'POST':

Then you should add this redirect to your URLS:

urlpatterns = [
    url(r'^thanks/', 'nome_do_seu_app.views.nome_da_sua_view'),
]

Finally, inside your template folder, it should contain an HTML file called Thanks.html.

0

Thank you guys! I’ve done many tricks here, it was necessary to create a url that calls a view that executes the action. I implemented the view to list all the addresses already registered and then put the list in a template. This way when I click on the button "display entries" the screen of registered addresses is displayed immediately, this was done with Ajax. imagem1 imagem2

Browser other questions tagged

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