<p>为什么不使用内置django Select?在</p>
<pre><code>from django import forms
>>> CHOICES = (('1', 'First',), ('2', 'Second',))
>>> choice_field = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES)
>>> choice_field.choices
[('1', 'First'), ('2', 'Second')]
>>> choice_field.widget.choices
[('1', 'First'), ('2', 'Second')]
>>> choice_field.widget.choices = ()
>>> choice_field.choices = (('1', 'First and only',),)
>>> choice_field.widget.choices
[('1', 'First and only')]
</code></pre>
<p><a href="https://docs.djangoproject.com/en/2.0/ref/forms/widgets/#widgets-inheriting-from-the-select-widget" rel="nofollow noreferrer">https://docs.djangoproject.com/en/2.0/ref/forms/widgets/#widgets-inheriting-from-the-select-widget</a></p>
<p><strong>编辑:</strong>当我看着你的代码时,你做了一件很奇怪的事情。。。您可以在模型中定义表单,并在表单中设置模型字段?我猜你把概念放错了</p>
<p><em>模型.py</em></p>
^{pr2}$
<p><em>表单.py</em>#在应用程序文件夹中创建此文件</p>
<pre><code>from django.forms import ModelForm
from myapp.models import Article
from .models import CharApp, RACE_CHOICES
# this is your form
class CharAppForm(ModelForm):
# Here you can change the widget or add new fields that is not related your model form
race = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES) #if you want to change the default form widget to this field
class Meta:
model = CharApp
fields = ['__all__'] # This will get all fields from your form, or just place each field you want to display
</code></pre>
<p>请记住,在视图.py在</p>
<pre><code>from .models import CharApp
from .forms import CharAppForm
# The function that will render your html or give you responses from your backend to your frontend
def your_view(request):
form = ConfigAppForm() # This will render your form with no value (so when pages render)
if request.method == "POST:
form = ConfigAppForm(request.POST) # This will bring the data that user have filled at your html and sended to your backend
if form.is_valid(): # Check if the form is valid and able to save
form.save() # will save the instance of your ConfigApp at your database
</code></pre>
<p>在你的html中你可以调用</p>
<pre><code><! This will render your entire form >
{{ form.as_p}}
<! Obs: If you need to add css class or something like it you can override the widget at your form.py and add this classes thought attr of your field >
</code></pre>
<p>以下是一些了解MVC(或django的MTV)概念的指南</p>
<p>MVC:<a href="https://softwareengineering.stackexchange.com/questions/127624/what-is-mvc-really">https://softwareengineering.stackexchange.com/questions/127624/what-is-mvc-really</a></p>
<p>MTV Django:<a href="https://docs.djangoproject.com/en/2.0/faq/general/" rel="nofollow noreferrer">https://docs.djangoproject.com/en/2.0/faq/general/</a></p>
<p>Django视图:<a href="https://docs.djangoproject.com/en/2.0/topics/http/views/" rel="nofollow noreferrer">https://docs.djangoproject.com/en/2.0/topics/http/views/</a></p>
<p>Django型号:<a href="https://docs.djangoproject.com/en/2.0/topics/db/models/" rel="nofollow noreferrer">https://docs.djangoproject.com/en/2.0/topics/db/models/</a></p>
<p>Django模型表单:<a href="https://docs.djangoproject.com/en/2.0/topics/forms/modelforms/" rel="nofollow noreferrer">https://docs.djangoproject.com/en/2.0/topics/forms/modelforms/</a></p>
<p><strong>编辑</strong></p>
<p>如果你想让你的代码运行而不做我之前说的任何事,就这么做吧</p>
<pre><code>VAMPIRE = 'VP'
DRAUGR = 'DR'
BAELNORN = 'BN'
LICH = 'LH'
SHADOW = 'SW'
RACE_CHOICES = (
('VP', 'Vampire'),
('DR', 'Draugr'),
('BN', 'Baelnorn'),
('LH', 'Lich'),
('SW', 'Shadow'),
class CharApp(forms.Form):
...
race = forms.Form(forms.ChoiceField( choices = RACE_CHOICES))
</code></pre>