Commit f0883adc by Patryk Czarnik

apka "sklep" z modelem

parent 9460a295
...@@ -38,6 +38,7 @@ INSTALLED_APPS = [ ...@@ -38,6 +38,7 @@ INSTALLED_APPS = [
"django.contrib.messages", "django.contrib.messages",
"django.contrib.staticfiles", "django.contrib.staticfiles",
"aplikacja", "aplikacja",
"sklep",
] ]
MIDDLEWARE = [ MIDDLEWARE = [
......
...@@ -17,6 +17,7 @@ Including another URLconf ...@@ -17,6 +17,7 @@ Including another URLconf
from django.contrib import admin 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 as sv
urlpatterns = [ urlpatterns = [
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
...@@ -28,4 +29,6 @@ urlpatterns = [ ...@@ -28,4 +29,6 @@ urlpatterns = [
path("rozmowa", rozmowa), path("rozmowa", rozmowa),
path("kalkulator_get", kalkulator_get), path("kalkulator_get", kalkulator_get),
path("kalkulator_post", kalkulator_post), path("kalkulator_post", kalkulator_post),
path("sklep/produkty.txt", sv.lista_towarow),
path("sklep/produkty.html", sv.lista_towarow_html),
] ]
from django.contrib import admin
# Register your models here.
# Ten plik edytujemy, aby w panelu administratora można było edytować produkty.
# Z bieżącego pakietu (czyli katalogu), z modułu (czyli pliku) models zaimportuj definicję klasy Produkt
from .models import Produkt
# lub: from sklep.models import Produkt
# lub: from sklep.models import *
admin.site.register(Produkt)
from django.apps import AppConfig
class SklepConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "sklep"
# Generated by Django 5.0 on 2023-12-12 15:58
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name="Produkt",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("nazwa", models.CharField(max_length=100)),
("cena", models.DecimalField(decimal_places=2, max_digits=10)),
("termin_waznosci", models.DateField()),
("promocja", models.BooleanField()),
],
),
]
from django.db import models
# ORM
# Create your models here.
class Produkt(models.Model):
nazwa = models.CharField(max_length=100) # varchar
cena = models.DecimalField(max_digits=10, decimal_places=2) # numeric / number / decimal
termin_waznosci = models.DateField()
promocja = models.BooleanField()
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="utf-8">
<title>Produkty naszego sklepu</title>
</head>
<body>
<h1>Lista towarów</h1>
<ul>
{% for t in towary %}
<li>towar: {{ t.nazwa }} - cena {{ t.cena }} zł
(termin ważności {{t.termin_waznosci}})
{% if t.promocja %}}
<strong>PROMOCJA!</strong>
{% endif %}
</li>
{% endfor %}
</ul>
</body>
</html>
from django.test import TestCase
# Create your tests here.
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from .models import Produkt
# Create your views here.
def lista_towarow(request:HttpRequest) -> HttpResponse:
produkty = Produkt.objects.all()
return HttpResponse(str(produkty), content_type='text/plain')
def lista_towarow_html(request:HttpRequest) -> HttpResponse:
produkty = Produkt.objects.all()
# Produkty to są normalne obiekty Pythona
# for p in produkty:
# print(p.cena)
return render(request=request,
template_name='towary.html',
context={'towary': produkty})
\ No newline at end of file
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