作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个表单清理方法,以确保输入的日期对当前未被模型中存储的日期范围覆盖。
所以我想捕获fromdate >= value <= todate
但是between
我的 clean 方法中的过滤器没有得到它。
有人可以提供一些启示吗?
模型.py
class Dates(models.Model):
fromdate = models.DateField()
todate = models.DateField()
表单.py
class Form(forms.Form):
fromdate = forms.DateField()
todate = forms.DateField()
class Meta:
model = Dates
def clean_fromdate(self):
form_fromdate = self.cleaned_data.get('fromdate')
exists = Dates.objects.filter(fromdate__contains=form_fromdate).exists()
if exists == True:
raise forms.ValidationError(_("A period is already entered for this date"))
between = Dates.objects.filter(fromdate__gte=form_fromdate,
todate__lte=form_fromdate).exists()
if between == True:
raise forms.ValidationError(_("Period already between this date 1"))
return form_fromdate
def clean_todate(self):
form_todate = self.cleaned_data.get('todate')
exists = Dates.objects.filter(todate__contains=form_todate).exists()
if exists == True:
raise forms.ValidationError(_("A period is already entered for this date"))
between = PeriodsOfInactivity2012.objects.filter(fromdate__gte=form_todate,
todate__lte=form_todate).exists()
if between == True:
raise forms.ValidationError(_("Period already between this date 2"))
return form_todate
最佳答案
这应该适合你
from django import forms
from models import Dates
class Form(forms.ModelForm):
fromdate = forms.DateField()
todate = forms.DateField()
class Meta:
model = Dates
def clean(self):
form_fromdate = self.cleaned_data.get('fromdate')
form_todate = self.cleaned_data.get('todate')
between = Dates.objects.filter(fromdate__gte=form_fromdate, todate__lte=form_todate).exists()
if between:
raise forms.ValidationError("Period already between this dates")
super(Form, self).clean()
<小时/>
编辑
#views.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.http import HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response
from forms import Form
def index(request, template='index.html'):
form = Form()
if request.method == "POST":
form = Form(request.POST)
if form.is_valid():
return HttpResponse(form)
kwvars = {
"form": form,
}
return render_to_response(template, kwvars, context_instance=RequestContext(request))
<小时/>
#forms.py
from django import forms
from models import Dates
class Form(forms.ModelForm):
fromdate = forms.DateField()
todate = forms.DateField()
class Meta:
model = Dates
def clean(self):
form_fromdate = self.cleaned_data.get('fromdate')
form_todate = self.cleaned_data.get('todate')
between = Dates.objects.filter(fromdate__gte=form_fromdate, todate__lte=form_todate).exists()
if between:
raise forms.ValidationError("Period already between this date 1")
super(Form, self).clean()
<小时/>
#index.html
<html>
<head>
</head>
<body>
<form action="" method="POST">
{% csrf_token %}
{% if form.errors %}
errors: {{ form.errors }}
{% endif %}
{{ form }}
<input type="submit" value="submit">
</form>
</body>
</html>
关于Django 表单字段清理以检查输入的日期是否在存储的范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13026689/
我尝试通过正则表达式将文本中的单引号更改为双引号。 (单字)示例:我走了。 You gona fly to planet 'Ziqtos' => 我需要在 I'm 中保留单引号,并在 You gona
我正在构建一个 API,其中大部分将包含 JSON 和 HTML 内容。但是一些非常具体的端点只呈现 true 或 false,并且还在 POST 中接受 true 或 false。这是请求或响应的整
我是一名优秀的程序员,十分优秀!