Skip to content

Commit

Permalink
43 - Raw Create Class Based View
Browse files Browse the repository at this point in the history
  • Loading branch information
codingforentrepreneurs committed Aug 8, 2018
1 parent 1298b91 commit 5d009ef
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 65 deletions.
11 changes: 11 additions & 0 deletions src/courses/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django import forms

from .models import Course


class CourseModelForm(forms.ModelForm):
class Meta:
model = Course
fields = [
'title'
]
14 changes: 14 additions & 0 deletions src/courses/templates/courses/course_create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends 'base.html' %}

{% block content %}


<form action='.' method='POST'>{% csrf_token %}
{{ form.as_p }}
<input type='submit' value='Save' />
</form>




{% endblock %}
5 changes: 3 additions & 2 deletions src/courses/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.urls import path
from .views import (
CourseView,
CourseListView
CourseListView,
CourseCreateView
# my_fbv
)

Expand All @@ -11,7 +12,7 @@
# path('', my_fbv, name='courses-list'),


# path('create/', <create_view>, name='courses-create'),
path('create/', CourseCreateView.as_view(), name='courses-create'),
path('<int:id>/', CourseView.as_view(), name='courses-detail'),
# path('<int:id>/update/', <update_view>, name='courses-update'),
# path('<int:id>/delete/', <delete_view>, name='courses-delete'),
Expand Down
16 changes: 16 additions & 0 deletions src/courses/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
from django.shortcuts import render, get_object_or_404
from django.views import View

from .forms import CourseModelForm
from .models import Course
# BASE VIEW CLass = VIEW

class CourseCreateView(View):
template_name = "courses/course_create.html" # DetailView
def get(self, request, *args, **kwargs):
# GET method
form = CourseModelForm()
context = {"form": form}
return render(request, self.template_name, context)

def post(self, request, *args, **kwargs):
# POST method
form = CourseModelForm(request.POST)
if form.is_valid():
form.save()
context = {"form": form}
return render(request, self.template_name, context)


class CourseListView(View):
Expand Down
Loading

0 comments on commit 5d009ef

Please sign in to comment.