Позначка: django templates

  • Create an E-commerce Shop with Django (Part 4)

    Create an E-commerce Shop with Django (Part 4)

    How to create a checkout form in an E-commerce Shop with Django? Part 4.

    In the previous Part 3 we create models for Order and in this Part 4 of Create an E-commerce Shop with Django we will create checkout form an order.

    Prerequisites

    • Django Latest Version
    • Virtual Environment
    • IDE PyCharm or Visual Studio Code

    1. Form

    In this part of the tutorial we will create a checkout form.

    Checkout.html file has been adjusted to the needs of the checkout display. then we only need to create a form from the core directory.

    Then in the core directory create a new file called forms.py where this file will store everything related to the form.

    Import :

    from django import forms
    from django_countries.fields import CountryField
    from django_countries.widgets import CountrySelectWidget

    You can see we are using the country field library and this is a third party library so we have to install it to use it. to install country library jus run command below :

    $ pip install django-countries

    and in project’s settings.py add this code :

    INSTALLED_APPS = [
       ...
       'django_countries',
    ]

    Then we create a class form calledCheckoutFormas a form from the checkout page.

    PAYMENT = (
        ('S', 'Stripe'),
        ('P', 'Paypal')
    )
    
    class CheckoutForm(forms.Form):
        street_address = forms.CharField(widget=forms.TextInput(attrs={
            'class': 'form-control',
            'placeholder': '1234 Main St.'
        }))
        apartment_address = forms.CharField(required=False, widget=forms.TextInput(attrs={
            'class': 'form-control',
            'placeholder': 'Apartment or suite'
        }))
        country = CountryField(blank_label='(select country)').formfield(widget=CountrySelectWidget(attrs={
            'class': 'custom-select d-block w-100'
        }))
        zip = forms.CharField(widget=forms.TextInput(attrs={
            'class': 'form-control'
        }))
        same_billing_address = forms.BooleanField(required=False)
        save_info = forms.BooleanField(required=False)
        payment_option = forms.ChoiceField(
            widget=forms.RadioSelect, choices=PAYMENT
        )

    And with this you will have checkout form.

    2. Models

    To create a checkout form we will create a new model class that functions to save shipping address an order.

    the class we will create isCheckoutAddresswhich will store the shipping address of the order from the form we made before.

    class CheckoutAddress(models.Model):
        user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
        street_address = models.CharField(max_length=100)
        apartment_address = models.CharField(max_length=100)
        country = CountryField(multiple=False)
        zip = models.CharField(max_length=100)
    
        def __str__(self):
            return self.user.username

    dont forget as well to impor becouse we would use Country Field:

    from django_countries.fields import CountryField

    Do not forget migrate your model database with the command below :

    $ python manage.py migrate
    $ python manage.py makemigrations

    3. Views

    In the view don’t forget to add the country field library and don’t forget to add the model and form that we just created

    from .forms import CheckoutForm
    from .models import (
        ...
        CheckoutAddress
    )

    Create CheckoutView class

    class CheckoutView(View):
        def get(self, *args, **kwargs):
            form = CheckoutForm()
            context = {
                'form': form
            }
            return render(self.request, 'core/checkout.html', context=context)
    
        def post(self, *args, **kwargs):
            form = CheckoutForm(self.request.POST or None)
    
            try:
                order = Order.objects.get(user=self.request.user, ordered=False)
                if form.is_valid():
                    street_address = form.cleaned_data.get('street_address')
                    apartment_address = form.cleaned_data.get('apartment_address')
                    country = form.cleaned_data.get('country')
                    zip = form.cleaned_data.get('zip')
                    same_billing_address = form.cleaned_data.get('same_billing_address')
                    save_info = form.cleaned_data.get('save_info')
                    payment_option = form.cleaned_data.get('payment_option')
    
                    checkout_address = CheckoutAddress(
                        user=self.request.user,
                        street_address=street_address,
                        apartment_address=apartment_address,
                        country=country,
                        zip=zip
                    )
    
                    checkout_address.save()
                    order.checkout_address = checkout_address
                    order.save()
                    return redirect('core:checkout')
                messages.warning(self.request, 'Fail checkout')
                return redirect('core:checkout')
    
            except ObjectDoesNotExist:
                messages.error(self.request, 'You do not have an order')
                return redirect('core:order-summary')

    In the forms.py file that we made earlier. we send the form into templates via view. Thepostfunction that captures the value sent via the form in the templates.

    4. Urls

    As usual after we make the view don’t forget to register it in the core/urls.py file.

    Import

    from django.urls import path
    from .views import (
        ...
        CheckoutView
    )

    Register all your class and fuctions in views to urls.py file. after that we will add the address in

    Create path url to checkout page with code below:

    urlpatterns = [
       ...
       path('checkout', CheckoutView.as_view(), name='checkout'),
    ]

    And finally the checkout form can be accessed and save the order data into the database

    Proceed to the next part!

    I hope you’ve found the fourth part of this tutorial helpful. In the next session, we’re going to handling payment with stripe to this E-Commerce Website.

    You can visit github where find other scripts.

  • Create a E-commerce Shop with Django (Part 3)

    Create a E-commerce Shop with Django (Part 3)

    In the previous Part 2 we create models for Order and in this Part 3 of Create a E-commerce Shop with Django we will make a summary of the order in the shopping cart of your Django shop.

    Prerequisites

    • Django Latest Version
    • Virtual Environment
    • IDE PyCharm or Visual Studio Code

    Before starting the tutorial don’t forget to activate your Virtual Environment, and the project we created in the previous tutorial.

    1. Models

    Open core / models.py using the edit code and add some functions in class model as follows :

    1. Class Item

    Nothing is changed in the Item class because the order summary is not directly related to the Item class

    2. Class OrderItem

    InOrderItemclass we will add some additional functions as follows

    class OrderItem(models.Model):
        user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
        ordered = models.BooleanField(default=False)
        item = models.ForeignKey(Item, on_delete=models.CASCADE)
        quantity = models.IntegerField(default=1)
    
        def __str__(self):
            return f'{self.quantity} of {self.item.item_name}'
    
        def get_total_item_price(self):
            return self.quantity * self.item.price
    
        def get_discount_item_price(self):
            return self.quantity * self.item.discount_price
    
        def get_amount_saved(self):
            saved = self.get_total_item_price() - self.get_discount_item_price()
            return f'{saved:.2f}'
    
        def get_final_price(self):
            if self.item.discount_price:
                return self.get_discount_item_price()
            return self.get_total_item_price()
    • get_total_item_price, returns the total price value of each product item
    • get_discount_item_price, returns the total price value of each product item based on discounted prices
    • get_amount_saved, returns the value of the price saved from existing discounts
    • get_final_price, returns which function is used as a price determinant (whether using the original price or discounted price)

    3. Class Order

    In the Order class add a function that will calculate the total price of the order

    class Order(models.Model):
        user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
        items = models.ManyToManyField(OrderItem)
        start_date = models.DateTimeField(auto_now_add=True)
        ordered_date = models.DateTimeField()
        ordered = models.BooleanField(default=False)
    
        def __str__(self):
            return self.user.username
    
        def get_total_price(self):
            total = 0
            for order_item in self.items.all():
                total += order_item.get_final_price()
            return total
    • get_total_price, returns the value of the total price of all ordered product items

    4. Migrate model database

    Do not forget migrate your model database with the command below :

    $ python manage.py migrate
    $ python manage.py makemigrations

    2. Views

    After manage our model, now switches to the core/views.py file.

    You can just just takeorder_summary.htmland place it in the templates directory.

    1. Import
    from django.core.exceptions import ObjectDoesNotExist
    from django.shortcuts import render, get_object_or_404, redirect
    from django.contrib import messages
    from django.contrib.auth.decorators import login_required
    from django.contrib.auth.mixins import LoginRequiredMixin
    from django.views.generic import ListView, DetailView, View
    from django.utils import timezone
    from core.models import (
        Item,
        Order,
        OrderItem
    )

    Import all requiment library in your views.py, in django.views.generic you can see we import the View class. for the order summary views we will use this View class

    2. CreateOrderSummaryView

    class OrderSummaryView(LoginRequiredMixin, View):
        def get(self, *args, **kwargs):
    
            try:
                order = Order.objects.get(user=self.request.user, ordered=False)
                context = {
                    'object' : order
                }
                return render(self.request, 'core/order_summary.html', context)
            except ObjectDoesNotExist:
                messages.error(self.request, "You do not have an order")
                return redirect("/")

    In the OrderSummaryView class we use the Order model as the model and order_summary.html as tempalate.

    3. Create reduce quantity product

    In the previous tutorial we made 2 function:add_to_cart()andremove_from_cart().

    add_to_cart() make the quantiy of product adding but remove_from_cart() function will make all order removed. So we will make function that will reduce a quantity of product item and remove it if quantity equal to 0

    def reduce_quantity_item(request, pk):
        item = get_object_or_404(Item, pk=pk)
        order_qs = Order.objects.filter(
            user = request.user,
            ordered=False
        )
        if order_qs.exists():
            order = order_qs[0]
            if order.items.filter(item__pk=item.pk).exists():
                order_item = OrderItem.objects.filter(
                    item = item,
                    user = request.user,
                    ordered = False
                )[0]
                if order_item.quantity > 1:
                    order_item.quantity -= 1
                    order_item.save()
                else:
                    order_item.delete()
                messages.info(request, "Item quantity is updated")
                return redirect("core:order-summary")
            else:
                messages.info(request, "This item not in your cart")
                return redirect("core:product")
        else:
            messages.info(request, "You do not have an order")
            return redirect("core:product")

    4. Make an user must log in to make an order

    For each function add an @login_required annotation, where this will throw the user into the login page when running the function if the user has not logged in

    @login_required
    def add_to_cart(request, pk):
       ...
    @login_required
    def remove_from_cart(request, pk):
       ...
    @login_required
    def reduce_quantity_item(request, pk):
       ...

    3. Urls

    After completing the view, we will now add the OrderSummaryView url and the reduce_quantity item function in the core/url.py file

    1. Import
    from django.urls import path
    from .views import (
        remove_from_cart,
        reduce_quantity_item,
        add_to_cart,
        ProductView,
        HomeView,
        OrderSummaryView
    )

    add all the views and functions that we made in the views.py file on import, so we just add OrderSummaryView and reduce_quantity_item from the previous file.

    2. Create Path Url

    Create path url to OrderSummaryView and reduce_quantity_item() function like this :

    urlpatterns = [
        ...
        path('order-summary/', OrderSummaryView.as_view(), name="order-summary"),
        path('reduce-quantity-item/<int:pk>', reduce_quantity_item, name='reduce-quantity-item'),
    ]

    And now you can access the view and another functions.

    Proceed to the next part!

    I hope you’ve found the third part of this tutorial helpful. In the next session, we’re going to make Checkout form to this E-Commerce Website.

    You can visit github where find other scripts.

  • Create a comment model in Django

    Create a comment model in Django

    I decide create a comment model in Django after Create my first open source project – Pyxtract and after learning Django. I wrote code for some Django apps, e.i. my Blog app.

    But how about receiving some feedbacks from your readers and letting them comment in your Django web site or blog?

    (більше…)
KUPRIENKO