前提・実現したいこと
プログラム勉強している学生です。
djangoを用いてウェブサイトを作成しています。
次のフォームを作成したのですが、フォームを入力し、保存ボタンを押した時、データがデータベースに更新されません
どこを修正すればよいかご教授ください
該当のソースコード
views.pyの内容
from django.shortcuts import render, redirect from django.contrib.auth.models import User from .forms import ProfileForm from .forms import UserForm from django.contrib.auth import login from .models import Profile from django.http import HttpResponse,HttpResponseRedirect from django.urls import reverse def setting_up(request, user_id=None): user = User.objects.get(username=request.user.username) profile = Profile.objects.get(user=request.user) if request.method == "POST": user_form = UserForm(request.POST) profile_form = ProfileForm(request.POST) if user_form.is_valid() * profile_form.is_valid(): user_cd = user_form.cleaned_data profile_cd = profile_form.cleaned_data user.last_name = user_cd['last_name'] profile.address = profile_cd['address'] profile.post = profile_cd['post'] user.first_name = user_cd['first_name'] user.save() profile.save() return HttpResponseRedirect('/accounts/7/setting/') else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(initial={"post":profile.post ,"address":profile.address }) return render(request, 'accounts/setting_up.html', {'user_form': user_form, 'profile_form': profile_form,'user': user, 'profile': profile})
urls.pyの内容
path('<int:user_id>/setting/', views.setting, name='setting'), path('<int:user_id>/setting_up/', views.setting_up, name='setting_up'),
htmlの内容
<form class="form-horizontal" action="" method="POST">{% csrf_token %} <table class="table table-bordered m-5"> <body> <tr> <td class="align-middle">ユーザー名</td> <td> {{user.username}}</td> </tr> <tr> <td class="align-middle">性</td> <td colspan="2">{{user_form.last_name}}</td> </tr> <tr> <td class="align-middle">名</td> <td>{{user_form.first_name}}</td> </tr> <tr> <td class="align-middle">性別</td> <td> {% if profile.gender_cd == 0 %}男{% endif %} {% if profile.gender_cd == 1 %}女{% endif %} {% if profile.gender_cd == 2 %}その他{% endif %}</td> </tr> <tr> <td class="align-middle">生年月日</td> <td>{{profile.birthday}}</td> </tr> <tr> <td class="align-middle">メールアドレス</td> <td>{{user.email}}</td> </tr> <tr> <td class="align-middle">郵便番号</td> <td>{{profile_form.post}}</td> </tr> <tr> <td class="align-middle">住所<br></td> <td>{{profile_form.address}}</td> </tr> {{ form.as_p }} <input type="submit" class="btn btn-primary btn-lg" value="保存"> </form> {% endblock %}
models.pyの内容
from django.conf import settings from django.db import models from django.contrib.auth.models import User # Create your models here. class Profile(models.Model): GENDER_CHOICES=( (0,'男性'), (1,'女性'), (2,'その他'), ) address = models.CharField(max_length=128,verbose_name="住所") gender_cd = models.SmallIntegerField(choices=GENDER_CHOICES,verbose_name="性別") birthday = models.DateField(verbose_name="生年月日") post = models.CharField(max_length=7,verbose_name="郵便番号") balance = models.SmallIntegerField(default=0, verbose_name="残高") user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True) class Comments(models.Model): comments = models.CharField(max_length=200,verbose_name="コメント")
formタグのactionにurlを指定しないのは意図して行っていますか?