我使用脆脆的形式让用户创建一个新的问题帖子,但我希望用户不要更改“项目”字段,以便问题可以放在正确的项目下。我使用了
form.fields['project'].widget.attrs['readonly'] = True
,但是这个只有样式禁用效果,并没有真正的功能。
模型.py
def new_issue(request, project_id):
if request.method == 'POST':
form = IssueForm(request.POST)
if form.is_valid():
issue = form.save(commit=False)
issue.author = request.user
issue.save()
return redirect('project:issue_tracker:issue_detail',project_id=project_id,issue_id=issue.id)
else:
form = IssueForm(initial={'project': project_id})
form.fields['project'].widget.attrs['readonly'] = True
template = 'issue_tracker/issue/new_issue.html'
context = {'form': form,'project_id':project_id}
return render(request, template, context)
表格.py
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('body',)
class IssueForm(forms.ModelForm):
class Meta:
model = Issue
fields = ('title','content','project','status')
class NewIssueForm(forms.ModelForm):
class Meta:
model = Issue
fields = ('title','content','project','status')
new_issue.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<h1>Add New Issue </h1>
<form method="POST" class="Issue-form">{% csrf_token %}
{{form|crispy}}
<button type="submit" class="btn btn-success">Submit</button>
</form>
{% endblock %}
将值设置为 readonly 不够。这只会影响 HTML 的呈现方式。根据浏览器在这种情况下执行的操作,用户仍然可以编辑表单字段。
但是,即使用户无法编辑表单,对 HTTP 请求如何工作有一定了解的人仍然可以在请求中恶意输入值,从而更改数据库中不应该更改的值。
自 djang-1.9 起,您可以更改表单上的
.disabled
属性。在这种情况下,这不仅会将该字段呈现为禁用或只读,还会防止人们恶意输入值。如果用户输入一些数据,则该数据将被忽略(对于该特定字段)。所以我们可以写:
def new_issue(request, project_id):
if request.method == 'POST':
form = IssueForm(request.POST, initial={'project': project_id})
form.fields['project'].disabled = True
if form.is_valid():
issue = form.save(commit=False)
issue.author = request.user
issue.save()
return redirect('project:issue_tracker:issue_detail',project_id=project_id,issue_id=issue.id)
else:
form = IssueForm(initial={'project': project_id})
form.fields['project'].disabled = True
template = 'issue_tracker/issue/new_issue.html'
context = {'form': form,'project_id':project_id}
return render(request, template, context)
请注意,您可能需要一些
initial
或 instance
,否则根本没有任何数据可供参考。
在上面的代码片段中,我们将相同的表达式写了两次:每次构造一个表单。这违背了 DRY 原则。通常,您指定字段,或者 - 例如,在字段并不总是禁用的情况下,您可以通过覆盖
__init__
构造函数来指定。
例如:
class IssueForm(forms.ModelForm):
def __int__(self, *args, disabled_project=True, **kwargs):
super(IssueForm, self).__init__(*args, **kwargs)
self.fields['project'].disabled = disabled_project
class Meta:
model = Issue
fields = ('title','content','project','status')
所以现在我们可以使用一个参数来初始化
IssueForm
,并指定disabled_project
标志是True
还是False
。
如果您使用较旧的 Django 版本(不推荐,因为不再支持),我们仍然可以通过添加
clean_somefield
函数来确保忽略数据。例如:
class IssueForm(forms.ModelForm):
def __int__(self, *args, **kwargs):
super(IssueForm, self).__init__(*args, **kwargs)
widget = self.fields['project'].widget
widget.attrs['readonly'] = widget.attrs['disabled'] = widget.disabled = True
def clean_project(self):
return self.instance.project
class Meta:
model = Issue
fields = ('title','content','project','status')
可能会使该领域
readonly
会有所帮助:
class NewIssueForm(forms.ModelForm):
class Meta:
model = Issue
fields = ('content','project','status')
title= forms.CharField(widget=forms.TextInput(attrs={ 'id': 'title',
'readonly':'readonly'}))
在您看来,您需要用原始值预填充标题字段,如下所示:
some_form = NewIssueForm()
some_form.fields['title'].initial = "some_title"