2015-02-09 57 views
1

我想调用一个函数,它位于/inscription/views.py中,因为所有视图(因为它是用于登录的)。我需要在参数中传递用户名和密码才能记录用户。django在所有视图中调用函数(登录)

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) 

,在我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> 

回答

2

,你需要的是强制用户登录,如果他是不是已经登录后才能访问您的观点。要做到这一点,你需要做的就是login_requireddecorator

from django.contrib.auth.decorators import login_required 

@login_required 
def my_view(request): 
    ... 

从文档来装饰你的观点:

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. 

更新:

从你的评论,现在我明白了您需要在所有页面中填写表格供用户登录;如果他已经登录,则需要登出链接。首先,您需要为这些视图定义您的URL:

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

而在你的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 %} 

作为一个方面说明,我强烈建议你使用这些可复用的应用之一权威性和登记。除非你有奇怪的要求。

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

+0

不,这不是我的问题对不起,我再试一次^^ 功能登录和注销是在/铭文的视图。 但是,“login”和“logout”按钮以及“username”和“password”项在base.html中不在inscription.html中,因为用户可以在所有视图中连接自己(在网站的顶部所有的网站)。这就是为什么我把它放在base.html中。 那么我怎么能在所有视图中调用登录和注销功能(登录需要2个参数,用户名和密码,所以我需要发送这两个参数)。 非常感谢你 – 2015-02-09 18:36:54

+0

@clementl看到我的更新 – almalki 2015-02-11 08:44:14

+0

非常感谢你这是我的问题我会尽快测试 谢谢 – 2015-02-11 16:15:13

0

,你所面对的,是ü希望登录和注销,从其他页面也正常工作的问题,所以,这个你不用去任何额外的功能。所有你需要做的是,你只要将你的base.html扩展到所有其他的html页面。那么你一定能够从所有页面登录和注销。

假设你已经登录在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> 

现在做一些其它的HTML说在开始的test.html

有你写

{% extends 'base.html' %} 

其次是你的HTML标记。

不要忘记使用

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

在基地以及其他HTML页面。

在其他页面中,您尝试将完整的代码写入模板标签。

对于查询https://docs.djangoproject.com/en/1.7/topics/templates/

同时尝试使用装饰的概念。