django 在保存所有其他输入时,不保存输入日期

rt4zxlrg  于 2023-08-08  发布在  Go
关注(0)|答案(2)|浏览(107)

提交表单/ POST后,未保存due_date输入。数据库中没有注册任何内容,但控制台中也没有显示错误。
除了due_date输入外,所有数据都被保存,我不知道错误在哪里。
我尝试了几百种可能性,但都失败了。我感谢你的帮助。谢谢你,谢谢!

型号

from datetime import date
from django.db import models
from django.contrib.auth.models import User

class Todo(models.Model):
    title = models.CharField(max_length=100)
    memo = models.TextField(blank=True)
    created = models.DateTimeField(auto_now_add=True)
    due_date = models.DateField(null=True, blank=True)
    datecompleted = models.DateTimeField(null=True, blank=True)
    important = models.BooleanField(default=False)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

字符串

表单

from django import forms
from .models import Todo

class TodoForm(forms.ModelForm):
    class Meta:
        model = Todo
        fields = ['title', 'memo', 'due_date', 'important']
        due_date = forms.DateField(
        widget=forms.DateInput(format='%m/%d/%Y'),
        input_formats=('%m/%d/%Y', )
        )

**查看 *

@login_required
def createtodo(request):
    if request.method == 'GET':
        return render(request, 'todo/createtodo.html', {'form':TodoForm()})
    else:
        try:
            form = TodoForm(request.POST)
            newtodo = form.save(commit=False)
            newtodo.user = request.user
            newtodo.save()
            
            return redirect('currenttodos')
        except ValueError:
            return render(request, 'todo/createtodo.html', {'form':TodoForm(), 'error':'Bad data passed in. Try again.'})

HTML表单

<form method="POST">
                {% csrf_token %}
                <div class="form-group">
                    <label for="title">Title</label>
                    <input type="text" name="title" class="form-control" id="title" required>
                </div>
                <div class="form-group">
                    <label for="due_date">Due</label>
                    <input type="date" class="form-control" id="due_date" value="{{todo.due_date}}">
                </div>
                <div class="form-group">
                    <label for="memo">Memo</label>
                    <textarea name="memo" class="form-control" id="memo" ></textarea>
                </div>
                <div class="form-group form-check">
                    <input type="checkbox" name="important" class="form-check-input" id="important">
                    <label class="form-check-label" for="important">Important</label>
                </div>
                <button type="submit" class="btn btn-primary">Save</button>
            </form>
`

8cdiaqws

8cdiaqws1#

问题是HTML日期输入标记中缺少 .value*,因此value="{{todo.due_date}}"应为value="{{todo.due_date.value}}"

wr98u20j

wr98u20j2#

尝试更改您的表单代码为下一个:

from django import forms
from .models import Todo

class TodoForm(forms.ModelForm):
    class Meta:
        model = Todo
        fields = ['title', 'memo', 'due_date', 'important']
        due_date = forms.DateField(
            input_formats=['%Y-%m-%d'],
            widget=forms.DateInput(
                attrs={
                    'type': 'date',
                    'class': 'form-control'
                },
                format='%Y-%m-%d'
            )
        )

字符串
因为HTML <input type='date'>元素接受下一个格式YYYY-mm-dd的日期值,所以请注意:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date#value

相关问题