Commit f7275052 by Patryk Czarnik

wypełnienie aplikacji sklep zawartością

parent 201bb0a4
from django.contrib import admin from django.contrib import admin
# Register your models here. # Register your models here.
# from sklep.models import Product
from .models import *
admin.site.register(Product)
# Generated by Django 5.2.1 on 2025-05-16 08:24
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name="Product",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=100)),
("price", models.DecimalField(decimal_places=2, max_digits=10)),
("vat", models.DecimalField(decimal_places=2, max_digits=2)),
("valid_to", models.DateField(blank=True, null=True)),
("available", models.BooleanField(default=True)),
],
),
]
from django.db import models from django.db import models
# Create your models here. # Create your models here.
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=10, decimal_places=2)
vat = models.DecimalField(max_digits=2, decimal_places=2)
valid_to = models.DateField(blank=True, null=True)
available = models.BooleanField(default=True)
def vat_jako_calkowita(self):
return int(100 * self.vat)
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="utf-8">
<title>Produkty naszego sklepu</title>
</head>
<body>
<h1>Lista towarów</h1>
<ul>
{% for p in products %}
<li>towar: {{ p.name }} - cena {{ p.price }} zł, {{ p.vat_jako_calkowita }}% VAT
{% if p.valid_to %}
<br/>(termin ważności {{p.valid_to}})
{% endif %}
{% if not p.available %}
<br/><strong>Produkt niedostępny</strong>
{% endif %}
</li>
{% endfor %}
</ul>
</body>
</html>
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render from django.shortcuts import render
from .models import *
# Create your views here. # Create your views here.
def lista_produktow(request: HttpRequest) -> HttpResponse:
produkty = Product.objects.all()
return HttpResponse(str(produkty), content_type='text/plain')
def lista_produktow_txt(request: HttpRequest) -> HttpResponse:
products = Product.objects.all()
wynik = 'Produkty naszego sklepu:'
for product in products:
wynik += f'\n - {product.name} za {product.price} zł'
return HttpResponse(wynik, content_type='text/plain;charset=utf-8')
def lista_produktow_html(request: HttpRequest) -> HttpResponse:
products = Product.objects.all()
return render(request, 'towary.html', context={'products': products})
...@@ -19,6 +19,7 @@ from django.contrib import admin ...@@ -19,6 +19,7 @@ from django.contrib import admin
from django.urls import path from django.urls import path
from aplikacja.views import * from aplikacja.views import *
import sklep.views
urlpatterns = [ urlpatterns = [
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
...@@ -31,4 +32,8 @@ urlpatterns = [ ...@@ -31,4 +32,8 @@ urlpatterns = [
path("kalkulator", kalkulator), path("kalkulator", kalkulator),
path("kalkulator_post", kalkulator_post), path("kalkulator_post", kalkulator_post),
path("formularz", formularz), path("formularz", formularz),
path("sklep", sklep.views.lista_produktow),
path("sklep.txt", sklep.views.lista_produktow_txt),
path("sklep.html", sklep.views.lista_produktow_html),
] ]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment