学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

Django3.2 | クラウドソーシングアプリの構築 | 24 | 配達依頼

↓↓クリックして頂けると励みになります。


23 | クレジットカード登録】 << 【ホーム】 >> 【25 | ページレイアウト


「crowdsource/urls.py」ファイルを編集します。


記述編集 【Desktop/crowdsource/crowdsource/urls.py】16行目

from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static

from core import views

from core.customer import views as customer_views
from core.courier import views as courier_views

customer_urlpatters = [
    path('', customer_views.home, name="home"),
    path('profile/', customer_views.profile_page, name="profile"),
    path('payment_method/', customer_views.payment_method_page, name="payment_method"),
    path('create_job/', customer_views.create_job_page, name="create_job"),
]

courier_urlpatters = [
    path('', courier_views.home, name="home"),
]


urlpatterns = [
    path('admin/', admin.site.urls),
    path('oauth/', include('social_django.urls', namespace='social')),
    path('', views.home),

    path('sign-in/', auth_views.LoginView.as_view(template_name="sign_in.html")),
    path('sign-out/', auth_views.LogoutView.as_view(next_page="/")),
    path('sign-up/', views.sign_up),

    path('customer/', include((customer_urlpatters, 'customer'))),
    path('courier/', include((courier_urlpatters, 'courier'))),

]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)



「core/customer/views.py」ファイルを編集します。


記述編集 【Desktop/crowdsource/core/customer/views.py】

import firebase_admin
from firebase_admin import credentials, auth
import stripe

from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.urls import reverse
from core.customer import forms

from django.contrib import messages
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth import update_session_auth_hash
from django.conf import settings

cred = credentials.Certificate(settings.FIREBASE_ADMIN_CREDENTIAL)
firebase_admin.initialize_app(cred)

stripe.api_key = settings.STRIPE_API_SECRET_KEY

@login_required()
def home(request):
    return redirect(reverse('customer:profile'))

@login_required(login_url="/sign-in/?next=/customer/")
def profile_page(request):
    user_form = forms.BasicUserForm(instance=request.user)
    customer_form = forms.BasicCustomerForm(instance=request.user.customer)
    password_form = PasswordChangeForm(request.user)

    if request.method == "POST":
 
        if request.POST.get('action') == 'update_profile':
            user_form = forms.BasicUserForm(request.POST, instance=request.user)
            customer_form = forms.BasicCustomerForm(request.POST, request.FILES, instance=request.user.customer)

            if user_form.is_valid() and customer_form.is_valid():
                user_form.save()
                customer_form.save()

                messages.success(request, 'プロフィールが更新されました。')
                return redirect(reverse('customer:profile'))

        elif request.POST.get('action') == 'update_password':
            password_form = PasswordChangeForm(request.user, request.POST)
            if password_form.is_valid():
                user = password_form.save()
                update_session_auth_hash(request, user)

                messages.success(request, 'パスワードが更新されました。')
                return redirect(reverse('customer:profile'))

        elif request.POST.get('action') == 'update_phone':
            # Get Firebase user data
            firebase_user = auth.verify_id_token(request.POST.get('id_token'))

            request.user.customer.phone_number = firebase_user['phone_number']
            request.user.customer.save()
            messages.success(request, '電話番号が更新されました。')
            return redirect(reverse('customer:profile'))

    return render(request, 'customer/profile.html', {
        "user_form": user_form,
        "customer_form": customer_form,
        "password_form": password_form,
    })

