0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Django】SNS app作成手順02-サインアップページ作成2

Last updated at Posted at 2022-01-17

—————signup.htmlを編集----------

使うなら、その後base.htmlの作成。
継承する内容を記載する。

user登録・重複登録防止処理を記載する。

----------Base.html—————

<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<title>Hello, world!</title>

{%block header%}
{% endblock header%}

{% block content %}
{% endblock content %}

—————signup.html—————

{% extends 'base.html' %}

{%block content%}

{{error}}
<main class="form-signin">
  <form method='POST'>{% csrf_token %}
    <h1 class="h3 mb-3 fw-normal">Please sign in</h1>
    <div class="form-floating">
      <input type="text" class="form-control" id="floatingInput" name='username' placeholder="name@example.com">
      <label for="floatingInput">username</label>
    </div>
    <div class="form-floating">
      <input type="password" class="form-control" id="floatingPassword" name='password' placeholder="Password">
      <label for="floatingPassword">Password</label>
    </div>
    <button class="w-100 btn btn-lg btn-primary" type="submit">Sign in</button>
    <p class="mt-5 mb-3 text-muted">&copy; 2017–2021</p>
  </form>
</main>
{%endblock content%}

----------Views.py----------

from sqlite3 import IntegrityError
from django.shortcuts import render
from django.contrib.auth.models import User
from django.db import IntegrityError

Create your views here.

def signupfanc(request):
object_list = User.objects.all()
print(object_list)

if request.method == 'POST':
    username = request.POST['username']
    password = request.POST['password']
    
    try:
        user = User.objects.create_user(username,'',password)
    except IntegrityError:
        return render(request,'signup.html',{'error':'このユーザーはすでに登録済みです。'})
else:
    print('not POST')
    
return render(request,'signup.html',{})
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?