下記のようなコードで投稿できるプログラムを作ったのですが、このコードで投稿ボタンを押すとform.is_valid()の結果がFalseになっているのかなぜかindexページに飛ばされ投稿ができません。
ターミナルでは普通に"POST /post/new/ HTTP/1.1" 302 0
と正常にリダイレクトされたようなのですが何が原因で投稿に失敗するのでしょうか。
詳しい方教えていただけると幸いです。
models.py
django
1from django.db import models 2from django.conf import settings 3from django.utils import timezone 4 5 6class Category(models.Model): 7 COLOR_CHOICES = ( 8 ('#D50000','Red'), 9 ('#D94182','Pink'), 10 ('#E36A00','Orange'), 11 ('#F6BF26','Yellow'), 12 ('#0B8043','Green'), 13 ('#258C6D','ForestGreen'), 14 ('#039BE5','Cyan'), 15 ('#1169EB','Blue'), 16 ('#8E24AA','Purple'), 17 ('#AA6402','Brown'), 18 ('#616161','Gray'), 19 ('#000000','Black'), 20 ('#FFFFFF','White'), 21 ) 22 color = models.CharField(max_length=100, choices=COLOR_CHOICES) 23 name = models.CharField(max_length=150) 24 user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) 25 created_at = models.DateTimeField(default=timezone.now) 26 27 def __str__(self): 28 return self.name 29 30class Project(models.Model): 31 title = models.CharField(max_length=255) 32 category = models.ForeignKey(Category, on_delete=models.PROTECT) 33 description = models.CharField(max_length=400,blank=True) 34 user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) 35 created_at = models.DateTimeField(default=timezone.now) 36 37 def __str__(self): 38 return self.title 39 40class Record(models.Model): 41 project = models.ForeignKey(Project, on_delete=models.PROTECT) 42 time = models.DurationField() 43 user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) 44 created_at = models.DateTimeField(default=timezone.now) 45 46 def __str__(self): 47 return self.project.title
forms.py
Django
1from django import forms 2from .models import Category, Record, Project 3from django.forms import ModelForm 4 5 6class RecordForm(forms.ModelForm): 7 class Meta: 8 model = Project 9 fields = ("title","category","description","user") 10 11 project = forms.ModelChoiceField( 12 queryset=Project.objects.all(), 13 widget=forms.Select, 14 empty_label=None 15 ) 16 time = forms.CharField(label="時間")
views.py
Django
1from django.shortcuts import render, redirect 2from django.views.generic import View 3from .models import Category, Record, Project 4from .forms import RecordForm 5from django.contrib.auth.mixins import LoginRequiredMixin 6 7class IndexView(View): 8 def get(self, request, *args, **kwargs): 9 post_data = Record.objects.order_by('-id') 10 return render(request, 'record/index.html', { 11 'post_data': post_data 12 }) 13 14 15 16class CreateRecordView(LoginRequiredMixin ,View): 17 def get(self, request, *args, **kwargs): 18 form = RecordForm(request.POST or None) 19 return render(request, 'record/post_form.html', { 20 'form': form 21 }) 22 23 def post(self, request, *args, **kwargs): 24 form = RecordForm(request.POST) 25 if form.is_valid(): 26 post = form.save() 27 post.user = request.user 28 post.time = form.cleaned_data['time'] 29 post.project = form.cleaned_data['project'] 30 post.save() 31 return redirect('new') 32 else : 33 form = RecordForm() 34 return redirect('index')
post_form.html
html
1{% extends 'record/base.html' %} 2{% load widget_tweaks %} 3 4{% block content %} 5<div class="post-form-main"> 6 7<div class="post-form"> 8<div class="post-form-title">Create a post</div> 9<form method="post"> 10 {% csrf_token %} 11 <div> 12 <select id="tag" class="form-control" name="tag"> 13 <option hidden>Choose a project</option> 14 {% for project in form.project %} 15 <option value="{{ project }}"></option> 16 {% endfor %} 17 </select> 18 <div class="content-input">{% render_field form.time placeholder="時間"%}</div> 19 <button class="btn" type="submit">Post</button> 20 </div> 21</form> 22</div> 23</div> 24{% endblock %}
あなたの回答
tips
プレビュー