@login_required(login_url="/sign-in/?next=/customer/")
def payment_method_page(request):
    current_customer = request.user.customer

    # Remove existing card
    if request.method == "POST":
        stripe.PaymentMethod.detach(current_customer.stripe_payment_method_id)
        current_customer.stripe_payment_method_id = ""
        current_customer.stripe_card_last4 = ""
        current_customer.save()
        return redirect(reverse('customer:payment_method'))

    # Save stripe customer infor
    if not current_customer.stripe_customer_id:
        customer = stripe.Customer.create()
        current_customer.stripe_customer_id = customer['id']
        current_customer.save()    

    # Get Stripe payment method
    stripe_payment_methods = stripe.PaymentMethod.list(
        customer = current_customer.stripe_customer_id,
        type = "card",
    )

    print(stripe_payment_methods)

    if stripe_payment_methods and len(stripe_payment_methods.data) > 0:
        payment_method = stripe_payment_methods.data[0]
        current_customer.stripe_payment_method_id = payment_method.id
        current_customer.stripe_card_last4 = payment_method.card.last4
        current_customer.save()
    else:
        current_customer.stripe_payment_method_id = ""
        current_customer.stripe_card_last4 = ""
        current_customer.save()


    if not current_customer.stripe_payment_method_id:
        intent = stripe.SetupIntent.create(
            customer = current_customer.stripe_customer_id
        )

        return render(request, 'customer/payment_method.html', {
            "client_secret": intent.client_secret,
            "STRIPE_API_PUBLIC_KEY": settings.STRIPE_API_PUBLIC_KEY,
        })
    else:
        return render(request, 'customer/payment_method.html')

@login_required(login_url="/sign-in/?next=/customer/")
def create_job_page(request):
    current_customer = request.user.customer

    if not current_customer.stripe_payment_method_id:
        return redirect(reverse('customer:payment_method'))
    
    return render(request, 'customer/create_job.html')



「core/templates/customer」フォルダに「create_job.html」ファイルを新規作成します。
作成した「core/templates/customer/create_job.html」ファイルを編集します。



新規作成 【Desktop/crowdsource/core/templates/customer/create_job.html】

{% extends 'base.html' %}
{% load bootstrap4 %}

{% block content %}
<div class="container mt-4">
  <div class="row">
    <!-- 左側 -->
    <div class="col-lg-4">
      <div class="card">
        <div class="card-header">
          仕事の概要
        </div>
        <div class="card-body">
          {% if not job %}
          <p>求人情報の概要がここに表示されます</p>
          {% else %}

          {% endif %}

        </div>
      </div>
    </div>

    <!-- 右側 -->
    <div class="col-lg-8">

      <!-- Step tabs -->
      <div class="card mb-5">
        <div class="card-body">


        </div>
      </div>

    </div>
  </div>
</div>

{% endblock %}



「core/templates/customer/base.html」ファイルを編集します。


記述編集 【Desktop/crowdsource/core/templates/customer/base.html】

{% extends 'base.html' %}
{% load static %}

{% block content %}

{% url 'customer:create_job' as create_job_url %}
{% url 'customer:profile' as profile_url %}
{% url 'customer:payment_method' as payment_method_url %}

<div class="container bg-light mt-4 pt-3 pb-3">
  <div class="row">
    <div class="col-lg-4">
      <div class="text-center">
        <img
          src="{% if request.user.customer.avatar %}{{ request.user.customer.avatar.url }}{% else %}{% static 'img/avatar01.jpg' %}{% endif %}"
          alt="" class="rounded-circle mb-2" width="150">
        <h4>{{ request.user.last_name }} {{ request.user.first_name }}</h4>
        <p class="text-secondary">{{ request.user.email }}</p>
      </div>

      <div class="list-group mt-5">
        <a href="{% url 'customer:create_job' %}" class="list-group-item list-group-item-info list-group-item-action {% if request.path == create_job_url %}active{% endif %}">仕事登録</a>
        <a href="#" class="list-group-item list-group-item-info list-group-item-action">自分の仕事</a>
        <a href="{% url 'customer:profile' %}" class="list-group-item list-group-item-info list-group-item-action {% if request.path == profile_url %}active{% endif %}">プロフィール</a>
        <a href="{% url 'customer:payment_method' %}" class="list-group-item list-group-item-info list-group-item-action {% if request.path == payment_method_url %}active{% endif %}">支払い方法</a>
      </div>

    </div>
    <div class="col-lg-8">
      {% block main %}

      {% endblock %}
    </div>
  </div>
</div>

{% endblock %}



ブラウザを確認します。
http://127.0.0.1:8000/customer/create_job/
クレジットカードが登録されていないと、create_jobページは表示できないようになってます。

仕事登録ページ
仕事登録ページ


↓↓クリックして頂けると励みになります。


23 | クレジットカード登録】 << 【ホーム】 >> 【25 | ページレイアウト

YAE C5 CLINIC(札幌美容クリニック)

関連記事(外部サイト)