Commit bd18941f authored by Falk, Dennis Nikolas's avatar Falk, Dennis Nikolas
Browse files

Login Möglichkeit hinzugefügt. Über Footer ist Login möglich. Wenn der User...

Login Möglichkeit hinzugefügt. Über Footer ist Login möglich. Wenn der User eingelogt ist, dann erscheint ein Logout Button im Footer und der Name des Users.
parent bab58aab
......@@ -25,7 +25,16 @@
{% endblock %}
<footer class="fixed-bottom bg-light">
<ul class="nav justify-content-end">
{% if user.is_authenticated %}
<li class="nav-item nav-link">Hallo, {{ user.username }}</li>
<li class="nav-item"><a class="nav-link" href="{% url 'blog:user_logout' %}">Logout</a></li>
{% else %}
<li class="nav-item"><a class="nav-link" href="{% url 'blog:user_login' %}">Login</a></li>
{% endif %}
<li class="nav-item"><a class="nav-link" href="{% url 'blog:blog-detail' slug='impress' %}">Impressum</a></li>
<li class="nav-item"><a class="nav-link" href="{% url 'blog:blog-detail' slug='contact' %}">Kontakt</a></li>
</ul>
......
{% extends 'blog/base.html' %}
{% load bootstrap4 %}
{% block page-title %}
Login
{% endblock %}
{% block heading %}
<h1 class="display-2">Login</h1>
{% endblock %}
{% block content %}
<div class="container">
<form action="{% url 'blog:user_login' %}" method="post">
{% csrf_token %}
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Login</div>
<div class="panel-body">
{% if error_message %}
<p class="bg-danger p-d ml-b">{{ error_message }}</p>
{% endif %}
<div class="form-group clearfix">
<label for="username" class="col-md-4 control-label text-right">Username:</label>
<div class="col-md-6">
<input name="username" value="" type="text" class="form-control" />
</div>
</div>
<div class="form-group clearfix">
<label for="password" class="col-md-4 control-label text-right">Passwort:</label>
<div class="col-md-6">
<input name="password" type="password" class="form-control" />
</div>
</div>
<div class="col-md-6 col-md-offset-4">
<input name="login" type="submit" value="Login" class="btn btn-success" /> &nbsp;
</div>
</div>
</div>
</div>
</form>
</div>
{% endblock %}
\ No newline at end of file
......@@ -13,9 +13,9 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.urls import path
from . import views # . because the views are in the same folder
from . import views # . because the views are in the same folder
app_name = 'blog'
......@@ -24,6 +24,8 @@ urlpatterns = [
path('<slug:slug>', views.detail, name='blog-detail'),
path('comment/save', views.save_comment, name='save_comment')
path('comment/save', views.save_comment, name='save_comment'),
url('login/', views.user_login, name='user_login'),
url('logout/', views.user_logout, name='user_logout')
]
\ No newline at end of file
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseNotAllowed, Http404
from django.contrib import messages
from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse_lazy
from django.contrib.auth import authenticate, login, logout
from .forms import CommentForm
from .models import *
from django.views.generic import DetailView
......@@ -61,3 +64,29 @@ def save_comment(request):
blog = get_object_or_404(Blog, pk=form.data.get('related_blog'))
# back to the details page
return redirect(reverse_lazy('blog:blog-detail', kwargs={'slug': blog.slug}))
def user_login(request):
if request.method == 'POST':
# Process the request if posted data are available
username = request.POST['username']
password = request.POST['password']
# Check username and password combination if correct
user = authenticate(username=username, password=password)
if user is not None:
# Save session as cookie to login the user
login(request, user)
# Success, now let's login the user.
# return render(request, 'blog/all_blogs.html', {'blogs': blogs})
return all_blogs(request)
else:
# Incorrect credentials, let's throw an error to the screen.
return render(request, 'blog/login.html', {'error_message': 'Falscher Username und / oder Passwort.'})
else:
# No post data availabe, let's just show the page to the user.
return render(request, 'blog/login.html')
def user_logout(request):
logout(request)
return all_blogs(request)
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment