It should be so simple. I'm following all the rules and all the answers from previous questions but it still won't happen. I have a Bottle model, that has it's own inline formset form to it's parent Wine. The date on which I bought the bottle, should show up as a standard date picker. I cannot for the life of my figure out where it goes wrong. The input type should change from "text" to "date", but it won't.
事情本应如此简单。我正在遵循所有的规则和前面问题的所有答案,但它仍然不会发生。我有一个瓶子模型,它有它自己的内联Formset形式到它的母公司Wine。我买瓶子的日期应该会显示为标准的日期选择器。我无论如何也想不出哪里出了问题。输入类型应该从“Text”更改为“Date”,但不会。
I'm using crispy forms and bootstrap 5, but disabling those also does nothing.
我使用的是易碎的表单和bootstrap 5,但禁用它们也没有任何作用。
My models (only relevant models shown):
我的型号(仅显示相关型号):
from django.db import models
from django.conf import settings
from django.utils import timezone
from django.contrib import admin
from django.forms import ModelForm
class Wine(models.Model):
producer = models.ForeignKey(Producer, null=True, blank=True, on_delete=models.CASCADE, default=0)
name = models.CharField(max_length=200)
year = models.IntegerField(blank=True, null=True)
alcohol = models.FloatField(blank=True, null=True)
grape = models.ManyToManyField(Grape)
colour = models.ForeignKey(Colour, null=True, blank=True, on_delete=models.CASCADE, default=0)
wine_type = models.ForeignKey(WineType, null=True, blank=True, on_delete=models.CASCADE, default=0)
notes = models.TextField(max_length=2000, blank=True, null=True)
wishlist = models.BooleanField(default=False)
def __str__(self):
return self.name
def get_bottles(self):
a = []
for bottle in self.Bottle.all():
a.append((bottle.date_bought))
return a
def available_bottle(self):
number = self.Bottle.filter(available=True).count()
return number
class Bottle(models.Model):
wine = models.ForeignKey(Wine, related_name='Bottle', on_delete=models.CASCADE)
date_bought = models.DateField(null=True)
bottleprice = models.CharField(max_length=200, blank=True,)
#notes = models.TextField(max_length=2000, blank=True, null=True)
available = models.BooleanField(default=True)
drank_on = models.DateField(blank=True, null=True)
def __str__(self):
return str(self.date_bought)
def save(self, *args, **kwargs):
if self.drank_on is not None:
self.available=False
super(Bottle, self).save(*args, **kwargs)
My Views:
我的观点是:
from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
from .forms import WineForm, BottleForm
from django.forms import inlineformset_factory
from django.views.generic import ListView, DetailView, CreateView
from .models import Wine, Bottle, WineType, Country
def wine_list(request):
wines = Wine.objects.order_by('name')
context = {
'wines': wines,
# 'available_bottles': Bottle.objects.filter(available=True).count()
}
return render(request, 'winedb/wine_list.html', context)
def wine_detail(request, pk):
wine = get_object_or_404(Wine, id=pk)
bottles = wine.Bottle.all()
context = {'wine': wine,
'bottles': bottles}
return render(request, 'winedb/wine_detail.html', context)
def add_new_bottle(request, pk):
WineFormSet = inlineformset_factory(Wine, Bottle, fields=('date_bought', 'bottleprice', 'available', 'drank_on'))
wine = Wine.objects.get(id=pk)
formset = WineFormSet(instance=wine)
# form = BottleForm(initial={'wine':wine})
if request.method == "POST":
form = BottleForm(request.POST)
if form.is_valid():
wine = form.save(commit=False)
wine.save()
return redirect('/')
context = {'formset':formset}
return render(request, 'winedb/bottle_form.html', context)
def add_new_wine(request):
if request.method == "POST":
form = WineForm(request.POST)
if form.is_valid():
wine = form.save(commit=False)
wine.save()
return redirect('wine_detail', id=wine.pk)
else:
form = WineForm()
return render(request, 'winedb/wine_edit.html', {'form': form})
The form:
表格:
from django import forms
from datetime import datetime
from django.forms import ModelForm
from .models import Wine, Bottle
class WineForm(forms.ModelForm):
class Meta:
model = Wine
fields = ('producer', 'name', 'year', 'alcohol', 'grape', 'colour', 'wine_type', 'notes', 'wishlist')
class BottleForm(forms.ModelForm):
date_bought = forms.DateField(widget=forms.DateInput(attrs={'type':'date'}))
def __init__(self, *args, **kwargs):
super().__init__(*args, *kwargs)
class Meta:
model = Bottle
fields = ('wine', 'date_bought', 'bottleprice', 'available', 'drank_on')
widgets= {
'date_bought': forms.DateInput(
attrs={'type':'date'}),
}
And the template:
和模板:
{% extends 'winedb/base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<h2>New Bottle</h2>
<form method="POST" class="post-form">{% csrf_token %}
{% for form in formset%}
{{ form }}
<hr>
{% endfor %}
<button type="submit" class="save btn btn-secondary">Save</button>
</form>
{% endblock %}
I have tried literally anything. From changing the format, to changing the language from US to UK in my settings and using custom widgets like:
我几乎什么都试过了。从更改格式,到在我的设置中将语言从美国更改为英国,并使用以下自定义小部件:
class DateInput(forms.DateInput):
input_type = 'date'
更多回答
我是一名优秀的程序员,十分优秀!