问题描述
我在django中有两个模板:
I have two template in django:
首先,我给名字index.html
first, i give name index.html
<html>
<head>
<title>Django</title>
</head>
<body>
<div id="formulir">
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="label">Username</div><div class="input"><input type="text" name="username"/></div>
<div class="label">Email</div><div class="input"><input type="text" name="email"/></div>
<input type="submit" name="tambah" value="Add"/>
</form>
</div>
<div id="data">
{% block data %}{% endblock %}
</div>
</body>
</html>
其次,我给名字data.html
Second, i give name data.html
{% extends 'index.html' %}
{% block data %}
<table border="1">
<tr>
<th>Username</th>
<th>Email</th>
</tr>
{% for i in data %}
<tr>{{ i.username }}</tr>
<tr>{{ i.email }}</tr>
{% endfor %}
</table>
{% endblock %}
我希望在index.html提交表单时,数据显示在data.html中,当我第二次提交data.html显示2个数据时,所以第一个数据仍然存在。我不想将数据保存到数据库。
I want to do when form in index.html submitted, the data show in data.html and when I submitted for the second time the data.html show 2 data, so the first data still exist. I don't want to save the data to database.
这是我的views.py
This is my views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
global data
data = []
def home(request):
if request.POST:
data = data.append(request.POST)
return render_to_response('data.html', locals(), context_instance=RequestContext(request))
return render_to_response('index.html', locals(), context_instance=RequestContext(request))
任何人都可以帮助我吗?
anybody can help me?
推荐答案
使用全局变量总体不好,但如果您的应用程序运行一个系统,则可以使用它过程:。您可以在会话中保存数据,或者您需要将所有发布的数据在隐藏的输入中进行形式化并重新发布,例如,如果您需要在任何位置保存,则不需要保存:
Using global variables is bad practice overall but you may use it if your application run with one system process: Python Django Global Variables. You can save data in session or you need to put all posted data to form and repost it in hidden inputs for example if you need to get it without saving anywhere:
...
<div id="formulir">
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="label">Username</div><div class="input"><input type="text" name="username"/></div>
<div class="label">Email</div><div class="input"><input type="text" name="email"/></div>
<input type="submit" name="tambah" value="Add"/>
{% for i in data %}
<input type="hidden" name="username" value="{{ i.username }}"/>
<input type="hidden" name="email" value="{{ i.email }}"/>
{% endfor %}
</form>
</div>
....
创建数据
变量,并使用这样的代码 {{i.username}}
的模板访问它,您需要一些逻辑:
To create data
variable and access to it in templates with code like that {{ i.username }}
you need some logic in view:
def home(request):
data = []
if request.POST:
username = request.POST.getlist('username')
email = request.POST.getlist('email')
data = [{'username': u, 'email': e} for u, e in zip(username, email)]
return render_to_response('data.html', locals(),
context_instance=RequestContext(request))
return render_to_response('index.html', locals(),
context_instance=RequestContext(request))
这篇关于全局变量Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!