TAGS :Viewed: 4 - Published at: a few seconds ago

[ django call function in all views (login) ]

I would like to call a function which is in /inscription/views.py since all views (because it's for the login). And I need to pass the username and the password in parameters to log the user.

def login_user(request):
    if request.method =='POST':
        auth_form=AuthenticationForm(data=request.POST)
        if auth_form.is_valid():
           username = request.POST.get('username')
           password = request.POST.get('password')
           uti = authenticate(username = username,password = password)

           if uti:
            if uti.is_active:
                login(request, uti)
                return HttpResponseRedirect('/accueil')
            else:
                return HttpResponse("Your account is disabled.")
        else:
            return HttpResponse("Invalid login details supplied.")

    else:
        auth_form=AuthenticationForm()
        return render_to_response('authentication.html',
                              {'auth_form': auth_form}, RequestContext(request))

def logout_user(request):
    logout(request)

And In my base.html I would like to add something like :

<label class="form_login">pseudo : </label>
            <input type="text" name="username" id="id_username" class="login_input">

          <label class="form_login">mot de passe : </label>
            <input type="text" name="password" id="id_password" class="login_input">

          <input value="login" type="submit"/>
          <button><a href="/inscription/logout">logout</a></button>

Answer 1


If I understand your question correctly, what you need is to force the user to login if he is not already logged in before he can access your views. To do this, all you need to do is to decorate your views with login_required decorator

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    ...

From the docs:

login_required() does the following:

 - If the user isn’t logged in, redirect to settings.LOGIN_URL, passing
   the current absolute path in the query string. Example:
   /accounts/login/?next=/polls/3/.

 - If the user is logged in, execute the view normally. The view code is
   free to assume the user is logged in.

Update:

From your comment, now I understand that you need to make a form in all pages for the user to login, or a logout link if he is already logged in. First you need to define your URLs for these views:

url(r'^login/$', 'inscription.views.login', name='auth_login'),
url(r'^logout/$', 'inscription.views.logout', name='auth_logout'),

And in your base.html:

{% if user.is_authenticated %}

<a href="{% url 'auth_logout' %}">Logout</a>

{% else %}
    <form method="post" action="{% url 'auth_login' %}">
        {% csrf_token %}
        <input type="text" name="username" id="id_username">
        <input type="text" name="password" id="id_password">
        <input type="submit" value="Log in" />
    </form>
{% endif %}

As a side note, I highly recommend you to use one of these reusable apps for auth and registration. unless you have strange requirements.

http://django-registration-redux.readthedocs.org/en/latest/ http://django-allauth.readthedocs.org/en/latest/

Answer 2


The problem which you are facing is , that u want the login and logout to work from other pages also, So, for this you need not to go for any extra function. All you need to do is, u just extend your base.html to all other html pages. Then you will surely be able to login and logout from all the pages.

Suppose you have login/logout in base.html

<label class="form_login">pseudo :</label>
<input type="text" name="username" id="id_username" class="login_input">

<label class="form_login">mot de passe : </label>
<input type="text" name="password" id="id_password" class="login_input">

<input value="login" type="submit"/>
<button><a href="/inscription/logout">logout</a></button>

Now make some other html say test.html

There at the beginning you write

{% extends 'base.html' %}

followed by your HTML markup.

Don't forget to use

{% block content %} {% endblock %} **template tags**

In base as well as other HTML pages.

In other pages u try to write the complete code in template tags.

For query https://docs.djangoproject.com/en/1.7/topics/templates/

Also try using the concept of decorator.