Global Variable Django
I have two template in django: first, i give name index.html Django
Solution 1:
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:
...
<divid="formulir"><formaction=""method="POST"enctype="multipart/form-data">
{% csrf_token %}
<divclass="label">Username</div><divclass="input"><inputtype="text"name="username"/></div><divclass="label">Email</div><divclass="input"><inputtype="text"name="email"/></div><inputtype="submit"name="tambah"value="Add"/>
{% for i in data %}
<inputtype="hidden"name="username"value="{{ i.username }}"/><inputtype="hidden"name="email"value="{{ i.email }}"/>
{% endfor %}
</form></div>
....
To create data
variable and access to it in templates with code like that {{ i.username }}
you need some logic in view:
defhome(request):
data = []
if request.POST:
username = request.POST.getlist('username')
email = request.POST.getlist('email')
data = [{'username': u, 'email': e} for u, e inzip(username, email)]
return render_to_response('data.html', locals(),
context_instance=RequestContext(request))
return render_to_response('index.html', locals(),
context_instance=RequestContext(request))
Post a Comment for "Global Variable Django"