#πŸ”’ HTML Dropdown Form/Python and Discord Information Issues

35 messages Β· Page 1 of 1 (latest)

mellow kiln
#

My HTML form creates a dropdown menu when a checkbox is enabled. It lets me select a channel (from my discord server) and I hit the save button to save the channel id to a database. When the page refreshes, the check box is enabled, but the drop down box is blank, it wont retain the selected option. Because of this, if I hit save again, it clears the database entry because the entry is technically blank. How do I make it retain the selected option after I hit save and the page reloads?

Here is the HTML
https://paste.pythondiscord.com/MD6Q

Endpoint/Database Code
https://paste.pythondiscord.com/IDAA

I am pretty sure my python code is okay, just hoping someone might know if im missing something with the html (I know this is a python help thread).

mental elbowBOT
#

@mellow kiln

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mellow kiln
#

Any help is much appreciated

sweet comet
#

How do you determine in your backend code which channel is selected?

mellow kiln
sweet comet
#

So as the page renders, the ID of that channel is "{{ logging_settings[event + '_id'] }}"?

mellow kiln
#

my confusion is, I can get it to do all that, but I cant make the selection remain in the html page when it rloads

mellow kiln
#

I do wanna verify one thing, one moment

#

im looking at it though, and I wonder if the names are off

sweet comet
#

Alrighty then, that's relatively simple. πŸ™‚
To have an option pre-selected, add selected="selected" (or just selected attribute by itself, either way) into the <option> tag for that option:

<option value="" selected></option>

To figure out which to do that on in the template, you have channel.id and this logging_settings[event + '_id']. So they should be equal.
Wrap that in an if (I assume this is Jinja?):

<option value="{{ channel.id }}"{% if channel.id == logging_settings[event + '_id'] %} selected{% endif %}>{{ channel.name }}</option>
mellow kiln
#

I wonder if I need to do something like whats below it

                <option value="{{ channel.id }}"{% if channel.id == logging_settings[event + '_id'] %} selected{% endif %}>{{ channel.name }}</option>
                {% for channel in channels %}
                    <option value="{{ channel.id }}">{{ channel.name }}</option>
                {% endfor %}```
sweet comet
#

Right, inside the {% for loop you have there, that's where channel is created.

#

On the first blank option, that would not apply.

#

Basically it's generating lines in a loop.

<option value="1"></option>
<option value="2"></option>
<option value="3"></option>

Suppose the logging_settings event ID results in 2, then that second line gets selected according to the if:

<option value="1"></option>
<option value="2" selected></option>
<option value="3"></option>

When the full page renders, the option is pre-selected in the select element.

#

Takes advantage of plain HTML features by writing it out with Jinja templating πŸ™‚

mellow kiln
# sweet comet Takes advantage of plain HTML features by writing it out with Jinja templating οΏ½...

originally before I had removed <option value=""></option> and just left

                {% for channel in channels %}
                    <option value="{{ channel.id }}">{{ channel.name }}</option>
                {% endfor %}```

and it enabled all check boxes and set the first channel as default for everything (not what I wanted)

For you suggestion, are you saying I should replace all of this
            <option value=""></option>
            {% for channel in channels %}
                <option value="{{ channel.id }}">{{ channel.name }}</option>
            {% endfor %}```

Of just the blank option

#

becuase replace either or seems to error (unless im missing something obvious)

sweet comet
#

No, replace the part inside the loop:

<option value=""></option>
{% for channel in channels %}
  <option value="{{ channel.id }}"{% if channel.id == logging_settings[event + '_id'] %} selected{% endif %}>{{ channel.name }}</option>
{% endfor %}
#

The {% if channel.id == logging_settings[event + '_id'] %} selected{% endif %} part is just added into the middle of it, as you see. Everything else on that line is correct πŸ™‚

mellow kiln
#

same thing it looks like

sweet comet
#

I think there's a type mismatch. The Jinja template runner will see them as ints and strings, and it looks like you're storing selected_channel_id as an int, while in logging_settings it's a string.

#
<option value=""></option>
{% for channel in channels %}
  <option value="{{ channel.id }}"{% if str(channel.id) == str(logging_settings[event + '_id']) %} selected{% endif %}>{{ channel.name }}</option>
{% endfor %}
mellow kiln
#
{
    "detail": "'str' is undefined"
}```
mellow kiln
#

@acoustic night

#

just saw your message in chat

#

adding you

#

I appreicate it

mental elbowBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.