当我加载文本时,我希望文本已经在我的表单中,我认为这样做的一个好方法是添加一个值属性。然而它很棘手,因为我使用django形式,事后生成html。所以这是我在模板中显示表单的相关部分:
<form role="form" action="/editfeedback/{{feedback.id}}/{{board}}/" method="post">
{% csrf_token %}
{% for field in form %}
{% if field.name == "title" %}
<div class="form-group">
<label class="col-sm-2 control-label" for="id_{{ field.name }}">{{ field.label }}</label>
<div class="col-sm-10">
{{ field|attr:"value:THIS IS THE TITLE" }}
</div>
</div>
{% elif field.name == "body" %}
<div class="form-group">
<label class="col-sm-2 control-label" for="id_{{ field.name }}">{{ field.label }}</label>
<div class="col-sm-10">
{{ field|attr:"value:THIS IS THE BODY" }}
</div>
</div>
{% else %}
<div class="form-group">
<label class="col-sm-2 control-label" for="id_{{ field.name }}">{{ field.label }}</label>
<div class="col-sm-10">
{{ field|attr:"value:tag" }}
</div>
</div>
{% endif %}
{% endfor %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary" value="Save">{% trans "Save" %}</button><br>
</div>
</div>
</form>
这适用于显示预定的文字,但我有一个帖子列表,每个帖子都附有一个编辑表格。我的问题是:有没有办法将字段值属性设置为django变量?我尝试了{{ field|attr:"value:{{post.title}}" }}
但是这给了我以下错误:
TemplateSyntaxError
attr需要2个参数,1提供
编辑:从forms.py
添加EditFeedbackForm,从views.py
添加edit_feedback视图
class EditFeedbackForm(forms.Form):
title = forms.CharField(label='Title', widget=forms.TextInput(attrs={'class':'form-control'}))
body = forms.CharField(label='Body', widget=forms.Textarea(attrs={'class':'form-control'}))
tags = forms.CharField(label='Tags', widget=forms.TextInput(attrs={'class':'form-control'}))
我的观点:
@login_required
def edit_feedback(request, feedback_id, board):
boardObj = Board.objects.get(board_name=board)
post = Feedback.objects.get(id=feedback_id)
form = EditFeedbackForm(initial={'title': post.feedback_title, 'body': post.feedback_description})
if request.method == 'POST':
form = EditFeedbackForm(request.POST)
if form.is_valid():
title = form.cleaned_data['title']
body = form.cleaned_data['body']
tags = form.cleaned_data['tags']
post.feedback_title = title
post.feedback_description = body
post.feedback_last_modified = datetime.now()
post.save(update_fields=['feedback_title', 'feedback_description', 'feedback_last_modified'])
# print "saved feedback " + str(post.id)
else:
form = EditFeedbackForm()
return redirect('/board/' + board + '/')
答案 0 :(得分:1)
在您的视图中,初始化表单时,您可以指定初始值:
form = WhatsYourFormsClass(initial={'field1': value1, 'field2': value2})
修改强> 您正确初始化具有初始值的表单:
(...)
form = EditFeedbackForm(initial={'title': post.feedback_title, 'body': post.feedback_description})
if (...)
然后传递另一个(空)实例:
(...)
else
form = EditFeedbackForm()
return (...)
你的方法应该是这样的:
@login_required
def edit_feedback(request, feedback_id, board):
boardObj = Board.objects.get(board_name=board)
post = Feedback.objects.get(id=feedback_id)
if request.method == 'POST':
form = EditFeedbackForm(request.POST)
(form processing)
(if processing went OK, redirect to your chosen view)
else:
form = EditFeedbackForm(initial={'title': post.feedback_title, 'body': post.feedback_description})
return render(**your form template name here**, {'form': form})