首先,我想为我的英语道歉,因为它不是我的母语:( 所以…我正在使用Django进行web开发,我面临一个问题,问题是我目前有这个模型:
class Project(models.Model):
project_name = models.CharField(max_length=200)
project_company = models.CharField(max_length=200)
project_notes = models.TextField(default="")
我想在HTML文本区域中显示project_notes数据,为此,我一直以以下方式使用Javascript:
<script>
var notes = "{{ proyecto.project_notes | safe }}";
document.getElementById("notesTextArea").value = notes;
</script>
请注意,notesTextArea是我的html上textarea标记的id,该脚本位于我的html文档的底部(在body标记结束之前)
我正在my views.py上使用python函数呈现此模板(并在POST请求时保存更改):
def notas(request, id):
project = Project.objects.get(id=id)
if request.method == 'GET':
return render(request=request, template_name="tasks/notes.html", context={"proyecto": project})
else:
new_notes = request.POST["notes"]
project.project_notes = new_notes
project.save()
return redirect('tasks:notas', id=id)
问题是,当我在文本区域(按intro)上添加换行符并保存数据时,显然数据正在保存(因为我选中了django admin),但文本区域不再显示数据。显然,特征线存在问题,因此我尝试使用:
{{ proyecto.project_notes | linebreaks }}
但是它在我的文本区域上显示了
标记,当我试图通过javascript替换标记时,文本区域同样不显示任何数据。
请注意,我使用双引号调用python变量,因为javascript在我没有调用时向我发送了一个错误(uncaughtsyntaxerror:Unexpected-token<;)
有人能帮我吗?我做错了什么? 另外,我想说的是,我最近开始从事软件开发工作,所以你可能会发现有更好的方法来完成我在这里做过的一些事情(请随意评论),另外,这是一个个人web应用程序,用于管理我当前的项目
我还将使用当前模板视图(和django管理信息)添加一些图片
What text area is showing with | safe
What text area shows with | linebreaks
谢谢
更新 我如何分配textArea的值?这样:
<script> var notes = "{{ proyecto.project_notes | linebreaks }}"; document.getElementById("notesTextArea").value = notes;
</script>
目前没有回答
相关问题 更多 >
编程相关推荐