From b9a1a0739d526d3dc4a052b08b3b6bc21e6bd69d Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 25 Apr 2020 21:09:00 +0200 Subject: [PATCH] Make it pretty! --- ubookmark/settings.py | 2 ++ ubookmark/templates/ubookmark/base.html | 11 +++++++++- ubookmark/templates/ubookmark/login.html | 14 +++++++++++++ ubookmark/urls.py | 2 ++ ubookmark/views.py | 26 +++++++++++++++++++++--- 5 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 ubookmark/templates/ubookmark/login.html diff --git a/ubookmark/settings.py b/ubookmark/settings.py index 59a5521..3866cfc 100644 --- a/ubookmark/settings.py +++ b/ubookmark/settings.py @@ -154,3 +154,5 @@ AUTHENTICATION_BACKENDS = [ AUTH_USER_MODEL = 'ubookmark.User' CRISPY_TEMPLATE_PACK = 'bootstrap4' + +LOGOUT_URL="/logout" diff --git a/ubookmark/templates/ubookmark/base.html b/ubookmark/templates/ubookmark/base.html index 512d4c2..09e84c6 100644 --- a/ubookmark/templates/ubookmark/base.html +++ b/ubookmark/templates/ubookmark/base.html @@ -14,7 +14,7 @@ diff --git a/ubookmark/templates/ubookmark/login.html b/ubookmark/templates/ubookmark/login.html new file mode 100644 index 0000000..9e85929 --- /dev/null +++ b/ubookmark/templates/ubookmark/login.html @@ -0,0 +1,14 @@ +{% extends 'ubookmark/base.html' %} +{% load crispy_forms_tags %} + +{% block content %} +
+ +
+ {% csrf_token %} + {{ form|crispy }} + +
+
+ +{% endblock %} diff --git a/ubookmark/urls.py b/ubookmark/urls.py index b290419..a79f0e4 100644 --- a/ubookmark/urls.py +++ b/ubookmark/urls.py @@ -21,5 +21,7 @@ from ubookmark import views urlpatterns = [ path('', views.IndexView.as_view(), name='index'), path('submit/', views.SubmitView.as_view(), name='submit'), + path('login/', views.LoginView.as_view(), name='login'), + path('logout/', views.logout_view, name='logout'), path('admin/', admin.site.urls), ] diff --git a/ubookmark/views.py b/ubookmark/views.py index 86f290c..3737f3c 100644 --- a/ubookmark/views.py +++ b/ubookmark/views.py @@ -1,6 +1,10 @@ from django.views.generic.list import ListView from django.views.generic.edit import CreateView from django.contrib.auth import views as auth_views +from django.contrib.auth.mixins import LoginRequiredMixin + +from django.contrib.auth import logout +from django.shortcuts import redirect from django.conf import settings @@ -8,19 +12,22 @@ from ubookmark.models import * class IndexView(ListView): model = BookmarkModel - paginate_by = 3 + paginate_by = 5 queryset = BookmarkModel.objects.order_by('-posted_at') def get_context_data(self, **kwargs): context = super(IndexView, self).get_context_data(**kwargs) context['site_name'] = settings.SITE_NAME context['site_description'] = settings.SITE_DESCRIPTION + context['site_description'] = settings.SITE_DESCRIPTION return context -class SubmitView(CreateView): +class SubmitView(LoginRequiredMixin, CreateView): model = BookmarkModel fields = [ 'comment', 'url' ] + login_url = '/login/' + success_url = '/' def get_context_data(self, **kwargs): context = super(CreateView, self).get_context_data(**kwargs) @@ -29,5 +36,18 @@ class SubmitView(CreateView): return context + def form_valid(self, form): + form.instance.owner = self.request.user + return super(SubmitView, self).form_valid(form) + class LoginView(auth_views.LoginView): - pass + template_name = 'ubookmark/login.html' + extra_context = {} + + extra_context['site_name'] = settings.SITE_NAME + extra_context['site_description'] = settings.SITE_DESCRIPTION + + +def logout_view(request): + logout(request) + return redirect("/")