Merge branch 'master' into currency

* master:
  fix the category list
  add a view to show the details of an article
  add a view to list all articles in a category
  point the project root to the webshop app
  add an index view and a coresponding template
This commit is contained in:
Ivan Hörler 2017-12-18 19:11:24 +01:00
commit 28841df4e3
6 changed files with 106 additions and 3 deletions

View File

@ -17,6 +17,7 @@ from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^webshop/', include('webshop.urls')),
url(r'', include('webshop.urls')),
url(r'^admin/', admin.site.urls),
url(r'^admin/', admin.site.urls),
]

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="{{ STATIC_URL }}/static/admin/css/base.css" />
</head>
<body>
<div id="content" class="flex">
<h1>{{ article.name }}</h1>
<h3>Description</h3>
<p>{{ article.description }}</p>
<p><b>Stock:</b> {{ article.stock }}</p>
<p><b>Status:</b> {{ article.status}}</p>
<p><b>Price:</b> {{ article.price_in_chf }}</p>
</div>
</body>
</html>

View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="{{ STATIC_URL }}/static/admin/css/base.css" />
</head>
<body>
<div id="content" class="flex">
<h1>{{ category.name }}</h1>
{% if article_list %}
<ul>
{% for article in article_list %}
<li><a href="{% url 'details' article.id %}">{{ article.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>There are no articles in this category.</p>
{% endif %}
</div>
</body>
</html>

View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="{{ STATIC_URL }}/static/admin/css/base.css" />
</head>
<body>
<div id="content" class="flex">
<h1>Music Shop</h1>
{% if category_list %}
<ul>
{% for category, sub_category in category_list.items %}
<li><a href="{% url 'category' category.id %}">{{ category.name }}</a></li>
{% for i in sub_category %}
<ul>
<li><a href="{% url 'category' i.id %}">{{ i.name }}</a></li>
</ul>
{% endfor %}
{% endfor %}
</ul>
{% else %}
<p>No categories are available.</p>
{% endif %}
</div>
</body>
</html>

View File

@ -4,4 +4,10 @@ from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^details/(?P<article_id>[0-9]+)/$',
views.article_details,
name='details'),
url(r'^category/(?P<category_id>[0-9]+)/$',
views.articles_in_category,
name='category'),
]

View File

@ -1,8 +1,44 @@
from django.shortcuts import render
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.
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
parent_category_list = Category.objects.filter(parent_category=None)
category_list = {}
for i in parent_category_list:
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))
def articles_in_category(request, category_id):
selected_category = Category.objects.get(id=category_id)
hidden = ArticleStatus.objects.get(name="hidden")
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))
def article_details(request, article_id):
article = get_object_or_404(Article, pk=article_id)
return render(request,
'webshop/article_details.html',
{'article': article})