Skip to content

Commit

Permalink
7 - ImageField & FileField
Browse files Browse the repository at this point in the history
  • Loading branch information
codingforentrepreneurs committed Sep 1, 2017
1 parent b734374 commit 08a2426
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 2 deletions.
Binary file modified src/db.sqlite3
Binary file not shown.
20 changes: 20 additions & 0 deletions src/products/migrations/0003_product_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-09-01 21:48
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('products', '0002_product_price'),
]

operations = [
migrations.AddField(
model_name='product',
name='image',
field=models.FileField(blank=True, null=True, upload_to='products/'),
),
]
21 changes: 21 additions & 0 deletions src/products/migrations/0004_auto_20170901_2159.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-09-01 21:59
from __future__ import unicode_literals

from django.db import migrations, models
import products.models


class Migration(migrations.Migration):

dependencies = [
('products', '0003_product_image'),
]

operations = [
migrations.AlterField(
model_name='product',
name='image',
field=models.ImageField(blank=True, null=True, upload_to=products.models.upload_image_path),
),
]
21 changes: 20 additions & 1 deletion src/products/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import random
import os
from django.db import models

# Create your models here.
def get_filename_ext(filepath):
base_name = os.path.basename(filepath)
name, ext = os.path.splitext(base_name)
return name, ext


def upload_image_path(instance, filename):
# print(instance)
#print(filename)
new_filename = random.randint(1,3910209312)
name, ext = get_filename_ext(filename)
final_filename = '{new_filename}{ext}'.format(new_filename=new_filename, ext=ext)
return "products/{new_filename}/{final_filename}".format(
new_filename=new_filename,
final_filename=final_filename
)

class Product(models.Model):
title = models.CharField(max_length=120)
description = models.TextField()
price = models.DecimalField(decimal_places=2, max_digits=20, default=39.99)
image = models.ImageField(upload_to=upload_image_path, null=True, blank=True)

def __str__(self):
return self.title
Expand Down
3 changes: 2 additions & 1 deletion src/products/templates/products/detail.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
{{ object.title }} <br/>
{{ object.description }} <br/>
{{ object.description }} <br/>
<img src='{{ object.image.url }}' class='img-fluid' />
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"folders":
[
{
"path": "."
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 08a2426

Please sign in to comment.