[Fixed]-Django – a form input field doesn't get posted

0πŸ‘

βœ…

Ok, so I went with a totally different approach that works for me, posting as an answer for future generations πŸ™‚

What I did was to create a new url to post to, instead of posting to the same url.
Then I simply provide more parameters to the post function in my view.

So the 2 urls for the same view:

url(r'^show/(?P<show_name>.+)/episodes', ShowDetail.as_view(), name='show-detail'),
url(r'^show/(?P<show_name>.+)-(?P<episode_season>\d+)-(?P<episode_number>\d+)',
    ShowDetail.as_view(), name='show-detail-update')

Then on the page I simply post to the newly created url:

<td><form id='episodes-{{ episode.title }}' action="{% url 'shows_app:show-detail-update' show_name episode.season episode.number %}" method="post">
                            {% csrf_token %}
                            <input type="checkbox" class="episode-check" id="{{ episode.title }}" {% if episode.watched %}checked="checked" {% endif %}/>
                            <label for="{{ episode.title }}"></label>
     </form>
</td>

And the view itself:

class ShowDetail(View):
    def get(self, request, show_name):
        # handling the get request

    def post(self, request, show_name, episode_season, episode_number):
        # handling the post request
        # referring the user the the same page
        return self.get(request, show_name)

It still feels like a bit of a hack, but it works.

If anyone has a better suggestion I’d be glad to hear it.

πŸ‘€DeepSpace

1πŸ‘

First, you have created multiple forms with the same id:

<form id='episodes' action="" method="post" enctype="multipart/form-data">

You can forgo form ids entirely and use a JQuery class selector:

<input type="checkbox" class="episode-check" {% if episode.watched %}checked="checked" {% endif %}/>

and

$(".episode-check").change(function() {
    this.closest('form').submit();
}

…to access the parent form element like so. Then call submit on the correct form (you should still have unique ids if you are giving your forms id’s).

πŸ‘€frederix

0πŸ‘

You might want to refer this link to understand how html elements are processed and passed through the request object.

πŸ‘€rkatkam

Leave a comment