Skip to content

Commit

Permalink
41 - Raw Detail Class Based View
Browse files Browse the repository at this point in the history
  • Loading branch information
codingforentrepreneurs committed Jul 15, 2018
1 parent e0fe3da commit 1b25f00
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
21 changes: 21 additions & 0 deletions src/courses/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 2.0.7 on 2018-07-15 23:27

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Course',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=120)),
],
),
]
2 changes: 2 additions & 0 deletions src/courses/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.db import models

# Create your models here.
class Course(models.Model):
title = models.CharField(max_length=120)
5 changes: 5 additions & 0 deletions src/courses/templates/courses/course_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends 'base.html' %}

{% block content %}
<h1>{{object.id }} - {{ object.title }}</h1>
{% endblock %}
2 changes: 1 addition & 1 deletion src/courses/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


# path('create/', <create_view>, name='courses-create'),
# path('<int:id>/', CourseView.as_view(), name='courses-detail'),
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'),
]
14 changes: 9 additions & 5 deletions src/courses/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from django.shortcuts import render
from django.shortcuts import render, get_object_or_404
from django.views import View


from .models import Course
# BASE VIEW CLass = VIEW

class CourseView(View):
template_name = "about.html"
def get(self, request, *args, **kwargs):
template_name = "courses/course_detail.html" # DetailView
def get(self, request, id=None, *args, **kwargs):
# GET method
return render(request, self.template_name, {})
context = {}
if id is not None:
obj = get_object_or_404(Course, id=id)
context['object'] = obj
return render(request, self.template_name, context)

# def post(request, *args, **kwargs):
# return render(request, 'about.html', {})
Expand Down

0 comments on commit 1b25f00

Please sign in to comment.