From 6e1a3ca81828ff072b76f60261e424393e4c66d2 Mon Sep 17 00:00:00 2001 From: ack_ik Date: Mon, 26 Jan 2026 15:08:24 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BA=D0=B0=D1=80=D1=82=D0=B8=D0=BD=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=B2=20=D0=BE=D0=B4=D0=BD=D1=83=20=D0=B2=D1=8B=D1=81?= =?UTF-8?q?=D0=BE=D1=82=D1=83,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=20=D0=B2=D1=80=D0=B5?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=B8=20=D0=B3=D0=B5=D0=BD=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D0=B8=20=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=BD=D0=BE=D0=B9=20=D1=81=D1=80=D0=B5=D0=B4=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- products/templates/products/list.html | 2 +- products/views.py | 31 +++++++++++++-- scripts/init_project.ps1 | 55 +++++++++++++++++++++++++++ templates/base.html | 8 ++++ templates/components/footer.html | 5 +++ templates/components/navbar.html | 3 ++ 7 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 scripts/init_project.ps1 diff --git a/.gitignore b/.gitignore index e9dbbab..56d8c05 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ db.sqlite3 .env staticfiles/ media/ -.vscode \ No newline at end of file +.vscode +.venv/ \ No newline at end of file diff --git a/products/templates/products/list.html b/products/templates/products/list.html index 5342e04..5307ae6 100644 --- a/products/templates/products/list.html +++ b/products/templates/products/list.html @@ -12,7 +12,7 @@
{% if product.image %} - {{ product.name }} + {{ product.name }} {% endif %}
{{ product.name }}
diff --git a/products/views.py b/products/views.py index c18c187..99fb7e6 100644 --- a/products/views.py +++ b/products/views.py @@ -1,11 +1,34 @@ import os +import time +from django.http import HttpResponse +from django.template.loader import render_to_string from django.shortcuts import render from .models import Product + def product_list(request): - items = Product.objects.all() env_type = os.getenv("ENV_TYPE", "undefined") - return render(request, 'products/list.html', { + + # Measure DB query time (force evaluation) + db_start = time.time() + items = list(Product.objects.all()) + db_time = time.time() - db_start + + # First render to measure render time + ctx = { 'products': items, - 'env_type': env_type - }) + 'env_type': env_type, + 'db_time': db_time, + 'total_time': None, + } + + render_start = time.time() + _ = render_to_string('products/list.html', ctx, request=request) + render_time = time.time() - render_start + + total_time = db_time + render_time + + # Final render with measured times + ctx['total_time'] = total_time + html = render_to_string('products/list.html', ctx, request=request) + return HttpResponse(html) diff --git a/scripts/init_project.ps1 b/scripts/init_project.ps1 new file mode 100644 index 0000000..231f0bb --- /dev/null +++ b/scripts/init_project.ps1 @@ -0,0 +1,55 @@ +param( + [string]$Username = "ack", + [string]$Email = "admin@example.com", + [string]$Password = "123" +) + +Write-Host "Initializing project environment..." + +if (-not (Test-Path ".venv")) { + Write-Host "Creating virtual environment .venv..." + python -m venv .venv +} else { + Write-Host "Virtual environment .venv already exists." +} + +$venvPython = Join-Path -Path ".venv" -ChildPath "Scripts\python.exe" +$venvPip = Join-Path -Path ".venv" -ChildPath "Scripts\pip.exe" + +Write-Host "Upgrading pip and installing requirements..." +& $venvPython -m pip install --upgrade pip +& $venvPip install -r requirements.txt + +Write-Host "Applying database migrations..." +& $venvPython manage.py migrate + +Write-Host "Collecting static files..." +& $venvPython manage.py collectstatic --noinput + +if (-not $Password) { + $secure = Read-Host "Enter superuser password (input hidden)" -AsSecureString + $bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure) + $plainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr) + [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) +} else { + $plainPassword = $Password +} + +Write-Host "Creating superuser (if it doesn't exist)..." +$createUserPy = @" +from django.contrib.auth import get_user_model +User = get_user_model() +username = '$Username' +email = '$Email' +password = '$plainPassword' +if not User.objects.filter(username=username).exists(): + User.objects.create_superuser(username=username, email=email, password=password) + print('Superuser created.') +else: + print('Superuser already exists.') +"@ + +# pipe the python snippet into manage.py shell +$createUserPy | & $venvPython manage.py shell + +Write-Host "Initialization complete. You can now run the development server with: .\.venv\Scripts\python.exe manage.py runserver" diff --git a/templates/base.html b/templates/base.html index 9ea2fbd..c53adc6 100644 --- a/templates/base.html +++ b/templates/base.html @@ -7,6 +7,14 @@ {% block title %}Мой проект{% endblock %} + {% block extra_css %}{% endblock %} diff --git a/templates/components/footer.html b/templates/components/footer.html index 8a8f37c..7d5a18c 100644 --- a/templates/components/footer.html +++ b/templates/components/footer.html @@ -2,5 +2,10 @@

© 2023 Мой проект. Все права защищены.

+

+ ENV: {{ env_type|default:"unknown" }} + | Page generated in {{ total_time|default:"-"|floatformat:3 }}s + (DB: {{ db_time|default:"-"|floatformat:3 }}s) +

\ No newline at end of file diff --git a/templates/components/navbar.html b/templates/components/navbar.html index 6efb009..3430f51 100644 --- a/templates/components/navbar.html +++ b/templates/components/navbar.html @@ -15,6 +15,9 @@