Django Forms ChoiceField 没有显示选项;仍然隐藏
Django Forms ChoiceField is not showing options; remains hidden
views.py
def rank(request):
if request.method == 'POST':
form = RankRegisterForm(request.POST or None)
if form.is_valid:
form.user = request.user
form.save(commit=False)
return redirect('matches/rank.html')
else:
form = RankRegisterForm()
return render(request, 'matches/rank.html', {'form': form})
forms.py
class RankRegisterForm(forms.ModelForm):
rank = forms.ChoiceField(choices=rankChoices)
class Meta:
model = Rank
fields = (
"rank",
)
html
{% extends 'base.html' %}
{% block content %}
{% if user.is_authenticated %}
<div class="container">
<p> Belt Rank Status </p>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button class="red-text text-darken-1" type="submit">Submit</button>
<br></br>
</form>
</div>
{% endif %}
{% endblock content %}
models.py
class Rank(models.Model):
user = models.OneToOneField(
CustomUser,
on_delete=models.CASCADE,
primary_key=True,
)
rank = models.CharField("rank", max_length=20, choices=rankChoices)
rankPoints = models.IntegerField("rank points", default=0)
certificateOfLegitimacy = models.BooleanField(default=False)
promoted = models.BooleanField(default=False)
def __str__(self):
return self.rank + ", " + self.user.email
DOM
<form method="POST">
<input type="hidden" name="csrfmiddlewaretoken" value="ITC0cjPUCmvhuYD2K1eDgjPOt1daSRJbi8mbpLmv6ETGVe9akMI2SOfjEJQcXJ9A">
<p>
<label for="id_rank">Rank:</label>
<select name="rank" id="id_rank">
白色的
蓝色的
紫色的
棕色的
黑色的
谢谢,这很糟糕。我一直在到处寻找,没有一个与我相关的问题得到解答。它是隐藏的,我怀疑这是因为 csrf 狗屎。但是我不知道。帮一个兄弟。
尝试将 Meta
class 和 rank
变量缩进 RankRegisterForm
class
#forms.py
class RankRegisterForm(forms.ModelForm):
rank = forms.ChoiceField(choices=rankChoices)
class Meta:
model = Rank
fields = (
"rank",
)
views.py
def rank(request):
if request.method == 'POST':
form = RankRegisterForm(request.POST or None)
if form.is_valid:
form.user = request.user
form.save(commit=False)
return redirect('matches/rank.html')
else:
form = RankRegisterForm()
return render(request, 'matches/rank.html', {'form': form})
forms.py
class RankRegisterForm(forms.ModelForm):
rank = forms.ChoiceField(choices=rankChoices)
class Meta:
model = Rank
fields = (
"rank",
)
html
{% extends 'base.html' %}
{% block content %}
{% if user.is_authenticated %}
<div class="container">
<p> Belt Rank Status </p>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button class="red-text text-darken-1" type="submit">Submit</button>
<br></br>
</form>
</div>
{% endif %}
{% endblock content %}
models.py
class Rank(models.Model):
user = models.OneToOneField(
CustomUser,
on_delete=models.CASCADE,
primary_key=True,
)
rank = models.CharField("rank", max_length=20, choices=rankChoices)
rankPoints = models.IntegerField("rank points", default=0)
certificateOfLegitimacy = models.BooleanField(default=False)
promoted = models.BooleanField(default=False)
def __str__(self):
return self.rank + ", " + self.user.email
DOM
<form method="POST">
<input type="hidden" name="csrfmiddlewaretoken" value="ITC0cjPUCmvhuYD2K1eDgjPOt1daSRJbi8mbpLmv6ETGVe9akMI2SOfjEJQcXJ9A">
<p>
<label for="id_rank">Rank:</label>
<select name="rank" id="id_rank">
白色的
蓝色的
紫色的
棕色的
黑色的
谢谢,这很糟糕。我一直在到处寻找,没有一个与我相关的问题得到解答。它是隐藏的,我怀疑这是因为 csrf 狗屎。但是我不知道。帮一个兄弟。
尝试将 Meta
class 和 rank
变量缩进 RankRegisterForm
class
#forms.py
class RankRegisterForm(forms.ModelForm):
rank = forms.ChoiceField(choices=rankChoices)
class Meta:
model = Rank
fields = (
"rank",
)