经过大量研究,我仍然没有找到如何做到这一点:我的目的是能够在键/值中分离我的 json,以仅显示我认为必要的内容(例如标题、作者、.. .)。这是一个 Django 网站。我已经做到了:
在 models.py 中
class Production(models.Model):
titre = models.CharField(max_length=255, blank=True)
publis = JSONField()
def __str__(self):
return '%s' % (self.titre)
class Meta:
db_table = 'Production'
Run Code Online (Sandbox Code Playgroud)
在 Views.py 中
def post_json(request):
posts = Production.objects.all()
return render(request, 'appli/post_json.html', {'posts': posts})
Run Code Online (Sandbox Code Playgroud)
*和模板:post_json.html *
这完全显示了我的 json 数据
{% for post in posts %}
<div>
<p>aa = {{ post.publis }}</p>
</div>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
这就是我只想向作者展示的内容
<h1>Publications lalala</h1>
{% for post in posts %}
aa = {{ post.publis }}
<p> Num : {{ …Run Code Online (Sandbox Code Playgroud) 我想创建一个启动 python 脚本时的按钮。该脚本打开一个 data.txt 文件并将其转换为 json,以便我将来可以将其集成到我的数据库中。
我今天想做的只是创建一个按钮,单击该按钮即可启动脚本(如果已创建文件 json result.txt,我将检查我的文档,这允许我检查该功能是否有效)。这是我所做的:
在 urls.py 中
url(r'whatever^$', views.recup_wos, name='recup_wos'),
Run Code Online (Sandbox Code Playgroud)
在views.py中
def recup_wos(request):
if request.method == 'POST':
import newscript.py
os.system('python newscript.py')
return redirect('accueil')
Run Code Online (Sandbox Code Playgroud)
模板accueil.html
<form action='{% url recup_wos %}' method="POST">
<input value="Executer" type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
错误信息如下:
Reverse for '' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Run Code Online (Sandbox Code Playgroud)

我认为错误主要是在视图中,也许是语法错误?
我改变了我的模板和我的观点。它可以很好地进行重定向,但脚本无法启动:
def recup_wos(request):
if request.method == 'POST':
os.system('Python newscript.py')
return redirect('../page/accueil')
Run Code Online (Sandbox Code Playgroud)