Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 1.2-requests-templates/pagination/pagination/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
from django.urls import path, include

urlpatterns = [
path('', include('stations.urls')),
path('', include('stations.urls'), name='stations'),
]
4 changes: 3 additions & 1 deletion 1.2-requests-templates/pagination/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
django
asgiref==3.5.2
Django==4.0.6
sqlparse==0.4.2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load static %}
{% load static %}

<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -31,7 +31,7 @@
</tr>
</thead>
<tbody>
{% for station in bus_stations %}
{% for station in page %}
<tr>
<td>{{ station.Name }}</td>
<td>{{ station.Street }}</td>
Expand Down
18 changes: 11 additions & 7 deletions 1.2-requests-templates/pagination/stations/views.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
from django.core.paginator import Paginator
from django.shortcuts import render, redirect
from django.urls import reverse
import csv

import pagination.settings


def index(request):
return redirect(reverse('bus_stations'))


def bus_stations(request):
# получите текущую страницу и передайте ее в контекст
# также передайте в контекст список станций на странице

context = {
# 'bus_stations': ...,
# 'page': ...,
}
page_number = int(request.GET.get('page', 1))
with open(pagination.settings.BUS_STATION_CSV, encoding='utf8') as csvfile:
reader = list(csv.DictReader(csvfile))
paginator = Paginator(reader, 10)
page = paginator.get_page(page_number)
context = {'page': page,
}
return render(request, 'stations/index.html', context)
9 changes: 7 additions & 2 deletions 1.2-requests-templates/recipes/calculator/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
# можете добавить свои рецепты ;)
}

# Напишите ваш обработчик. Используйте DATA как источник данных
# Результат - render(request, 'calculator/index.html', context)

def recipes(request, dish):
servings = int(request.GET.get('servings', 1))
context = {'recipe': {key: value * servings for key, value in DATA[dish].items()}}
return render(request, 'calculator/index.html', context)


