Skip to content

Commit 0c6f951

Browse files
committed
Tutorial Upload
1 parent 9646721 commit 0c6f951

26 files changed

+493
-0
lines changed

myproject/Pipfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ verify_ssl = true
66
[dev-packages]
77

88
[packages]
9+
asgiref = "==3.3.1"
10+
django-crispy-forms = "==1.11.2"
11+
django-filter = "==2.4.0"
12+
pytz = "==2021.1"
13+
sqlparse = "==0.4.1"
14+
Django = "==3.1.7"
915

1016
[requires]
1117
python_version = "3.7"

myproject/requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
asgiref==3.3.1
2+
Django==3.1.7
3+
django-crispy-forms==1.11.2
4+
django-filter==2.4.0
5+
pytz==2021.1
6+
sqlparse==0.4.1

myproject/src/manage.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
"""Run administrative tasks."""
9+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
10+
try:
11+
from django.core.management import execute_from_command_line
12+
except ImportError as exc:
13+
raise ImportError(
14+
"Couldn't import Django. Are you sure it's installed and "
15+
"available on your PYTHONPATH environment variable? Did you "
16+
"forget to activate a virtual environment?"
17+
) from exc
18+
execute_from_command_line(sys.argv)
19+
20+
21+
if __name__ == '__main__':
22+
main()

myproject/src/myapp/__init__.py

Whitespace-only changes.

myproject/src/myapp/admin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.contrib import admin
2+
from .models import People
3+
4+
class PeopleAdmin(admin.ModelAdmin):
5+
pass
6+
7+
admin.site.register(People, PeopleAdmin)

myproject/src/myapp/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class MyappConfig(AppConfig):
5+
name = 'myapp'

myproject/src/myapp/filters.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# import django_filters
2+
# from .models import People
3+
#
4+
# class PeopleFilter(django_filters.FilterSet):
5+
# age = django_filters.RangeFilter()
6+
#
7+
# class Meta:
8+
# model = People
9+
# fields = ['age']
10+
11+
from django_filters import FilterSet
12+
from django_filters.filters import RangeFilter
13+
from .models import People
14+
from .forms import PeopleFilterFormHelper
15+
from .widgets import CustomRangeWidget
16+
17+
class AllRangeFilter(RangeFilter):
18+
def __init__(self, *args, **kwargs):
19+
super().__init__(*args, **kwargs)
20+
values = [p.age for p in People.objects.all()]
21+
min_value = min(values)
22+
max_value = max(values)
23+
self.extra['widget'] = CustomRangeWidget(attrs={'data-range_min':min_value,'data-range_max':max_value})
24+
25+
class PeopleFilter(FilterSet):
26+
age = AllRangeFilter()
27+
28+
class Meta:
29+
model = People
30+
fields = ['age']
31+
form = PeopleFilterFormHelper

myproject/src/myapp/forms.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from crispy_forms.helper import FormHelper
2+
from crispy_forms.bootstrap import StrictButton
3+
from crispy_forms.layout import Field, Layout
4+
from django import forms
5+
from django_filters.fields import RangeField
6+
7+
class PeopleFilterFormHelper(forms.Form):
8+
def __init__(self, *args, **kwargs):
9+
super().__init__(*args, **kwargs)
10+
self.helper = FormHelper(self)
11+
self.helper.form_method = 'get'
12+
layout_fields = []
13+
for field_name, field in self.fields.items():
14+
if isinstance(field, RangeField):
15+
layout_field = Field(field_name, template="forms/fields/range-slider.html")
16+
else:
17+
layout_field = Field(field_name)
18+
layout_fields.append(layout_field)
19+
layout_fields.append(StrictButton("Submit", name='submit', type='submit', css_class='btn btn-fill-out btn-block mt-1'))
20+
self.helper.layout = Layout(*layout_fields)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 3.1.7 on 2021-04-06 13:40
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
initial = True
9+
10+
dependencies = [
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='People',
16+
fields=[
17+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18+
('name', models.CharField(blank=True, max_length=50, null=True)),
19+
('surname', models.CharField(blank=True, max_length=50, null=True)),
20+
('age', models.IntegerField()),
21+
],
22+
),
23+
]

myproject/src/myapp/migrations/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)