0

I want to know that if I am using the action in login.html <form > tag and if not using it, In both cases all is good. I am able to successfully login and if there is any error, my views.py showing the respective errors.

I think after rendering the template the django automatically send the data back to user_login function in views.py without specifying the action attribute to the <form > tag.

I just want to know that when do I need to use action attribute in the <form > tag in django template.

My urls.py

from django.urls import path
from . import views

# TEMPLATE URLS!
app_name = 'basic_app'

urlpatterns = [
    path('register/', views.register, name='register'),
    path('user_login/', views.user_login, name='user_login'),
]

views.py

def user_login(request):
    if request.method == 'POST':
        username = request.POST.get('username')  
        password = request.POST.get('password')  

        user = authenticate(username=username, password=password)

        if user:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect(reverse('index'))
            else:
                return HttpResponseRedirect("ACCOUNTS NOT ACTIVE")
        else:
            print("Someone tried to login and failed!")
            print("Username: {} and password: {}".format(username, password))
            return HttpResponse("Invalid login details supplied!")
    else:
        return render(request, 'basic_app/login.html', {})

login.html

{% extends 'basic_app/base.html' %}
{% block body_block %}

<div class="jumbotron">
    <h1>Please Login!</h1>
        <form method="post" action="{% url 'basic_app:user_login' %}">
        {% csrf_token %}
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" placeholder="Enter Username">

        <label for="password">Password:</label>
        <input type="password" id="password" name="password" placeholder="*******">

        <input type="submit" name="" value="Login">
    </form>
</div>
{% endblock %}

{#action="{% url 'basic_app:user_login' %}"#}

If I am not using action="{% url 'basic_app:user_login' %}" in <form > tag of login.html, nothing changes.

  • You can use `action` attribute in form whenever you want some information to be taken from the user. for example, in `login` form the `user` has to enter a `username` and `password`. so the action of the form will be the url for login view. – Ajay Lingayat Nov 27 '20 at 06:58
  • You would ideally specify an action attribute only if you want a different view to handle the form data. – danish_wani Nov 27 '20 at 07:40
  • Yes, but without using action attribute for the URL of login view also, it can send the data to login view. I just want to know why should I use action attribute in the form. If I am not using it everything is fine. And I want to know in which case I need to use this action attribute. – Aditya Gupta Nov 27 '20 at 07:43
  • ok, @danish_wani. thanks – Aditya Gupta Nov 27 '20 at 07:44

1 Answers1

0

@Aditya Gupta

Please look this answer first ---->>>

Now in django normaly you must define action attribut when you want the view you specified on it receive some data. It recommanded to specify url of in action attribute of form.

Mbambadev
  • 729
  • 1
  • 7
  • 26