Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how does a html form work exactly?
#2
When you render the page and load the form for the first time is_favorite for all songs is False.
In the given example album has four songs. There is one radio control for each song. They have different ids and different value (the id of the song) but the same name ('song').
When you submit the form you post one key-value pair - {'song':song.id} where song.id is the respective id of the song (the value of the selected radio control).

Now your python function

    selected_song = album.song_set.get(pk=int(request.POST['song']))
    if selected_song.is_favourite:
        selected_song.is_favourite = False
    else:
        selected_song.is_favourite = True
    selected_song.save()
here it basically toggle the is_favourite property of the song - if it was True it will become False and vice-verse. This is important later on, because this is how you will in-favorite song that was previously favorite.

at the end of the python function you render the same page, however now the album that you pass has changed and one song is favorite. That is why the respective lines you identified correctly will execute and show a star next to the song. If you now submit the form with the same song selected, it will in-favorite it and next time page is rendered it will not display star next to it.

as a side note that
if selected_song.is_favourite:
    selected_song.is_favourite = False
else:
    selected_song.is_favourite = True
can be replaced with just
selected_song.is_favourite = not selected_song.is_favourite
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
how does a html form work exactly? - by mp3909 - Apr-01-2020, 12:17 PM
RE: how does a html form work exactly? - by buran - Apr-01-2020, 12:37 PM
RE: how does a html form work exactly? - by mp3909 - Apr-01-2020, 04:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Post HTML Form Data to API Endpoints Dexty 0 1,443 Nov-11-2021, 10:51 PM
Last Post: Dexty
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,733 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Using Python request without selenium on html form with javascript onclick submit but eraosa 0 3,233 Jan-09-2021, 06:08 PM
Last Post: eraosa
  Using python within an html form t4keheart 5 5,603 Aug-17-2020, 12:28 PM
Last Post: t4keheart
  Python3 + BeautifulSoup4 + lxml (HTML -> CSV) - How to loop to next HTML/new CSV Row BrandonKastning 0 2,406 Mar-22-2020, 06:10 AM
Last Post: BrandonKastning
  requests post/get to HTML form mrdominikku 1 2,356 Nov-03-2019, 07:12 PM
Last Post: Larz60+
  getting options from a html form pgoosen 5 3,330 Jul-03-2019, 06:07 PM
Last Post: nilamo
  How to send data from remotely hosted HTML form to Pi sajid 2 2,640 Jun-27-2019, 10:28 PM
Last Post: sajid
  how i save the html form to flask database mebaysan 1 7,341 Feb-07-2019, 12:56 AM
Last Post: snippsat
  Python....excel....html form tarliver 2 7,382 Dec-20-2017, 07:02 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020