前提・実現したいこと
Djangoの学習で名前・年齢・性別・部署を登録できるようになったのですが
登録後フォームを空白にして表示させたいのですが
現状入力フォーム自体が消えてしまっています。
空白の入力フォームを表示させるにはどうすればいいですか?
発生している問題・エラーメッセージ
登録前 https://gyazo.com/09422226c80ec5daf860b4000dba2227 登録後 https://gyazo.com/d33a40d19980d128098234ac4a411526 DBへは保存されています。 登録ボタンに関しては無視してください。
該当のソースコード
Django
1views.py 2 3from django.urls import reverse 4from django.http import HttpResponse, HttpResponseRedirect 5from django.views.decorators.http import require_http_methods 6from django.shortcuts import render 7from django.template import loader 8from .form import RegistrationForm 9 10from .models import User 11 12def index(request): 13 return render(request, 'employee/index.html') 14 15@require_http_methods(['GET', 'POST']) 16def registration(request): 17 if request.method == 'GET': 18 params = {'employee_name': '','age': '',"sex": '','department': ''} 19 context = {'form': RegistrationForm(initial=params)} 20 else: 21 form = RegistrationForm(request.POST) 22 if form.is_valid(): 23 registration = User( 24 employee_name=form.cleaned_data["employee_name"], 25 age=form.cleaned_data["age"], 26 sex=form.cleaned_data["sex"], 27 department=form.cleaned_data["department"], 28 ) 29 registration.save() 30 return render(request, 'employee/registration.html') 31 return HttpResponseRedirect(reverse('employee:registration')) 32 33 template = loader.get_template('employee/registration.html') 34 return HttpResponse(template.render(context, request)) 35 36def show(request): 37 return render(request, 'employee/show.html') 38 39def update(request, question_id): 40 return HttpResponse("You're voting on question %s." % question_id)
Django
1form.py 2 3from django import forms 4from django.core import validators 5 6list_department = [(0,""),(1,"管理"),(2,"システム開発"),(3,"営業")] 7list_sex = [("start",""),(True,"男性"),(False,"女性")] 8 9class RegistrationForm(forms.Form): 10 employee_name = forms.CharField(label='担当者名',max_length=50) 11 age = forms.IntegerField(label='年齢',validators=[validators.MinValueValidator(1), validators.MaxValueValidator(200),]) 12 sex = forms.ChoiceField(choices=list_sex,label='性別') 13 department = forms.ChoiceField(choices=list_department,label='部署') 14 15 def __init__(self,*args,**kwargs): 16 super().__init__(*args,**kwargs) 17 self.label_suffix=" " 18 19 def clean(self): 20 cleaned_data = super().clean() 21 return cleaned_data 22 23 def clean_employee_name(self): 24 employee_name = self.cleaned_data['employee_name'] 25 return employee_name 26 27 def clean_age(self): 28 age = self.cleaned_data['age'] 29 return age 30 31 def clean_sex(self): 32 sex = self.cleaned_data['sex'] 33 return sex 34 35 def clean_department(self): 36 department = self.cleaned_data['department'] 37 return department
Django
1urls.py 2 3from django.urls import path 4 5from . import views 6 7urlpatterns = [ 8 path('', views.index, name='index'), 9 path('show/', views.show, name='show'), 10 path('<int:registration_id>/update/', views.update, name='update'), 11 path('registration/', views.registration, name='registration'), 12]
html
1{% load static %} 2<link rel="stylesheet" type="text/css" href="{% static 'employee/reset.css' %}"> 3<link rel="stylesheet" type="text/css" href="{% static 'employee/style.css' %}"> 4<link rel="stylesheet" type="text/css" href="{% static 'employee/registration.css' %}"> 5 6<div class = top> 7 <h1>登録</h1> 8 9 {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} 10 11 <div class = form> 12 <div class = registration-form> 13 <form action="" method="POST"> 14 {% csrf_token %} 15 {% for value in form %} 16 <div class = form-content> 17 <p>{{ value.label }}</p> 18 <p>{{ value }}{{ value.errors }}</p> 19 </div> 20 {% endfor %} 21 <button type="submit" class="btn">登録</button> 22 </form> 23 </div> 24 <div class = move-btn> 25 <div><a href="/employee/" class = btn>メニュー</a></div> 26 <div><a href="/employee/show/" class = btn>登録</a></div> 27 </div> 28 </div> 29</div>
補足情報(FW/ツールのバージョンなど)
Windows10
VitualStudioCode
Python 3.9.4
Django 3.2.3
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。