开发者

How to access a form field's custom attribute at save time in Django?

Should be awarding double points for this one; I think it's going to开发者_JAVA技巧 be tricky.

Say for example I had a form field which looked like this:

<li><label for="id_question_3_1"><input name="question_3" question_id="3" type="radio" class="inlinechoice" value="4" id="id_question_3_1" /> Agree</label></li>

And I really really had to put that custom attribute "question_id" into my response that I was saving into the database. How would I go about grabbing it? I can't find an example of anyone needing to do this, so can't crib from anyone!

If it's not possible, how do I go about doing it?

I'm building a dynamic form, from some (usually 5 but not necessarily) questions stored in the database. I need to store the answers along with a reference to the question it refers to.

I've got a response model to store, with a fk to the question, but need to get that custom attr out of the form data that is submitted.


What's the purpose of that question_id attribute? Obviously that attribute itself isn't going to come back in the request, unless...

  • Parse the name=question_{id} of the input field (if this is the id) which implies you don't really need the question_id attribute
  • Server-side, generate a hidden form field with name=question_id and value=3 using your template.
  • Client-side, generate a hidden form field with some javascript. Not sure I like this one though, since it relies on the client having javascript.

Hidden form fields are normally the way to go here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