Switched index view to class-based

This commit is contained in:
Edgar P. Burkhart 2022-12-28 12:38:20 +01:00
parent a722165ee2
commit bcbf33984b
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
2 changed files with 16 additions and 13 deletions

View file

@ -3,7 +3,7 @@ from django.urls import path
from . import views from . import views
urlpatterns = [ urlpatterns = [
path("", views.index, name="index"), path("", views.IndexView.as_view(), name="index"),
path("login", views.LoginView.as_view(), name="login"), path("login", views.LoginView.as_view(), name="login"),
path("logout", views.LogoutView.as_view(), name="logout"), path("logout", views.LogoutView.as_view(), name="logout"),
path("transactions", views.TransactionListView.as_view(), name="transactions"), path("transactions", views.TransactionListView.as_view(), name="transactions"),

View file

@ -12,7 +12,13 @@ from django.db import models
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.views.generic import CreateView, DeleteView, ListView, UpdateView from django.views.generic import (
CreateView,
DeleteView,
ListView,
TemplateView,
UpdateView,
)
from django.views.generic.edit import ProcessFormView from django.views.generic.edit import ProcessFormView
from .models import ( from .models import (
@ -27,18 +33,15 @@ from .models import (
) )
@login_required class IndexView(LoginRequiredMixin, TemplateView):
def index(request): template_name = "main/index.html"
_transactions = Transaction.objects.all()[:10]
_categories = Category.objects.all()
_snapshots = Snapshot.objects.all()
context = { def get_context_data(self, **kwargs):
"transactions": _transactions, return super().get_context_data(**kwargs) | {
"categories": _categories, "transactions": Transaction.objects.all()[:10],
"snapshots": _snapshots, "categories": Category.objects.all(),
"snapshots": Snapshot.objects.all(),
} }
return render(request, "main/index.html", context)
class LoginView(auth_views.LoginView): class LoginView(auth_views.LoginView):