0
所以我使用Django的用户模型如何(从django.contrib.auth.models导入用户),并通过使用的ModelForm(从Django中创建的模型形式。表单导入ModelForm)。当我将它显示在模板上时,它会显示在选择框上作为用户名。我想显示它是first_name和last_name。我们可以格式化为标准格式显示在Django模板
THISIS我在HTML
<form class="form-horizontal" method="post" role="form">
{% csrf_token %}
<fieldset>
<legend>{{ title }}</legend>
{% for field in form %} {% if field.errors %}
<div class="form-group">
<label class="control-label col-sm-2">{{ field.label }}</label>
<div class="controls col-sm-10">
{{ field }}
<p class="formError">
{% for error in field.errors %}{{ error }}{% endfor %}
</p>
</div>
</div>
{% else %}
<div class="form-group">
<label class="control-label col-sm-2">{{ field.label }}</label>
<div class="controls col-sm-10">
{{ field }} {% if field.help_text %}
<p class="help-inline"><small>{{ field.help_text }}</small></p>
{% endif %}
</div>
</div>
{% endif %} {% endfor %}
</fieldset>
<div class="form-actions" style="margin-left: 150px; margin-top: 30px;">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
感谢您的答复。如果User是另一个模型的foregin_key,怎么做?像模型表单是用于另一个模型,但用户是模型中的一个字段。 –
上述形式'MyModelForm'是对于具有一个外键'User'模型'MyModel'。自定义选择字段意味着,用户将显示为'的''而不是'。这不是你想要的吗?如果没有,请用一些代码更新您的答案,以帮助解释。 –
Alasdair
是的,这是我想要的。我有一个名为PermissionAssignment模型 –