Skip to main content

Passing values from the view to the form.

Passing values from the view to the form it is really popular. Ex: form should be different for different kind of the user. Unfortunately I have not seen a documentation how to do this with class based view.


This task is separated in the two parts:
1) Prepare values in the view

class Myview():
""" This is only example view """
# set form
form_class = myformwithadditionallogic
# our function which pass some values to the form
def get_initial(self):
""" Passing initial values to the form"""
# default initial should be saved
super(Myview, self).get_initial()
self.initial = {"ourvalue":1}
return self.initial


That all for view.
Second part is retreive this data in the form:

class myformwithadditionallogic():
""" the example form . has no field only constructor
"""
def __init__(self, *args, **kwargs):
""" Constructor """
myvalues = kwargs.pop( 'initial', None)
# Save default values
super(myformwithadditionallogic, self).__init__(*args, **kwargs)
# print our values or do something else
print myvalues['ourvalue']


That all !

Comments

Popular posts from this blog

Small script for one of the plarium game

few notes about google games. Goggle informed that access to the all games be closed after June, 30. I played "Pirates: Tides of fortune" and decided to limit my game time using small script. Detail is below. Some history First internet games were updated network games only. 15 years ago internet connection requires a lot of money and nobody had a problem with lagging of the other players. Usually games uses personal communication protocol. Warbirds, Aces High and Eve online use this way. Next part use a browser and trivial HTTP protocol. One of the popular game in this area is travian . Travian player uses browser and every browser (PC, cell phone, tablet) can be used for playing. Of course, popularity of the game is related to graphics. Trivial HTTP does not have good power in this and other technology is used for this side. One of them is Flash . Unfortunately flash requires a lot of CPU : "Pirates" was near to froze at my old celeron with

Mac, emacs and new python3.10

Mac, emacs and new python3.10   I've deceided to upgrade my python to the new Python 3.10 and upgrade my IDE based on Emacs too.  Upgrading python to version 3.10  A installing python 3.10  is really simple task using a homebrew:  MacBook-Pro-Hohlov:call-me skhohlov$ brew search python ==> Formulae app-engine-python gst-python python-launcher python-tk@3.9 python@3.8 pythran boost-python ipython python-markdown python-yq python@3.9 ✔ jython boost-python3 micropython python-tabulate python@3.10 reorder-python-imports cython bpython ptpython python-tk@3.10 python@3.7 wxpython ==> Casks awips-python awips-python
  debian,  amavis,  virus inside archive   One my client asked informed me, that amavis skips some files types. mail server configuration is really simple: Postfix as SMTP server and  amavis working as context filter. Also amavis runs spamassasin and clamd antivirus. Amavis gets files from attachment and unpack it. lha file is not detected. short investigation First I deceided to run amavis  in debug mode and verify how virus passed postix+amavis.  root@newserver:/var/lib/amavis# /etc/init.d/amavis stop [ ok ] Stopping amavis (via systemctl): amavis.service. root@newserver:/var/lib/amavis# /etc/init.d/amavis debug Trying to run amavisd-new in debug mode. Debug mode inform about loaded plugins: ' Nov 13 22:07:23.335 newserver. /usr/sbin/amavisd-new[40334]: Found decoder for .cpio at /bin/pax Nov 13 22:07:23.336 newserver. /usr/sbin/amavisd-new[40334]: Found decoder for .tar at /bin/pax Nov 13 22:07:23.336 newserver. /usr/sbin/amavisd-new[40334]