如何在 django 中为 HttpRequest.GET 设置默认值?
我有一个网页,根据默认日期显示数据。然后,用户可以通过使用日期选择器选择日期并单击提交按钮来更改数据视图。我已经设置了一个变量,因此如果没有选择日期,则使用默认日期......那么问题是什么?如果用户尝试在不带参数的情况下输入 url 页面,问题就会出现...就像这样:
http://mywebpage/viewdata (example A)
而不是
http://mywebpage/viewdata?date= (example B)
我尝试使用:
if request.method == 'GET':
但显然,即使示例 A 仍然返回 true。我确信我犯了一些明显的初学者错误,但无论如何我都会问...除了将 url 传递给字符串并检查字符串中的“?date=”之外,是否有更简单的方法来处理示例 A?
I have a webpage that displays data based on a default date. The user can then change their view of the data by slecting a date with a date picker and clicking a submit button. I already have a variable set so that if no date is chosen, a default date is used.... so what's the problem? The problem comes if the user trys to type in the url page without a parameter... like so:
http://mywebpage/viewdata (example A)
instead of
http://mywebpage/viewdata?date= (example B)
I tried using:
if request.method == 'GET':
but apparently, even example A still returns true. I'm sure I'm doing some obvious beginner's mistake but I'll ask anyway... Is there a simpler way to handle example A other than passing the url to a string and checking the string for "?date="?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您提到您在某处定义了默认值。
与其做这样的事情:
这样做更容易:
You mentioned that you have default values defined somewhere.
Instead of doing something like this:
It's easier to do it this way:
我不太明白你的问题 - 更多代码会有帮助 - 但你不需要这样做:
I don't really understand your question - some more code would have helped - but don't you just need to do:
http://docs.djangoproject.com/en/dev/ref/request -response/
听起来你对 POST 感兴趣
http://docs.djangoproject.com/en/dev/ref/request-response/
It sounds you are interested in POST