From b8fa6eef5c5c872e8c42f2ae334ca0a269e02546 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Tue, 19 Dec 2017 20:12:21 +0100 Subject: [PATCH 1/2] refactor the views --- django/didgeridoo/webshop/views.py | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 0db7d06..046ae5f 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -1,7 +1,4 @@ from django.shortcuts import get_object_or_404, render -from django.http import HttpResponse -from django.template import loader - from .models import Article, Category, ArticleStatus # Create your views here. @@ -15,11 +12,9 @@ def index(request): category_list.update( {i: Category.objects.filter(parent_category=i.id)}) - template = loader.get_template('webshop/index.html') - context = { - 'category_list': category_list, - } - return HttpResponse(template.render(context, request)) + return render(request, + 'webshop/index.html', + {'category_list': category_list}) def articles_in_category(request, category_id): @@ -29,16 +24,12 @@ def articles_in_category(request, category_id): article_list = Article.objects.filter( category=selected_category.id).exclude(status=hidden.id) - template = loader.get_template('webshop/category.html') - context = { - 'article_list': article_list, - 'category': selected_category, - } - return HttpResponse(template.render(context, request)) + return render(request, 'webshop/category.html', + {'article_list': article_list, + 'category': selected_category}) def article_details(request, article_id): article = get_object_or_404(Article, pk=article_id) - return render(request, - 'webshop/article_details.html', + return render(request, 'webshop/article_details.html', {'article': article}) From bc3b14b5dd697f1784acbcbfa0ef0948de4ef560 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Tue, 19 Dec 2017 20:21:46 +0100 Subject: [PATCH 2/2] add a base.html file and refactor the other html files --- .../templates/webshop/article_details.html | 24 +++++-------- .../webshop/templates/webshop/base.html | 17 ++++++++++ .../webshop/templates/webshop/category.html | 34 +++++++------------ .../webshop/templates/webshop/index.html | 15 +++----- 4 files changed, 43 insertions(+), 47 deletions(-) create mode 100644 django/didgeridoo/webshop/templates/webshop/base.html diff --git a/django/didgeridoo/webshop/templates/webshop/article_details.html b/django/didgeridoo/webshop/templates/webshop/article_details.html index aa66890..e580fdc 100644 --- a/django/didgeridoo/webshop/templates/webshop/article_details.html +++ b/django/didgeridoo/webshop/templates/webshop/article_details.html @@ -1,15 +1,9 @@ - - - - - -
-

{{ article.name }}

-

Description

-

{{ article.description }}

-

Stock: {{ article.stock }}

-

Status: {{ article.status}}

-

Price: {{ article.price_in_chf }}

-
- - +{% extends "webshop/base.html" %} +{% block section_title %}{{ article.name }}{% endblock %} +{% block content %} +

Description

+

{{ article.description }}

+

Stock: {{ article.stock }}

+

Status: {{ article.status}}

+

Price: {{ article.price_in_chf }}

+{% endblock %} diff --git a/django/didgeridoo/webshop/templates/webshop/base.html b/django/didgeridoo/webshop/templates/webshop/base.html new file mode 100644 index 0000000..fa6b702 --- /dev/null +++ b/django/didgeridoo/webshop/templates/webshop/base.html @@ -0,0 +1,17 @@ + + + + + +
+

{% block section_title %}Music Instrument Shop{% endblock %}

+ {% block content %}{% endblock %} +
+ +
+ {% block footer %} +

This is a case study project of Ivan Hörler and Andreas Zweili. + It is a school project/excercise and has no commercial intent.

+ {% endblock %} +
+ diff --git a/django/didgeridoo/webshop/templates/webshop/category.html b/django/didgeridoo/webshop/templates/webshop/category.html index 7e42cfa..373f03f 100644 --- a/django/didgeridoo/webshop/templates/webshop/category.html +++ b/django/didgeridoo/webshop/templates/webshop/category.html @@ -1,21 +1,13 @@ - - - - - - - -
-

{{ category.name }}

-{% if article_list %} - -{% else %} -

There are no articles in this category.

-{% endif %} -
- - +{% extends "webshop/base.html" %} +{% block section_title %}Category Overview{% endblock %} +{% block content %} + {% if article_list %} + + {% else %} +

There are no articles in this category.

+ {% endif %} +{% endblock %} diff --git a/django/didgeridoo/webshop/templates/webshop/index.html b/django/didgeridoo/webshop/templates/webshop/index.html index 721caae..651ea42 100644 --- a/django/didgeridoo/webshop/templates/webshop/index.html +++ b/django/didgeridoo/webshop/templates/webshop/index.html @@ -1,10 +1,5 @@ - - - - - -
-

Music Shop

+{% extends "webshop/base.html" %} +{% block content %} {% if category_list %}
    {% for category, sub_category in category_list.items %} @@ -18,7 +13,5 @@
{% else %}

No categories are available.

- {% endif %} -
- - + {% endif %} +{% endblock %}