LoginSignup
0
0

More than 3 years have passed since last update.

Djangoでbitflyerの収支管理アプリを作ってみる(03.ユーザー情報新規登録画面を作る。)

Last updated at Posted at 2019-12-30

前の。

Djangoでbitflyerの収支管理アプリを作ってみる(02_03.ログイン画面を作る③。)
https://qiita.com/redmeteor777/items/fd649baf43753185a5f7

今回は新規画面を作ります。
やることはあまり変わらないのでゴリゴリ書いていきましょう。

forms.pyを編集

forms.py
from django import forms

class LoginForm(forms.Form):
    user_id = forms.EmailField(
                        label='user_id',
                        max_length=30,
                        min_length=8,
                        required=True
                        )

    password = forms.CharField(
                        label='password',
                        max_length=30,
                        min_length=8,
                        required=True
                        )

class RegistrationForm(forms.Form):
    user_id = forms.EmailField(
                        label='user_id',
                        max_length=30,
                        min_length=8,
                        required=True
                        )

    password = forms.CharField(
                        label='password',
                        max_length=30,
                        min_length=8,
                        required=True
                        )

    api_key = forms.CharField(
                        label='api_key',
                        max_length=30,
                        min_length=8,
                        required=True
                        )

    api_secret = forms.CharField(
                        label='api_secret',
                        max_length=30,
                        min_length=8,
                        required=True
                        )

api_keyとapi_secretを登録できるようにします。

views.pyを編集する。

views.py
from django.shortcuts import render
from django.http import HttpResponse
from .forms import LoginForm
from .forms import RegistrationForm

def login(request):
    params = {
        'title': 'Login_Form',
        'message': 'input your e-mail and pass.',
        'form': LoginForm()
    }

    if (request.method == 'POST'):
        params['message'] = \
            'メールアドレス:' + request.POST['user_id'] + \
            '<br>パスワード:' + request.POST['password']
        params['form'] = LoginForm(request.POST)

    return render(request, 'bfmonitor/login.html', params)

def registration(request):
    params = {
        'title': 'Registration_Form',
        'message': 'input your e-mail, pass, API and Secret.',
        'form': RegistrationForm()
    }

    if (request.method == 'POST'):
        params['message'] = \
            'メールアドレス:' + request.POST['user_id'] + \
            '<br>パスワード:' + request.POST['password'] + \
            '<br>API_Key:' + request.POST['api_key'] + \
            '<br>API_Secret:' + request.POST['api_secret']
        params['form'] = RegistrationForm(request.POST)

    return render(request, 'bfmonitor/registration.html', params)

registration.htmlを作成する。

login.htmlと同じ階層に[registration.html]を作って編集します。
login.htmlをまるごとコピペで良いでしょう。
ダメでした、urlのとこを書き換える必要ありそうです。

registration.html

{% load static %}
<!doctype html>
<html lang="ja">
<head>
    <meta charset="uth-8">
    <title>{{title}}</title>
    {% comment %} <link rel="stylesheet" type="text/css"
        href="{% static 'login/css/style.css' %}" /> {% endcomment %}
</head>
<body>
    <h1>{{title}}</h1>
    <p>{{message|safe}}</p>
    <table>
        <form action="{% url 'registration' %}" method="post">
            {% csrf_token %}
            {{ form.as_table }}
            <tr><td></td><td><input type="submit" value="click"></td></tr>
        </form>
    </table>
</body>
</html>

urls.pyを編集。

urls.py
ntrib import admin
from django.urls import path
import login.views as login

urlpatterns = [
    path('admin/', admin.site.urls),
    path('login/', login.login, name='login'),
    path('registration/', login.registration, name='registration'),
]

path('registration/', login.registration),

path('registration/', login.registration, name='registration'),

と変更しました。

ではサーバーを起動して確認してみましょう。

>python manage.py runserver

ユーザー登録画面にアクセスして表示を確認します。

http://localhost:8000/registration/

APIキー、シークレットの入力フィールドが表示されていればOKです。
次回はダッシュボード画面を作成します。

やっっっっとビッフラのAPI叩ける!!!やったね!!!!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0