—————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">© 2017–2021</p>
</form>
</main>
----------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',{})