From 9bdc303f0e8fe31db80e614ae8c9ef86cb2476c3 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Thu, 1 Feb 2018 00:20:47 +0100 Subject: [PATCH] overview + center --- .idea/workspace.xml | 199 +++++++++++++----- Recipies/settings.py | 1 + cookbook/models.py | 4 + cookbook/tables.py | 9 + cookbook/templates/base.html | 1 + cookbook/templates/index.html | 15 +- cookbook/templates/tables/table_template.html | 100 +++++++++ cookbook/views.py | 8 +- db.sqlite3 | Bin 172032 -> 172032 bytes 9 files changed, 267 insertions(+), 70 deletions(-) create mode 100644 cookbook/tables.py create mode 100644 cookbook/templates/tables/table_template.html diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0d184fe3a..4915d31a1 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,17 @@ - + + + + + + + + + + + - + - - + + @@ -54,6 +64,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -69,8 +111,8 @@ - - + + @@ -79,8 +121,8 @@ - - + + @@ -90,7 +132,7 @@ - + @@ -99,8 +141,8 @@ - - + + @@ -120,6 +162,8 @@ settings.LOGIN_REDIRECT_URL LOGIN_REDIRECT_URL + 'django.template.context_processors.request' + of @@ -131,17 +175,19 @@ @@ -157,7 +203,9 @@ \ No newline at end of file diff --git a/Recipies/settings.py b/Recipies/settings.py index c9d01fce5..7ccb2aafa 100644 --- a/Recipies/settings.py +++ b/Recipies/settings.py @@ -38,6 +38,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'django_tables2', 'cookbook.apps.CookbookConfig', ] diff --git a/cookbook/models.py b/cookbook/models.py index 05bf8aad8..a8643468f 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -34,3 +34,7 @@ class Recipe(models.Model): def __str__(self): return self.name + + @property + def all_tags(self): + return ', '.join([x.name for x in self.keywords.all()]) diff --git a/cookbook/tables.py b/cookbook/tables.py new file mode 100644 index 000000000..99c256086 --- /dev/null +++ b/cookbook/tables.py @@ -0,0 +1,9 @@ +import django_tables2 as tables +from .models import * + + +class RecipeTable(tables.Table): + class Meta: + model = Recipe + template_name = 'tables/table_template.html' + fields = ('name', 'category', 'all_tags') diff --git a/cookbook/templates/base.html b/cookbook/templates/base.html index b21cfe6d6..11c458aaf 100644 --- a/cookbook/templates/base.html +++ b/cookbook/templates/base.html @@ -1,4 +1,5 @@ {% load staticfiles %} + {% block title %} diff --git a/cookbook/templates/index.html b/cookbook/templates/index.html index 83e078c3d..70dc264e3 100644 --- a/cookbook/templates/index.html +++ b/cookbook/templates/index.html @@ -1,4 +1,5 @@ {% extends "base.html" %} +{% load django_tables2 %} {% block title %}Rezepte{% endblock %} @@ -6,17 +7,7 @@ <h1>Übersicht</h1> - <table class="table table-bordered table-hover"> - <tr> - <th>Name</th> - <th>Kategorie</th> - </tr> - {% for recipe in recipes %} - <tr> - <td>{{ recipe.name }}</td> - <td>{{ recipe.category }}</td> - </tr> - {% endfor %} - </table> + + {% render_table recipes %} {% endblock %} \ No newline at end of file diff --git a/cookbook/templates/tables/table_template.html b/cookbook/templates/tables/table_template.html new file mode 100644 index 000000000..18d01d379 --- /dev/null +++ b/cookbook/templates/tables/table_template.html @@ -0,0 +1,100 @@ +{% load django_tables2 %} +{% load i18n %} +{% block table-wrapper %} + <div class="row"> + <div class="col"> + {% block table %} + <table {% if table.attrs %} + {{ table.attrs.as_html }}{% else %}class="table table-bordered table-hover"{% endif %}> + {% block table.thead %} + {% if table.show_header %} + <thead> + <tr> + {% for column in table.columns %} + {% if column.orderable %} + <th {{ column.attrs.th.as_html }}><a + href="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}">{{ column.header }}</a> + </th> + {% else %} + <th {{ column.attrs.th.as_html }}>{{ column.header }}</th> + {% endif %} + {% endfor %} + </tr> + </thead> + {% endif %} + {% endblock table.thead %} + {% block table.tbody %} + <tbody> + {% for row in table.page.object_list|default:table.rows %} {# support pagination #} + {% block table.tbody.row %} + <tr {{ row.attrs.as_html }}> + {% for column, cell in row.items %} + <td {{ column.attrs.td.as_html }}> + {% if column.localize == None %}{{ cell }}{% else %}{% if column.localize %} + {{ cell|localize }}{% else %}{{ cell|unlocalize }} + {% endif %}{% endif %}</td> + {% endfor %} + </tr> + {% endblock table.tbody.row %} + {% empty %} + {% if table.empty_text %} + {% block table.tbody.empty_text %} + <tr> + <td colspan="{{ table.columns|length }}">{{ table.empty_text }}</td> + </tr> + {% endblock table.tbody.empty_text %} + {% endif %} + {% endfor %} + </tbody> + {% endblock table.tbody %} + {% block table.tfoot %} + {% if table.has_footer %} + <tfoot> + <tr> + {% for column in table.columns %} + <td {{ column.attrs.tf.as_html }}>{{ column.footer }}</td> + {% endfor %} + </tr> + </tfoot> + {% endif %} + {% endblock table.tfoot %} + </table> + {% endblock table %} + + {% if table.page and table.paginator.num_pages > 1 %} + {% with current_page=table.page.number page_count=table.paginator.num_pages rows_per_page=table.page.object_list|length total_rows=table.page.paginator.count %} + {% block pagination %} + <nav> + <ul class="pagination justify-content-center"> + {% block pagination.allpages %} + <li class="page-item"> + {% blocktrans %}{% endblocktrans %} + </li> + {% for page in table.paginator.page_range %} + {% with range_start=current_page|add:"-3" range_end=current_page|add:"3" page_count_minus_5=page_count|add:"-5" page_count_minus_1=page_count|add:"-1" %} + {% if page == current_page %} + <li class="page-item active"> + <a class="page-link" href="#">{{ page }}</a> + + </li> + {% elif page == 1 or page >= range_start and page <= range_end or page == page_count %} + <li class="page-item"> + <a class="page-link" + href="{% querystring table.prefixed_page_field=page %}">{{ page }}</a> + </li> + {% endif %} + {% if page == 1 and current_page > 5 or pagina == page_count_minus_1 and current_page <= page_count_minus_5 %} + <li class="page-item"></li> + {% endif %} + {% endwith %} + {% endfor %} + {% endblock pagination.allpages %} + </ul> + </nav> + {% endblock pagination %} + {% endwith %} + {% endif %} + + </div> + </div> +{% endblock table-wrapper %} \ No newline at end of file diff --git a/cookbook/views.py b/cookbook/views.py index 85c1e85b4..037d7f565 100644 --- a/cookbook/views.py +++ b/cookbook/views.py @@ -1,11 +1,14 @@ from django.contrib.auth.decorators import login_required from django.shortcuts import render, redirect +from django_tables2 import RequestConfig from cookbook.forms import * +from cookbook.tables import RecipeTable def index(request): - recipes = Recipe.objects.all() - return render(request, 'index.html', {'recipes': recipes}) + table = RecipeTable(Recipe.objects.all()) + RequestConfig(request, paginate={'per_page': 3}).configure(table) + return render(request, 'index.html', {'recipes': table}) @login_required @@ -16,6 +19,7 @@ def new_recipe(request): recipe = form.save(commit=False) recipe.created_by = request.user.id recipe.save() + form.save_m2m() return redirect('index') else: form = RecipeForm() diff --git a/db.sqlite3 b/db.sqlite3 index 4818945a7c03d78fc91c391e4336108ca72ea9c7..f380104f38b7461ea871aeabad5fa2128447016c 100644 GIT binary patch delta 952 zcmaKp&ubGw6vt;~XMfB>@+wMOX@eU?JV-Y?`@1WmX+V0CP`n!~p@@Wv=28Sx@DH%! zAs3G#-b5qyq{MSB6;dxM$-#P2p+&8r9-Q46*|gNdzV9sG_dfG|yQ^(|wXLsB;g!47 zCA{*a^5EDwgPaOW(1knj2yQ_cUcza}wepq(@-#?iaPxHqPmU{xv(hQNxV(JzvUo0C zuP$D>R@HBp3(w01{A^=V&nzH#0XHGc?q;86YuOW-e&&8=p_MoMOk-YG(MeV;vWwLl z26r9X^&Ho;EgsHTe8%C1?L<}(S-$CcVc>@cJ9A7$M+FHBQr*~#@FOoU?H~xfAdP4U zddeatO+~rCaq33RuBDqruM=_Kob@fs*_$NgoL5zJ{2zqduEX6l$BsNVavU?@UPJ79 zfF+!gqN1tcO5Aq#b=tlegn=FW$02j_01|)Ux(G7@X~a?Ztmy}~x5pZB&QTRj3|HEJ z8@El)UDq3hCxoF>lm@>iiD3*8%?m)*#}w!y_z7R(Gkkz|&}|*6v|(ORD8g7LiX1B= z3XN4GvLuvgGNFiJLa`90BZPG5AlQaa@Dbj^8|XkEHsBY07qrLF+%6EB*JTM~!l=Zs z78f;nSK^}_7aF%65qUO|nEVD`U<=;ErXak6vns_&oJ1Ls6V(V+C>A(Lp-iv|rECyK zfgXb2@B?;4q%G(PNC)!qg}M?~CdXByNtNQ;i6k+$UMAEM36kL**i8-J>-Q!G8zh|% H*T~o(VsXTs delta 173 zcmZoTz}0YoYl1Xm?nD`9#@vkwOXS5k_^sLb)A_IP_w%pduj3Ek-^XvwZ@pR3fQ8>k zl8@PvBROA~k6D{BIX^!;35es1Qj;?aQknR%i840_=x-0uXWVjui<#eofqyT5HNOSW zEG_=&7W_<S7PBM>03|*!@PFX{&Hr_?V8SE*=^yl&M1T?s%UJ@L3^ubA{N>-yIDzRu FKLGsHG-m(+