Django: If form doesn't have information set value to

Issue

If value in interval is not selected, i would like to use default one ( 1d )

    # if  i got post request
    if request.method == "POST":
        #  check if interval is choosen in form 
        if 'interval' in request.POST:
            interval= request.POST["interval"]
        else:
            interval = "1d"
        if "ticker" in request.POST:
            ticker= request.POST["ticker"]

enter image description here

the value of intervals is not check properly if is there
enter image description here

Solution

You set this default value in frontend. In your html:

<option selected="selected">
1d
</option>

I would argue that it is the better option.

You can of course check for empty string in the interval keyword

if request.POST["interval"] == "":
    interval = "1d"

Answered By – ErikR

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published