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

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 1:[1]

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"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ErikR