Django - Using input variables in other functions -
i'm trying find way utilize user-provided variables later. i've stripped downwards minimal example:
models.py
class="lang-py prettyprint-override">class input(models.model): x = models.integerfield() class inputform(modelform): class meta: model = input
views.py
class="lang-py prettyprint-override">def input(request): if request.method == 'post': form = inputform(request.post) if form.is_valid(): form.save() homecoming httpresponseredirect('/results') else: form = inputform() homecoming render_to_response('input.html', {'form':form}, context_instance=requestcontext(request)) def result(request): # here input form, e.g. # # x = request.get["x"] # # or # # inp = inputform(request.get) # x = inp.x # # this, can able write line below: homecoming httpresponse("x = %f" % x)
what recommended way this?
edit:
the main problem seems request.get dict empty in results function, i.e.
class="lang-py prettyprint-override">def results(request): homecoming httpresponse(request.get)
shows empty page. , of course of study request.get['x'] gives exception "key 'x' not found in querydict: {}". input.html looks this:
class="lang-html prettyprint-override"><form method="post" action="">{% csrf_token %} {{ form }} <input type="submit" value="compute" /> </form>
ideally, able send request.get inputform, in fallenangel's original answer, not work because request.get empty.
if not misunderstand you, want utilize value of field of posted form without saving. can utilize commit=false
, so, create object using meta model, not save database:
def result(request): inp = inputform(request.get) if inp.is_valid(): my_obj = inp.save(commit=false) # not save database homecoming httpresponse("x = %f" % my_obj.x)
update: if not need form validation (for reasons), can utilize dictionary methods since request.post
, request.get
dictionary-like objects. (request , post objects in django). so:
request.get['x']
or
request.get.get('x')
will work you.
django django-models django-forms
No comments:
Post a Comment