2016-10-01から1ヶ月間の記事一覧
Cart VIewの編集と、Itemの削除 carts/view.html の追記 削除ボタンをつける。 {% extends "base.html" %} {% block content %} {{ object }} {{ object.cartitem_set.all }} <table class='table'> {% for item in object.cartitem_set.all %} <tr> <td>{{ item }}</td> <td>{{ item.quantity }}</td> <td>{</td></tr></table>…
Product Viewから、Cartにitemを追加する product_detail.html にコードを追記する。 タグを追加、method=GET, action='{% url "cart"%}' で、carts/view.html に送付する。 タグ、ボタンを追加 <form method='GET' action="{% url 'cart' %}"> <select class='form-control variation_select'> {% for vari_obj in object.variation_set.all %} </select></form>
CartViewをrenderする SingleObjectMixin をimportする 公式ドキュメント: Single object mixins | Django documentation | Django Provides a mechanism for looking up an object associated with the current HTTP request. つまり、サイトを開いている…
Djangoのセッションの利用方法 公式ドキュメント: セッションの使いかた | Django documentation | Django carts/views.py を編集する。 class CartView(View): cart_id = request.session.get("cart_id") if cart_id == None: cart = Cart() cart.save() c…
Add, Remove, Update のCart機能をつけていく carts/views.py のコードを書いていく from django.views.generic import View from django.http import HttpResponseRedirect from django.shortcuts import render # Create your views here. class CartView(…
python shellを使って manytomany through を確認してみる $ python manage.py shell >>> from products.models import Variation >>> from carts.models import Cart, CartItem >>> >>> Variation.objects.all() [<Variation: 16GB>, <Variation: 32GB>, <Variation: cs>, <Variation: cs6>, <Variation: Default>, </variation:></variation:></variation:></variation:></variation:>
多対多フィールドのthroughを使って見る carts/admin.py にcartを追加していく from django.contrib import admin # Register your models here. from .models import Cart, CartItem class CartItemInline(admin.TabularInline): model = CartItem class Ca…
Cart アプリの作成 carts appを作成する python manage.py startapp carts carts/models.py にコードを書いていく はじめに、ざっくりしたアウトラインを書いておくと便利。 class Cart(models.Model): # user # items # timestamp ** created # updated ** …
Admin Inlineで、Productを編集する 公式ドキュメントはこちら adminに新しいclassを設定する。 products/admin.py class ProductAdmin(admin.ModelAdmin): list_display = ['__str__','price'] class Meta: model = Product admin.site.register(Product, P…
HOMEに商品を表示させる newsletter/views.py の編集 ここで編集するのは2つ。 products = Product.objects.all() を、context is_authenticated の箇所を削除する。 def home(request): title = 'Sign Up Now' featured_image = ProductFeatured.objects.f…
Shopping Cartのアイコンを設定する。 navbar.htmlを編集する。 <ul class="nav navbar-nav navbar-right"> <li> <a href='#'><span class='badge'>0</span><i class="fa fa-shopping-cart" aria-hidden="true"></i></a> </li> カートのアイコンは、font awesomeから持ってくる。 ポイントは、<span class='badge'>0</span> 。ここに、カートのアイテム数をcontextなどで渡せば、表示できる。 css …</ul>
Dropdown menu にログインボタンをつける navbar.html を編集する。 コメントアウトしていたdropdown を元に戻す。 以下のような感じで、dropdown listの中に入れてみる。 <ul class="nav navbar-nav navbar-right"> {% if request.user.is_authenticated %} <li><a href="{% url 'auth_logout' %}">Logout</a></li> {% else %} <li><a href="{% url 'registration_register' %}">R…</a></li></ul>
フィーチャーされた商品の表示 models.py に、ProductFeatured のclassを追加する。 def image_upload_to_featured(instance, filename): title = instance.product.title slug = slugify(title) basename, file_extension = filename.split(".") new_filena…
Django Thumbnailを別のテンプレートに移す。 product_thumbnail.html を作成する。 product_list.htmlのthumbnailの機能のコードをproduct_thumbnail.htmlにコピペする。 <div class="thumbnail text-center"> <h4><a href='{{ product.get_absolute_url }}'>{{ product.title }}</a></h4> {% if product.get_image_url %} </div>
UIを修正する product_detai.html を修正 bootstrapのこのCustomContentのThumbnailを使っていく。 とりあえず、exampleをそのまま貼り付け {% for product in related %} <div class="thumbnail"> <img src="..." alt="..."> <div class="caption"> <h3>Thumbnail label</h3> <p>...</p> <p><a href="#" class="btn btn-primary" role="button">Button</a> </p></div></div>
重複なしの、ランダムなqueryset 今のままだと、重複する可能性がある。 class ProductDetailView(DetailView)の修正 order_by を外して、sorted と、lambda を使って見る。 context["related"] = sorted(Product.objects.get_related(instance)[:6], key=la…
Related Productsの表示 models.py のproductmanagerに、get_relatedのfuncを追記する。 class ProductManager(models.Manager): ... def get_related(self, instance): return self.get_queryset() views.py のProductdetailVIewのcontextを更新 related pr…
Categoryのdetailviewを作成する products/urls_categories.py を作成する products/urls_categories.py 新規ファイルを作成する。 products/ulrs.pyをコピペする。 products/views.py にcategory viewを追記していく from .models import Product, Variatio…
Product Categories のmodelをつくる class Product(models.Model): title = models.CharField(max_length=120) description = models.TextField(blank=True, null=True) price = models.DecimalField(decimal_places=0, max_digits=20) active = models.Boo…
Variationがない場合のPriceの表示 variation_set.countがない場合、そのvariation_setのfirstのみ表示する。 ここでも同様に、sale_priceとpriceを表示する。 また、original priceは赤字にして、ラインを入れる。 og-price として、custom.cssに登録する。…
jQueryを使って、variationを選択すると、そのvariationの価格が反映されるものを作りたい。 jQueryは、javascriptなので、javascript.html のincludeの後に、挿入していく? base.htmlにjQueryを反映していく 以下が、jQueryが使えるようになる呪文。以下は…
views.py , forms.py の編集 if new_item.title: をコメントアウト forms.pyの、extra=0にする。fieldから、titleを外しておく。 social shareの導入手順を確認 github.com 上記を参照にする。 product_detail.html に、facebook shareのリンクを貼るだけ <a href="https://www.facebook.com/sharer/sharer.php?u=*****************"> S</a>…
Displaying messages 公式サイト: The messages framework | Django documentation | Django base.html に挿入する。 include navbar の下あたりでいいと思われる。 {% if messages %} <ul class="messages"> {% for message in messages %} <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li> {% endfor %} </ul> …
Login required の機能をmixinに移す。 mixinは、簡単に言うと、他のmodelなどとは独立して機能するclass・functionのこと Login Required mixin は、staffメンバー、ログインしているユーザーでないと、variationを見れなくするための機能。これをmixinにす…
在庫表示のための、Formset フォームセットについての説明は、以下のサイト フォームセット (formsets) — Django 1.4 documentation フォームセット=フォームの集まり views.pyにvariationlistviewを追加する ProductListViewをそのままコピペして、class名…