diff --git a/padelclub_backend/settings.py b/padelclub_backend/settings.py index 584d5aa..eeccd44 100644 --- a/padelclub_backend/settings.py +++ b/padelclub_backend/settings.py @@ -15,9 +15,6 @@ import os # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent -MEDIA_URL = '/media/' -MEDIA_ROOT = os.path.join(BASE_DIR, 'media') - # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ diff --git a/shop/management/commands/create_initial_shop_data.py b/shop/management/commands/create_initial_shop_data.py index 8e90930..eb4438f 100644 --- a/shop/management/commands/create_initial_shop_data.py +++ b/shop/management/commands/create_initial_shop_data.py @@ -1,8 +1,6 @@ from django.core.management.base import BaseCommand from shop.models import Color, Size, Product -from django.core.files.base import ContentFile -import os -import urllib.request +from django.conf import settings class Command(BaseCommand): help = 'Creates initial data for the shop' @@ -54,7 +52,7 @@ class Command(BaseCommand): 'cut': 2, # Men 'colors': ['Black', 'White', 'Red'], 'sizes': ['M', 'L', 'XL'], - 'image_url': 'https://example.com/images/tennis_racket.jpg' + 'image_filename': 'hat.jpg' # Just the filename }, { 'title': 'Sports T-Shirt', @@ -63,7 +61,7 @@ class Command(BaseCommand): 'cut': 1, # Women 'colors': ['Black', 'White', 'Blue', 'Red'], 'sizes': ['XS', 'S', 'M', 'L', 'XL'], - 'image_url': 'https://example.com/images/tshirt.jpg' + 'image_filename': 'tshirt.jpg' # Just the filename }, { 'title': 'Kids Tennis Shorts', @@ -72,7 +70,7 @@ class Command(BaseCommand): 'cut': 3, # Kids 'colors': ['Blue', 'White'], 'sizes': ['XS', 'S', 'M'], - 'image_url': 'https://example.com/images/kids_shorts.jpg' + 'image_filename': 'kids_shorts.jpg' # Just the filename } ] @@ -97,17 +95,17 @@ class Command(BaseCommand): for size_name in product_data['sizes']: product.sizes.add(size_objects[size_name]) - # Add image (commented out, use if needed) - """ - try: - result = urllib.request.urlretrieve(product_data['image_url']) - product.image.save( - os.path.basename(product_data['image_url']), - ContentFile(open(result[0], 'rb').read()) - ) - except Exception as e: - self.stdout.write(f'Error downloading image: {e}') - """ + # Construct the full path for storage + if 'image_filename' in product_data and product_data['image_filename']: + # Construct the URL path to the image + # This uses STATIC_URL from your settings + image_path = f"{settings.STATIC_URL}shop/images/products/{product_data['image_filename']}" + print(image_path) + # Store this path in the database + product.image = image_path + product.save() + + self.stdout.write(f'Added image path "{image_path}" for: {product_data["title"]}') else: self.stdout.write(f'Product already exists: {product_data["title"]}') diff --git a/shop/migrations/0015_alter_product_image.py b/shop/migrations/0015_alter_product_image.py new file mode 100644 index 0000000..22e30bc --- /dev/null +++ b/shop/migrations/0015_alter_product_image.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.11 on 2025-03-20 17:30 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('shop', '0014_alter_size_name'), + ] + + operations = [ + migrations.AlterField( + model_name='product', + name='image', + field=models.CharField(blank=True, max_length=200, null=True), + ), + ] diff --git a/shop/models.py b/shop/models.py index 3f43ee6..eebbe93 100644 --- a/shop/models.py +++ b/shop/models.py @@ -28,7 +28,7 @@ class Size(models.Model): class Product(models.Model): title = models.CharField(max_length=200) - image = models.ImageField(upload_to="products/", null=True, blank=True) + image = models.CharField(max_length=200, null=True, blank=True) price = models.DecimalField(max_digits=10, decimal_places=2, default=0.00) # Use string references to prevent circular imports diff --git a/shop/static/shop/images/products/hat.jpg b/shop/static/shop/images/products/hat.jpg new file mode 100644 index 0000000..3a67a20 Binary files /dev/null and b/shop/static/shop/images/products/hat.jpg differ diff --git a/shop/static/shop/images/products/hoodie_h.jpeg b/shop/static/shop/images/products/hoodie_h.jpeg new file mode 100644 index 0000000..8b3fdee Binary files /dev/null and b/shop/static/shop/images/products/hoodie_h.jpeg differ diff --git a/shop/static/shop/images/products/tshirt_h.png b/shop/static/shop/images/products/tshirt_h.png new file mode 100644 index 0000000..b82c8c3 Binary files /dev/null and b/shop/static/shop/images/products/tshirt_h.png differ diff --git a/shop/templates/shop/product_item.html b/shop/templates/shop/product_item.html index 493e6ec..dda68f4 100644 --- a/shop/templates/shop/product_item.html +++ b/shop/templates/shop/product_item.html @@ -1,7 +1,7 @@
{% if product.image %} - {{ product.title }} + {{ product.title }} {% else %}
No Image Available
{% endif %} diff --git a/tournaments/urls.py b/tournaments/urls.py index a071a15..85a89fa 100644 --- a/tournaments/urls.py +++ b/tournaments/urls.py @@ -78,5 +78,3 @@ urlpatterns = [ path('activation-success/', views.activation_success, name='activation_success'), path('activation-failed/', views.activation_failed, name='activation_failed'), ] -if settings.DEBUG: - urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)