Commit 96e37d0e authored by Anya Habana's avatar Anya Habana

Converted index FBV to CBV

parent d02e2cd2
No preview for this file type
...@@ -3,7 +3,7 @@ from django.urls import path ...@@ -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('<int:pk>/details/', views.WidgetUserDetailView.as_view(), name='details'), path('<int:pk>/details/', views.WidgetUserDetailView.as_view(), name='details'),
path('add/', views.newuser, name='newuser'), path('add/', views.newuser, name='newuser'),
] ]
......
...@@ -2,22 +2,23 @@ from django.shortcuts import render, redirect ...@@ -2,22 +2,23 @@ from django.shortcuts import render, redirect
from django.http import HttpResponse from django.http import HttpResponse
from django.template import loader from django.template import loader
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.base import TemplateView
from .models import WidgetUser from .models import WidgetUser
from .forms import WidgetUserForm from .forms import WidgetUserForm
def index(request): class IndexView(TemplateView):
user_list = WidgetUser.objects.order_by("last_name") template_name = "users/index.html"
context = {
"user_list": user_list, def get_context_data(self, **kwargs):
} context = super(IndexView, self).get_context_data(**kwargs)
template = loader.get_template("users/index.html") context['user_list'] = WidgetUser.objects.order_by("last_name")
return HttpResponse(template.render(context, request)) return context
def newuser(request): def newuser(request):
if request.method == "POST": if request.method == "POST":
user_form = WidgetUserForm(request.POST) user_form = WidgetUserForm(request.POST)
if user_form.is_valid(): if user_form.is_valid():
new_user = user_form.save() user_form.save()
return redirect("homepage:index") return redirect("homepage:index")
else: else:
user_form = WidgetUserForm() user_form = WidgetUserForm()
......
Markdown is supported
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