# В качестве контекста должен быть передан словарь с рецептом:
# context = {
# 'recipe': {
Expand Down
3 changes: 2 additions & 1 deletion 1.2-requests-templates/recipes/recipes/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"""

from django.urls import path
from calculator.views import recipes

urlpatterns = [
# здесь зарегистрируйте вашу view-функцию
path('<dish>/', recipes, name='recipe')
]
4 changes: 3 additions & 1 deletion 1.2-requests-templates/recipes/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
django
asgiref==3.5.2
Django==4.0.6
sqlparse==0.4.2
27 changes: 26 additions & 1 deletion 2.1-databases/models_list_displaying/books/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
from django.core.paginator import Paginator
from django.shortcuts import render
from books.models import Book


def books_view(request):
template = 'books/books_list.html'
context = {}
context = {'books': list(Book.objects.all())}
return render(request, template, context)


def books_paginator(request, pub_date):
template = 'books/books_pagi.html'
books_object = Book.objects.all().order_by('pub_date')
dates = [str(book_date) for book_dates in books_object.values('pub_date') for book_date in book_dates.values()]
pub_date_index = dates.index(pub_date)
paginator = Paginator(dates, 1)
page = paginator.get_page(dates.index(pub_date) + 1)
if pub_date_index < len(dates) - 1:
next_page = dates[pub_date_index + 1]
else:
next_page = None
if pub_date_index > 0:
previous_page = dates[pub_date_index - 1]
else:
previous_page = None
context = {'page': page,
'previous_page': previous_page,
'next_page': next_page,
'books': list(Book.objects.filter(pub_date=pub_date)),
'pub_date': pub_date}
return render(request, template, context)
20 changes: 19 additions & 1 deletion 2.1-databases/models_list_displaying/fixtures/books.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"pk": 1,
"fields": {
"name": "Война и мир",
"author": "Ф.Д. Достоевский",
"author": "Лев Толстой",
"pub_date": "2018-02-27"
}
},
Expand All @@ -16,5 +16,23 @@
"author": "Джордж Оруэл",
"pub_date": "2016-12-06"
}
},
{
"model": "books.book",
"pk": 3,
"fields": {
"name": "Колыбельная",
"author": "Чак Паланик",
"pub_date": "2017-02-15"
}
},
{
"model": "books.book",
"pk": 4,
"fields": {
"name": "Пикник на обочине",
"author": "Аркадий и Борис Стругацкие",
"pub_date": "2021-08-09"
}
}
]
4 changes: 3 additions & 1 deletion 2.1-databases/models_list_displaying/main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
from django.contrib import admin
from django.urls import path

from books.views import books_view
from books.views import books_view, books_paginator

urlpatterns = [
path('', books_view, name='books'),
path('books/<pub_date>/', books_paginator, name='books_paginator'),
path('books/', books_view, name='books'),
path('admin/', admin.site.urls),
]
6 changes: 4 additions & 2 deletions 2.1-databases/models_list_displaying/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
django
psycopg2-binary
asgiref==3.5.2
Django==4.1
psycopg2-binary==2.9.3
sqlparse==0.4.2
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
<div class="header">
<h1><a href="/books">Каталог</a></h1>
</div>
<div class="row">
<div class="book col-md-4">
<h2>Сияние</h2>
<p>Автор: Стивен Кинг</p>
<p>Дата публикации: 2018-09-10</p>
</div>
<div class="book col-md-4">
<h2>1984</h2>
<p>Автор: Джордж Оруэл</p>
<p>Дата публикации: 2015-03-11</p>

<div class="content">
{% for book in books %}
<div class="book">
<h2>{{ book.name }}</h2>
<p>Автор: {{ book.author }}</p>
<p>Дата публикации: {{ book.pub_date }}</p>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<div class="container">
<div class="header">
<h1>Книги выпущенные {{ pub_date }}</h1>
</div>
<div class="row">
<table class="books">
<tbody>
{% for book in books %}
<div class="book">
<h2>{{ book.name }}</h2>
<p>Автор: {{ book.author }}</p>
<p>Дата публикации: {{ book.pub_date }}</p>
</div>
{% endfor %}
</tbody>
</table>

{% if page.has_previous %}
<a class="button button-primary" href="/books/{{ previous_page }}">< {{ previous_page }}</a>
{% endif %}

{% if page.has_next %}
<a class="button button-primary" href="/books/{{ next_page }}">{{ next_page }} ></a>
{% endif %}
</div>
</div>
2 changes: 1 addition & 1 deletion 2.1-databases/work_with_database/main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
'HOST': '127.0.0.1',
'PORT': '5432',
'USER': 'postgres',
'PASSWORD': 'ваш пароль',
'PASSWORD': '1',
}
}

Expand Down
6 changes: 5 additions & 1 deletion 2.1-databases/work_with_database/phones/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from django.contrib import admin
from .models import Phone

# Register your models here.

@admin.register(Phone)
class PhoneAdmin(admin.ModelAdmin):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ def handle(self, *args, **options):

for phone in phones:
# TODO: Добавьте сохранение модели
pass
Phone.objects.create(name=phone['name'],
price=phone['price'],
image=phone['image'],
release_date=phone['release_date'],
lte_exists=phone['lte_exists'],
slug='-'.join(phone['name'].split())
)
26 changes: 26 additions & 0 deletions 2.1-databases/work_with_database/phones/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.0.6 on 2022-08-02 18:00

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Phone',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=50)),
('price', models.DecimalField(decimal_places=2, max_digits=9)),
('image', models.ImageField(upload_to='')),
('releaze_date', models.DateField()),
('lte_exists', models.BooleanField()),
('slug', models.SlugField(allow_unicode=True)),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.6 on 2022-08-04 19:26

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('phones', '0001_initial'),
]

operations = [
migrations.RenameField(
model_name='phone',
old_name='releaze_date',
new_name='release_date',
),
]
8 changes: 6 additions & 2 deletions 2.1-databases/work_with_database/phones/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@


class Phone(models.Model):
# TODO: Добавьте требуемые поля
pass
name = models.CharField(max_length=50)
price = models.DecimalField(max_digits=9, decimal_places=2)
image = models.ImageField()
release_date = models.DateField()
lte_exists = models.BooleanField()
slug = models.SlugField(allow_unicode=True)
17 changes: 15 additions & 2 deletions 2.1-databases/work_with_database/phones/views.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
from django.shortcuts import render, redirect

from phones.models import Phone


def index(request):
return redirect('catalog')


def show_catalog(request):
sort = request.GET.get('sort', None)
if sort == 'name':
phones = list(Phone.objects.all().order_by(sort))
elif sort == 'min_price':
phones = list(Phone.objects.all().order_by('price'))
elif sort == 'max_price':
phones = list(Phone.objects.all().order_by('-price'))
else:
phones = list(Phone.objects.all())
print(phones)
template = 'catalog.html'
context = {}
context = {'phones': phones}
return render(request, template, context)


def show_product(request, slug):
template = 'product.html'
context = {}
context = {'phone': Phone.objects.get(slug=slug)}
print(context['phone'])
return render(request, template, context)
7 changes: 5 additions & 2 deletions 2.1-databases/work_with_database/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
django
psycopg2-binary
asgiref==3.5.2
Django==4.0.6
Pillow==9.2.0
psycopg2-binary==2.9.3
sqlparse==0.4.2
1 change: 1 addition & 0 deletions 2.1-databases/work_with_database/templates/product.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ <h1>{{ phone.name }}</h1>
<div>Дата выпуска: {{ phone.release_date }}</div>
<div>LTE: {{ phone.lte_exists|yesno:'есть,нет' }}</div>
</div>
</div>
{% endblock %}
Loading