开发者

How do set default values in django for an HttpRequest.GET?

开发者 https://www.devze.com 2022-12-29 16:03 出处:网络
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

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="?


You mentioned that you have default values defined somewhere.

Instead of doing something like this:

if 'date' in request.GET:
    date = request.GET['date']
else:
    date = '2010-05-04'

It's easier to do it this way:

date = request.GET.get('date', '2010-05-04')


I don't really understand your question - some more code would have helped - but don't you just need to do:

if 'date' in request.GET:


http://docs.djangoproject.com/en/dev/ref/request-response/

It sounds you are interested in POST

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号