How can I set default values ​​in django for HttpRequest.GET?

I have a web page that displays data based on a default date. The user can then change their data view by selecting a date using a date picker and clicking the submit button. I already have a set of variables, so if no date is selected, the default date is used .... so what's the problem? The problem occurs if the user tries to enter the url page without a parameter ... for example:

http://mywebpage/viewdata (example A)

      

instead

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 have some obvious beginner error, but I'll ask anyway ... Is there an easier way to handle Example A other than passing the url string and checking the string for "? Date ="?

+2


a source to share


3 answers


I don't really understand your question - some other code would help - but you just don't need to do:



if 'date' in request.GET:

      

+3


a source


You mentioned that you have defaults 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 this:

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

      

+6


a source


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

Sounds like you are interested in POST

0


a source







All Articles