简体   繁体   English

Django:如何从自定义收音机选择中读取信息

[英]Django: How to read info from custom radio select in view

So yesterday I spent whole day customizing star-rating ("radio-button-ish").所以昨天我花了一整天的时间来定制星级(“radio-button-ish”)。 Now I don't want to abandon my css masterpiece (I am not much of a front-end guy so this was real pain for me), but it looks like that my approach to the problem wasn't correct.现在我不想放弃我的 css 杰作(我不是一个前端人员,所以这对我来说真的很痛苦),但看起来我解决这个问题的方法是不正确的。 Html code is quite simple: Html 代码非常简单:

<div class="col-md-5 mb-0 mt-3">
    <div id="rating">
        <input type="radio" name="star" id="star1"><label for="star1"></label>
        <input type="radio" name="star" id="star2"><label for="star2"></label>
        <input type="radio" name="star" id="star3"><label for="star3"></label>
        <input type="radio" name="star" id="star4"><label for="star4"></label>
        <input type="radio" name="star" id="star5"><label for="star5"></label>
    </div>
</div>

Now, is there a way to simply read from this choice and then use it in my view?现在,有没有办法简单地从这个选择中读取然后在我看来使用它? The situation is, normally I would probably use Field in form with choices but the design would be lost.情况是,通常我可能会以带有选择的形式使用 Field,但设计会丢失。 Do you see a simple solution?你看到一个简单的解决方案吗? Or is there a way of customizing a ChoiceField with widget RadioSelect into the same form, so i can use the name, id just like in above html code?或者有没有办法将带有小部件 RadioSelect 的 ChoiceField 自定义为相同的表单,这样我就可以像上面的 html 代码一样使用 name, id ?

Note: this is my first django project, pardon me if it is a complete nonsense or I missed something basic..注意:这是我的第一个 django 项目,如果它完全是胡说八道或者我错过了一些基本的东西,请原谅我。

you can put this code inside your form.您可以将此代码放入表单中。

here the field name is star.这里的字段名称是star。 so you if you access field value of star, then you should get the selected value所以你如果你访问 star 的字段值,那么你应该得到选定的值

small note: in your code this snippent 'id="rating"' is unnecessary小提示:在您的代码中,此代码段 'id="rating"' 是不必要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM