Jeffrey Muller
Jeffrey Muller

Reputation: 850

Call Django view from a template

I want to call a Django view from a Django template. Is there a tag to do that ?

For example, I want to create a view which manage my login form. So basically, this view will be used just to send the Django form to the html template.

Thank you for your help.

Upvotes: 1

Views: 1695

Answers (2)

Burhan Khalid
Burhan Khalid

Reputation: 174614

If you are doing this only for logging users, you should read authenticating users in django which shows you how to write forms for logging in users.

If you want to send data filled in a form to a django view, simply set the action attribute of the form to the URL that is mapped to the view:

<form method="POST" action="/user-login">
  {% csrf_token %}

If you are using POST, you need to include {% csrf_token %} in your HTML form; and you should read the documention on csrf.


If you want to call some functionality from the template directly, write a custom template tag, which allows you to execute arbitrary code and put the results in any template (or add them to the template context).

Upvotes: 1

Rag Sagar
Rag Sagar

Reputation: 2374

Map an url to that view. Redirect to that url from template.

Upvotes: 0

Related Questions