Validating checkboxes from form in Django - python

Relatively new to Python..
I would like to know how to check if a check box has been selected.
I am reading the form values into a python script and am unsure how to go about validating.
Using the Web Framework Django. The form is being posted back to a views.py script.
I am loading up a dictionary with many different variables. These variables need to then be passed to a shell script, the command to the shell script requires these check box values to be returned in the form of 0 or 1 in order to create a zip file.
I understand this code is not python but it is my understanding of what i am trying to achieve:
if variable.checked == 'yes'
return 1
elif
return 0

The documentation for Django is really quite good. Check out the description of the HttpRequest class for more information on how this part works. The short answer is that you can do something like this:
def myview(request):
# do stuff
option_checked = 'myvariable.checkboxoption' in request.REQUEST
# do more stuff and return the response
That being said, Django also has a comprehensive form widget/validation/extraction subsystem. I'd recommend going through the full tutorial (particularly part 4, which covers forms) and checking out the mailing list to answer any more specific questions about how to use these libraries.

Related

How can I make unique user "sessions" for my Django web application running on heroku?

I made a web application using Django, that stores information in python arrays that are accessed by the user via the front-end. My problem now is that since I have deployed it via heroku, you can't use the website on more than one device, or else options selected from one device affects the website data for all devices.
How would I be able to make it so that user experiences are different / unrelated? How can I alter the views.py (or other components) so that the web application has a "session" for each user?
This is for a django web application running on heroku. The application is for my school, and it's akin to a battle-royal type site. Users can select people displayed on the site, which in turn removes them from a python dictionary that's stored in memory and puts them in another dictionary for later. The problem is, if more than one person is using the site at one time, they access the same dictionary. I haven't a clue what to try to solve this.
"processor.py"
names, accepted = list(), list()
# names contains many names of people, accepted is empty
def accept_person(person):
if person in names:
accepted.append(person)
names.remove(person)
else:
a = difflib.get_close_matches(person, names)
if len(a) > 1:
accepted.append(a[0])
names.remove(a[0])
else:
pass
"views.py"
def view_home(request):
if request.method == 'POST':
if(request.POST.get("yes")):
processor.accept_person(request.POST.get("yes", False))
Accepting a person on one device accepts that person for everyone accessing the site. I want to be able to have one "session" per user, so that one person changing this python dictionary doesn't affect everyone's experience.
What you are looking for here is a way to authenticate users, Django is brilliant for this and handles basically all of it for you.
But if you really want a full solution with minimal effort I would take a look at a package called django-allauth.
The official docs (https://django-allauth.readthedocs.io/en/latest/installation.html) can be a bit daunting so here is a good tutorial I followed the first time I integrated authentication into Django.
https://wsvincent.com/django-allauth-tutorial/

Where to validate Django AJAX app?

I'm working on an event based AJAX application using Django. Within my models I've a class that creates and updates an objects and I would like to include better input validation. For example, I know that my start date should be before my end date and would like to check for this fact within the app.
Do I place this within my models? It seems messy to messy to check all 8 input parameters are valid within the creation or save method.
For example, something like this:
if foo != '' and int(foo) > 0:
self.foo = foo
I know Django has the functionality to validate forms, but can this also be applied to AJAX streams?
Maybe this may help: https://github.com/alex/django-ajax-validation
Documentation:
https://github.com/alex/django-ajax-validation/blob/master/docs/usage.txt
https://github.com/alex/django-ajax-validation/blob/master/docs/serving-ajax-validation-media-server.txt

Is it possible to have a form built by a user?

For example:
I have a user that wants to create a contact form for their personal website. They want three input type=text and one textarea and they specify a label and an name/id for them on my site. Then they can use this form on their site, but I will handle it on mine?
Is it possible for django to spit out custom forms specified by the user?
Edit: If django is too "locked down" what would you recommend I do? I would like to stay with python.
something like http://code.google.com/p/django-forms-builder or one of the million similar addins?
(made into answer at OP's request)
For this you would have some kind of editor that would create a html string. This string would be stored into your database and then upon request you would display it on the user's site.
The editor should be very strict into what it can add and what the user has control over, there are some javascript editors available that will be able to provide this functionality.
The only issue I can think of is that you may run into django escaping the form when displayed to the page.

Django Redirect with Python Variables

So I'm currently working on a Django project where an object with an id is created from a form, and that id is needed in the form that the first form redirects to. Is there a way I can pass this id (in python) using redirect? I can't seem to figure out how to do this, but I'm sure there is a way since it seems like it would be pretty useful.
Thank you all,
Alex

Forms generated through admin in Django

I need to be able to create forms from admin panel. Process would look like this:
I click on "Add form" then I enter email to which the form should be sent and of course several fields (probably thanks to inlines) consisting of field name, type and if it is required. User should be able to view and fill the form and submit it and the data should be sent to the email given in admin.
Everything looks pretty straightforward but from my point of view it need some metaclass programming skills.
Could anyone point me to a goot form builder for Django or at least hand some tips about creating such thing? I found django-forms-builder but it is a bit too restricted imho.
I know this one's a few months old but I just though I'd post an update here anyway for anyone else that comes along.
django-forms-builder has just been rewritten to do exactly what you were looking for when you originally posted this question.
You can find the new version at http://github.com/stephenmcd/django-forms-builder or http://bitbucket.org/stephenmcd/django-forms-builder
There are many alternatives, although not many of them are actively maintained:
https://www.djangopackages.com/grids/g/form-builder/
If you want to have a full control of what's happening (change fields for your needs or add new ones, add captcha or honeypot, add custom handling of form data, use form wizards or even use your forms via web REST API), use django-fobi https://pypi.python.org/pypi/django-fobi

Categories