Skip to content

Commit

Permalink
Second class, second block :bowtie:
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelmv committed Jan 28, 2016
1 parent 26e2bca commit 20070ab
Show file tree
Hide file tree
Showing 27 changed files with 136 additions and 5 deletions.
Binary file added .DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions Shoppy/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,11 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]

STATIC_URL = '/static/'

MEDIA_ROOT = 'media'
MEDIA_URL = '/media/'
Binary file modified Shoppy/settings.pyc
Binary file not shown.
4 changes: 3 additions & 1 deletion Shoppy/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('products.urls')),
]
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Binary file modified Shoppy/urls.pyc
Binary file not shown.
Binary file modified db.sqlite3
Binary file not shown.
Binary file added media/leche.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/leche_DmkCTZA.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/queso.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/taza.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/taza_9SswOT1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added products/.DS_Store
Binary file not shown.
8 changes: 8 additions & 0 deletions products/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django import forms
from .models import Product


class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = '__all__'
Binary file added products/forms.pyc
Binary file not shown.
20 changes: 20 additions & 0 deletions products/migrations/0002_product_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.1 on 2016-01-28 01:21
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('products', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='product',
name='image',
field=models.ImageField(blank=True, upload_to=b''),
),
]
Binary file added products/migrations/0002_product_image.pyc
Binary file not shown.
4 changes: 4 additions & 0 deletions products/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class Product(models.Model):
description = models.CharField(max_length=255)
category = models.CharField(max_length=255)
price = models.DecimalField(max_digits=6, decimal_places=2)
image = models.ImageField(blank=True)

def __str__(self):
return self.name

class Meta:
ordering = ('id',)
Binary file modified products/models.pyc
Binary file not shown.
4 changes: 3 additions & 1 deletion products/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@


urlpatterns = [
url(r'^$', views.hello_world, name='hello')
url(r'^$', views.hello_world, name='hello'),
url(r'^product/(?P<pk>[0-9]+)/$', views.product_detail),
url(r'^product/new', views.new_product, name="new_product")
]
Binary file modified products/urls.pyc
Binary file not shown.
40 changes: 38 additions & 2 deletions products/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.http import HttpResponse
from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
from django.shortcuts import render
from django.shortcuts import render, get_object_or_404

from .forms import ProductForm
from .models import Product


Expand All @@ -12,3 +13,38 @@ def hello_world(request):
'product': product
}
return HttpResponse(template.render(context, request))


def product_detail(request, pk):
product = get_object_or_404(Product, pk=pk)
template = loader.get_template('product_detail.html')
context = {
'product': product
}

return HttpResponse(template.render(context, request))


def new_product(request):
if request.method == 'POST':
form = ProductForm(request.POST)
if form.is_valid():
product = form.save()
product.save()
return HttpResponseRedirect('/')
else:
form = ProductForm()


template = loader.get_template('new_product.html')
context = {
'form': form
}
return HttpResponse(template.render(context, request))







Binary file modified products/views.pyc
Binary file not shown.
Binary file added queso.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions static/css/base.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
body {
font-family: "HelveticaNeue-Light"
}

img {
width: 200px;
height: 200px;
}

9 changes: 8 additions & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>

{% load staticfiles %}
<meta charset="UTF-8">
<title>Hello World</title>
<link rel="stylesheet" href="{% static 'css/base.css' %}">
</head>
<body>
<h1 style="color:red">
Hola
</h1>
<a href="{% url 'products.views.new_product' %}">New products</a>
<ul>
{% for pr in product %}
<li>
{{ pr.name }} |
<a href="{% url 'products.views.product_detail' pk=pr.pk %}">
{{ pr.name }} |
</a>
{{ pr.description }}
<img src="{{ pr.image.url }}" alt="">
</li>
{% endfor %}
</ul>
Expand Down
16 changes: 16 additions & 0 deletions templates/new_product.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>New article</title>
</head>
<body>
<h1>New article</h1>

<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
</body>
</html>
20 changes: 20 additions & 0 deletions templates/product_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
{% load staticfiles %}
<meta charset="UTF-8">
<title>{{ product.name }}</title>
<link rel="stylesheet" href="{% static 'css/base.css' %}">
</head>
<body>
<h1>{{ product.name }}</h1>
<h2>{{ product.description }}</h2>

<p>
{{ product.category }} |
{{ product.price }}
</p>

<img src="{{ product.image.url }}" alt="">
</body>
</html>

0 comments on commit 20070ab

Please sign in to comment.