create form in templates(html) or in forms.py? :django - django

I'm new in django and python. I want to have a form in one of my website's page. When user fills the data, i want to create an xml file with user input. so i need to access the user's data (form data) from views.py. I want to know am i allowed to only have form in html? if yes, how can i use it's data in views.py? i mean should i have always a class defined in forms.py that contains my form fields? If the second way is a rule, what should i do with templates(html)? (It means if i define my fields in a class in forms.py, should i redefine them in templates(html) too? and how can i use data in views.py)
i need some kinds of input in my form: checkbox, input type:text and input type:file
I really need your answers. Thank you :)

You can totally define your form in raw html and write a view which just processes the post data. But then we come to point where you would invent the wheel once again. Django provides a bunch of field classes which provide html rendering and data validation (never trust user input).
I suggest you read the online documentation for forms.

Related

Django accessing fields which are NOT part of a form

I need some clarification as to what the best practice is regarding this. So, say you have a Django form which has many fields that the user can fill out. Say you also have corresponding fields in the models file, but the models file contains some extra fields that the user cannot modify, e.g. unique reference number. Say also, you want to access these hidden fields in the views so that you can present this reference number to the user.
What's the best way of accessing these "hidden" fields that are created when a valid form is submitted? I was thinking of grabbing the latest entry by date, though if there's concurrent requests at the same time, the wrong data may be pulled?
Try using Django's HiddenInput widget. This will allow you to associate data with a form without allowing the user to modify it.

When to use form vs model validation?

Just curious. What is the best practice for when to use form vs model validation?
From what I understand currently, form validation should be used for:
AJAX / HTTP requests params
Forms that do not correlate to a model?
Another question is: I have a HTML form that roughly correlates to a model instance, do I use a ModelForm for it?
Definitely use ModelForm, if your form resembles model object even in a tiny bit.
If there are some minor differences (e.g. you don't use some of the fields or you want to use different error messages etc.) it's much easier to customize ModelForm then to use Form and implement all this functionality from scratch.
For more reference regarding ModelForm please checkout PyDanny's Core Concepts of Django ModelForms.
I am also trying to understand what is the difference/relation between form and model validation and I would like to share my notes that are formed after reading several docs.
I am currently interested in Creating Forms from Models
#mariodev shared the document Core Concepts of Django ModelForms and this provided a good start.
ModelForms select validators based off of Model field definitions
The main story behind the scenes seems to be the DRY principal. This article explains very well what exactly is the case here.
All right, all this is fair. The question is "Where in the Django Documentation is this explained"?
I bumped on a very brilliant article where it states that:
The form.full_clean() method is called by django at the start of the validation process (by form.is_valid(), usually immediately after the view receives the posted data).
Correct me if I am wrong but that line reads that everytime I enter data and hit 'enter' the validation process begins!
OK, this is simple now:
The validation on a ModelForm begins when we hit 'enter'.
Django first validates the form by checking one by one every applicable validation method on Fields, Field Subclasses (This is the documentation for a model's field subclass, not for a form field subclass), Form Subclasses and ModelForm (since it is a ModelForm).
Finally, it validates the Model Instance.
This is how all this works theoretically. The only thing that remains is to implement it.

Django: what approaches are there to 'parametising' the display and editability of form fields.

Django: what approaches are there to 'parametising' the display and editability of form fields.
I have several forms where I want some fields to be simply missing from the form display (dependent on the current user) and some fields to be uneditable, depending on the user.
This needs to be enforced on the server, so that a malicious user cannot break the security by manually constructing a post request with the missing parameter.
Likewise fields which are not displayed to the user, must still come back in the form results, so that fields which are not displayed to a user and not 'wiped-out' when the model is written back to the DB.
I also need to solve the same hiding problem for templates.
The app will have dozens of forms, with different immutability/hiding requirements, so this needs to be a generic system. Coding permissions inside each form would be too prone to error.
Any help appreciated.
Chris.
For hidding fields from form display, simple delete unneeded fields in form's __init__ method. Constructing post request by adding deleted fields, will not have influence on the form.
Regarding disabled or readonly fields, check this article and another question. Basically, cleaning field should set it to initial state.

Django, custom fields, to_python, and displaying serialized text as-is in Django admin

I have a complex object that I'm storing serialized in a text field. For most purposes, I want the object pulled from the database to be that complex object. However, when I'm editing it in a form, I just want to see that serialized text in the field.
I tried using the value_to_string function but it appears as if it isn't being called at all when editing the record from with admin.
What do I do so that the raw serialized text shows up in the admin text field?
Since the admin already uses the value of your model field, one option would be re-serializing it for editing...
See also formfield_overrides:
This provides a quick-and-dirty way to override some of the Field
options for use in the admin. formfield_overrides is a dictionary
mapping a field class to a dict of arguments to pass to the field at
construction time.
I'm not sure if it's the same problem, but I figured out a way to change the field value in the admin form before displaying it. I explained how to use a custom widget and an custom admin form in this answer.
Note that the custom widget only helps you to display the value in a different format. It won't parse the input value back to an object, although I believe that would be possible too.

How customize fields in the Django admin interface

I have a model in my Django project with a member which is a char field. Basically data in this field will be entered as comma-separated values.
Without a long-winded explanation of what the overall goal of this is, basically rather than having the admin interface use a simple text field, I'd rather have have some custom HTML for the form so I can just use checkboxes and assemble the values of the checked boxes into a CSV string myself once the form is submitted.
Most of the django customization I was able to find on Google didn't answer my particular problem.
If I understand your question correctly I think you want to search for writing custom widgets. Perhaps start here: http://docs.djangoproject.com/en/dev/topics/forms/