English 中文(简体)
日期范围表
原标题:Django Form for date range

我正试图提出一个表格,让用户选择一个日期范围,在詹戈产生一个网络查询。 我认为,我错了过滤日期,我无法缩短日期。

我的<代码>form.py:

class ReportFiltersForm(forms.Form):
    start_date = forms.DateField(input_formats= %Y,%m,%d ,widget=SelectDateWidget())
    end_date = forms.DateField(input_formats= %Y,%m,%d ,widget=SelectDateWidget())

我的看法

if request.method ==  POST :
    form = ReportFiltersForm(request.POST)
    sdy = request.POST[ start_date_year ]
    sdm = request.POST[ start_date_month ]
    sdd = request.POST[ start_date_day ]
    edy = request.POST[ end_date_year ]
    edm = request.POST[ end_date_month ]
    edd = request.POST[ end_date_day ]
    start_date= datetime.date(sdy, sdm, sdd)
    end_date= datetime.date(edy, edm,edd)

追查

追查 (most recent call last):
File "/usr/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 651, in __call__
return self.application(environ, start_response)
File "/usr/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
response = self.get_response(request)
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 134, in get_response
return self.handle_uncaught_exception(request, resolver, exc_info)
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 154, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 92, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/projects/acms/cms/views.py", line 470, in eventreports
start_date= datetime.date(sdy, sdm, sdd)
TypeError: an integer is required
最佳回答

你在这里犯了许多错误。

首先,为了确定<代码>TypeError,您必须将数据输入<代码>int。 同样:

...
sdy = int(request.POST[ start_date_year ])
#Do the same with the other 5 fields

然而,这确实是做事的坏处。 一个人将不得不在每一领域周围设置<条码>/>除外区块,以确保用户进入立体,而不是体格。

既然你已经使用Django表格,那么为什么不能让你做所有的工作? 在这方面,你将如何这样做:

if request.method ==  POST :
    form = ReportFiltersForm(request.POST)
    if form.is_valid():
        start_date = form.cleaned_data[ start_date ]
        end_date = form.cleaned_data[ end_date ]
        #Do whatever you need to do with this data now. Since you had
        #defined the fields as DateFields, you will automatically get
        #datetime.date objects.
    else:
        return render_to_response( mytemplate.html , { form  : form})
....

这是出于多种原因做事的更好办法。 采用这一方式,你可以让Django谨慎地确认用户对你的投入。 此外,还将将这些数据输入<代码>日<>。 最后,如果用户输入无效数据,将允许他们向用户提供更好的错误信息。

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签