Leonardo Basso
Leonardo Basso

Reputation: 109

In Django {% if user.is_autenticated %} is not working

I made in Django a login system and it's not working the {% if user.is_autenticated %} method in html files. It always work as "not logged in", also when I'm logged in, I dont understand the problem. Can someone help me pease? Thanks for any help support This is my code:

Views.py

# Form Post 
class EditView(ListView):
    model = Article
    form_class = ArticleForm
    template_name = 'blog/form_post/index.html'
    ordering = ['-data']

class AddPostView (CreateView): # Create new Post
    model = Article
    form_class = ArticleForm
    template_name = 'blog/form_post/add_post.html'

class EditPostView(UpdateView): # Edit a post
    model = Article
    form_class = ArticleForm
    template_name = 'blog/form_post/edit_post.html'

class DeletePostView(DeleteView):
    model = Article
    template_name = 'blog/form_post/delete.html'
    success_url = reverse_lazy('EditHome')
# Login 
def Slogin(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(request, username = username, password = password)
        if user is not None:
            login(request, user)
            return redirect('EditHome')
        else:
            messages.info(request, 'Error')
    context = {}
    return render(request,'blog/form_post/Slogin.html' )
...

Html login File

{% extends 'blog/form_post/layout.html' %}
{% block title %} LogIn{% endblock title %}
{% block content %}

  <form method="POST" action="" class=" form__group">
  <h3 id="form-title" class="m-3 violet ">LOGIN</h3>
    {% csrf_token %}

    <input type="text" name="username" placeholder="Username..." class="w-75 m-3 border-top-0 border-left-0 border-right-0 bg-white">

    <input type="password" name="password" placeholder="Password..." class="w-75 m-3 border-top-0 border-left-0 border-right-0 bg-white"> <br>
    <button class="edit__button btn w-25 m-3" >Login</button>

  </form>



  {% for message in messages %}
 <h2 class="m-3 red">{{ message }}</h2>
  {% endfor %}

{% endblock content %}

html "index" file

{% extends 'blog/form_post/layout.html' %}
{% block title %} DashBoard{% endblock title %}
{% block content %}

{% if user.is_autenticated %}
code...
   {% else %}
   <p>You're not Admin</p>
{% endif %}
{% endblock content %}

Upvotes: 1

Views: 427

Answers (1)

Tom Wojcik
Tom Wojcik

Reputation: 6189

You have a typo. It should be user.is_authenticated.

Upvotes: 2

Related Questions