django_countries 不显示在模板上的表单中
django_countries does not display in the form on template
我用了 {{ form }} 看到的 here
template.html
<h4>Change your details</h4><br>
<form id="edit_form" method='post'>
{% csrf_token %}
{{ form }}
<div class='section group'>
<input id="update_details_button" type='submit' class='btn btn-primary wide' value='Change'/>
</div>
</form>
views.py
def user_view(request, is_admin):
user = request.user
form = myForm()
if request.method == 'POST' and is_admin:
form = myForm(request.POST)
if form.is_valid():
data = form.cleaned_data
user.name = data['name']
user.country = data['country']
user.save()
messages.success(request, 'You have successfully updated your details.')
return render(request, 'mysite/user.html', {
'user': user,
'is_admin': is_admin,
'form': form,
})
我的表格如下
class myForm(forms.Form):
name = forms.CharField(
label="Name",
widget=forms.TextInput(attrs={'placeholder': 'Name'}))
country = CountryField(blank_label='(select country)')
def __init__(self, *args, **kwargs):
super(myForm, self).__init__(*args, **kwargs)
名称字段在页面上显示正常,但没有 CountryField 的迹象,有人可以指出错误吗?当服务器为 运行.
时,代码编译正常并且没有错误
CountryField
是模型域,不是表单域。您应该将它添加到您的用户模型中,在这种情况下,基于该模型的模型表单将自动生成一个国家/地区字段。由于看起来您实际上已经向用户模型添加了一个名为 country
的字段,因此您应该在此处使用 CountryField。
但是,作为参考,在非模型表单上手动操作稍微复杂一些:
from django_countries import widgets, countries
class myForm(forms.Form):
country = forms.ChoiceField(widget=CountrySelectWidget, choices=countries)
其实更简单:https://pypi.org/project/django-countries/#custom-forms
from django_countries.fields import CountryField
class MyForm(forms.Form):
country = CountryField(blank=True).formfield()
我用了 {{ form }} 看到的 here
template.html
<h4>Change your details</h4><br>
<form id="edit_form" method='post'>
{% csrf_token %}
{{ form }}
<div class='section group'>
<input id="update_details_button" type='submit' class='btn btn-primary wide' value='Change'/>
</div>
</form>
views.py
def user_view(request, is_admin):
user = request.user
form = myForm()
if request.method == 'POST' and is_admin:
form = myForm(request.POST)
if form.is_valid():
data = form.cleaned_data
user.name = data['name']
user.country = data['country']
user.save()
messages.success(request, 'You have successfully updated your details.')
return render(request, 'mysite/user.html', {
'user': user,
'is_admin': is_admin,
'form': form,
})
我的表格如下
class myForm(forms.Form):
name = forms.CharField(
label="Name",
widget=forms.TextInput(attrs={'placeholder': 'Name'}))
country = CountryField(blank_label='(select country)')
def __init__(self, *args, **kwargs):
super(myForm, self).__init__(*args, **kwargs)
名称字段在页面上显示正常,但没有 CountryField 的迹象,有人可以指出错误吗?当服务器为 运行.
时,代码编译正常并且没有错误CountryField
是模型域,不是表单域。您应该将它添加到您的用户模型中,在这种情况下,基于该模型的模型表单将自动生成一个国家/地区字段。由于看起来您实际上已经向用户模型添加了一个名为 country
的字段,因此您应该在此处使用 CountryField。
但是,作为参考,在非模型表单上手动操作稍微复杂一些:
from django_countries import widgets, countries
class myForm(forms.Form):
country = forms.ChoiceField(widget=CountrySelectWidget, choices=countries)
其实更简单:https://pypi.org/project/django-countries/#custom-forms
from django_countries.fields import CountryField
class MyForm(forms.Form):
country = CountryField(blank=True).formfield()