- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据下图,当任务状态为已完成
或已取消
时,我尝试使字段类别和当前点不可编辑,否则字段应该是可编辑的。
下面是我的 html 文件中的代码。
{% extends "base.html" %} {% load widget_tweaks %}
{% block content %}
<div id="form-group">
<form method="POST" action="." enctype="multipart/form-data">
{% csrf_token %}
<label>Select your category</label>
{{ form.category|add_class:"card" }}
<label>What's the status of the task?</label>
{{ form.status|add_class:"card" }}
<label>Current points:</label>
{{ form.points|add_class:"card" }}
<label>Finalized date:</label>
{{ form.ending_date|add_class:"card" }}
<button type="submit" class="btn btn-success">Send</button>
</form>
</div>
下面是我的 forms.py
文件中的代码。
class TaskModelForm(forms.ModelForm):
class Meta:
model= Task
fields = ['category', 'status', 'points']
def __init__(self, *args, **kwargs):
super(TaskModelForm, self).__init__(*args, **kwargs)
self.fields['status'].required = False
self.fields['points'].required = False
当我想编辑此表单的内容时,我需要验证状态是否已完成,因此字段不可编辑,否则字段应该是可编辑的,我正在考虑:
{% extends "base.html" %} {% load widget_tweaks %}
{% block content %}
{% if form.status.value == 'Active' %} <!--make the fields editable -->
<div id="form-group">
<form method="POST" action="." enctype="multipart/form-data">
{% csrf_token %}
<label>Select your category</label>
{{ form.category|add_class:"card" }}
...
<button type="submit" class="btn btn-success">Send</button>
</form>
</div>
{% endif %}
{% if form.status.value == 'Finalized' %} <!--make the fields non-editable -->
<div id="form-group">
<form method="POST" action="." enctype="multipart/form-data">
{% csrf_token %}
<label>Select your category</label>
{{ form.category|add_class:"card" }}
...
<button type="submit" class="btn btn-success">Send</button>
</form>
</div>
{% endif %}
但是,我相信我的方法可能行不通,因为这可能是一个更前端的问题,而不是后端问题(只是猜测)。您能指出解决这个问题的正确方向吗?
最佳答案
由于状态是用户选择的,因此您无法使用服务器上运行的 Python (Django) 来满足您的要求。您必须使用在显示表单的网页中运行的 JavaScript 来解决该问题。像这样的事情肯定会成功。
{% extends "base.html" %} {% load widget_tweaks %}
{% block content %}
<body onload="makeReadOnly();">
<div id="form-group">
<form method="POST" action="." enctype="multipart/form-data">
{% csrf_token %}
<div class="tweet-composer">
<label>Insert your task</label>
{{ form.task|add_class:"card js-keeper-editor" }}
</div>
<label>Select your category</label>
{{ form.category|add_class:"card" }}
<label>Current points:</label>
{{ form.points|add_class:"card" }}
<button type="submit" class="btn btn-success">Send</button>
</form>
</div>
</body>
{% endblock content %}
<script type="text/javascript">
{% block jquery %}
function makeReadOnly()
{
if (document.getElementById('id_status').value == 'Finalized'){
document.getElementById('id_task').readOnly=true;
document.getElementById('id_category').readOnly=true;
}else if (document.getElementById('id_status').value == 'Active'){
document.getElementById('id_task').readOnly=true;
document.getElementById('id_category').readOnly=false;
}
}
document.getElementById('id_status').addEventListener('change', makeReadOnly);
{% endblock %}
</script>
通过“查看页面源代码”,您可以看到 Django 从您的表单中生成的 HTML 结构,以便您可以使用 JQuery 选择器识别正确的位。或者你可以这样做
f = SomethingForm()
f.as_p()
在./manage.py shell
控制台中。
在 Django 端,您可能需要自定义表单验证来处理状态值与其他字段是否必填之间的相互依赖关系。
关于javascript - 当满足特定条件时,如何使 Django 表单中的字段变为只读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59725697/
我正在与 svgpath 合作为了操作 svgs,我需要更改坐标系,使 y 变为 x,x 变为 y。我的问题是有什么办法可以做到这一点。我尝试围绕点 0 0 旋转并按图形的高度进行平移。 最佳答案 作
我有一个来源 Observable那: 注册一些 BroadcastReceivers当它订阅时 取消订阅时取消注册。 我只希望在订阅者数量从 0 增加到 1,或从 1 增加到 0 时发生订阅副作用。
def string_transf(): input('Enter a word or string') #letters = 'abcdefghijklmnopqrstvwxyz'
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Format Number like StackoverFlow (rounded to thousands
我是 R 的初学者,有一个任务,我必须创建一个函数,其中所有数字都重新缩放,Inf 映射到 1,-Inf 映射到 0。我了解如何进行重新缩放,我只是不知道如何添加到函数中,所以 Inf 变为 1,-I
我有一个由二进制值组成的单字节字符数组,我试图将它拆分成一个二维 int 数组(低 nybble 和高 nybble)。这是我的代码: int nybbles[2][4]; //[0][] is lo
有人可以解释为什么 FLEX 4.5 XMLDecoder 对我的 XML 数据这样做吗? var decoder:XMLDecoder = new XMLDecoder; var $object:O
问题: 我在使用 scanf() 时遇到问题。我从阅读论坛之类的地方知道 scanf() 在 C 中有很多问题,但我只是还在学习基础知识,所以我不知道所有的细节。 我想解决的代码片段。 #includ
我遇到了一个问题,其中 UIViewController.navigationController 变为 nil,我正在拼命寻找这个问题的答案。 UINavigationController 在应用程
我在做this教程。 基本上我的问题是,在 front_is_clear 为 false 后,它运行 Jump_over_hurdle,然后停止。 from my_lib import * while
我正在实现一个简单的 Drag'n'Drop Bahevior。首先我要订阅鼠标事件: protected override void OnAttached() { b
这是我的代码的简化版本: template TIterator findMaximalPosition(TIterator begin, TIterator end) { TIterator
大家好,我有这个代码 function computeChange(){ var change; var amountDue = parseFloat(document.getElementById(
就像我的其他问题一样,非常不言自明。问题是,当用户没有选择银行时,变量 bankMoney 在第一次调用 payDay 时会变为 NaN。不选择,它应该通过我的 random 函数进行随机化,但我认为
当我登录时,我已通过身份验证,但当我切换到另一个页面时,req.isAuthenticated 返回 false,并且我位于登录面板上。第二件事是,当我登录时,我不断收到错误“发送后无法设置 head
下面一行 filterM (\x -> Just (x > 0)) [2, 1, 0, -1] 输出 Just [2,1] 和行 filterM (\x -> Just (x > 0)) [] 显示
这可以完美地使用整数进行拓扑排序,但是我想让它与作为参数的字符串类型兼容。有人对如何从这里更改数据结构有任何指导吗?或者我是否必须重写整个内容才能使 [add.edge("a","b");] 工作?我
我正在尝试调试此应用程序,但存在一个大问题。当我尝试将数组保存到数据文件时,一切正常。但是,如果我关闭应用程序并重新打开数组中的 bool 值,则变为 nil。这是保存数组的代码: NSString
程序运行时,有一系列的ListView窗体。我们用项目(作为字符串)填充其中之一,并检查选择状态是否已更改。更改后,我们使用 FocusedItem.Text 获取所选项目的文本。第一次工作得很好,但
我正在编写一个 WP 插件,它连接到另一个 WP 站点,并获取一些数据作为返回(一些强大的条目,带有名称和其他内容)。 一切都很好,我的插件基本上按预期工作 - 但我今天注意到它有一些奇怪的编码问题
我是一名优秀的程序员,十分优秀!