diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0d184fe3..4915d31a 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 c9d01fce..7ccb2aaf 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 05bf8aad..a8643468 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 00000000..99c25608 --- /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 b21cfe6d..11c458aa 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 83e078c3..70dc264e 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 00000000..18d01d37 --- /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 85c1e85b..037d7f56 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 4818945a..f380104f 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