Django: Send posted files to a different form - django

Well, I have to send files posted by one form to other form, I have to read an XML file first, if the file is valid I'll redirect to the next form, which has to have the valid file received in the past form. I tried with sessions but I'm not sure if it's the right way to go, here my models.
class InitialForm(models.Model):
...
name = models.CharField(max_length=50)
xml = models.FieldField(label='Please choose an XML file')
...
class SecondForm(models.Model):
...
name = models.CharField(max_length=50)
pdf = models.FieldField(label='Please choose a PDF file')
...
The reason I got two forms is because I've got to read the XML first and validate it, and then in the next form 'SecondForm' show the data I just parse from the XML in order to verify and give feedback to the user. Then both files must be inserted in a database, only if the first one is valid.
Any help will be welcome.
Thanks in advance.

I think what you need is a form wizard that Django offers for situations when you need to build a form that is split into multiple requests.
From their docs this is how it works:
The user visits the first page of the wizard, fills in the form and
submits it.
The server validates the data. If it’s invalid, the form
is displayed again, with error messages. If it’s valid, the server
saves the current state of the wizard in the backend and redirects to
the next step.
Step 1 and 2 repeat, for every subsequent form in the
wizard.
Once the user has submitted all the forms and all the data has
been validated, the wizard processes the data – saving it to the
database, sending an email, or whatever the application needs to do.

Related

Add an additional form to a formset using POST without Javascript and validation

I want to use a Django formset, but with pure server rendering and without the need of Javascript to add additional forms to it. The user should just click a button on the page and the page should reload with an additional form in the formset. All user input should be preserved! The relevant part in the view is:
if request.POST.get('add_form') == "true":
cp = request.POST.copy()
cp['form-TOTAL_FORMS'] = int(cp['form-TOTAL_FORMS']) + 1
fs = MyFormSet(cp)
The problem is that when MyFormSet(cp) renders a form representation it adds validation errors to it (like "This field is required"). This is ugly and not acceptable. How can I render it without the errors (they should only be present when the whole form was submitted)?
MyFormSet(initial=...) seems not to be an option as it must also work in a UpdateView (the docs are pretty clear that initial is only for extra forms) and also the POST data can't be directly used as initial values.
I am super thankful for any hint as it took me several hours without getting anywhere (and it seems to be such a common feature as the rest of Django is so Javascript agnostic).
It feels like a hack, but this works (after the formset was initialized):
fs._errors = {}
for form in fs:
form._errors = {}
This removes all errors from the fields when it is rendered to HTML. The background is that when _errors is set to None (the default) the form validates itself when it is rendered. When it is set to an empty dict the form will not validate itself anymore and just "thinks" that there are no errors in it. So no error messages are rendered.

Sitecore WebForms for Marketers : send email to users

I am using Send Email Message action. I have added one action for send email to admin and another thank you mail to user who filled the form so my question is how I can add user email in Send Email Editor. I tried to add email Insert Field but it is not working.
EDIT:
By Default, the TO Dropdown only allows content from the Email Field Type. To allow other field types, please go to to the Send Email Message action found here by default: /sitecore/system/Modules/Web Forms for Marketers/Settings/Actions/Save Actions/Send Email Message
go to the Editor section, and enter the following into the QueryString field:
AllowedToTypes={84ABDA34-F9B1-4D3A-A69B-E28F39697069}|{YOUR CUSTOM FIELD TYPE GUID}
The first guid is for the standard Email Field type, and the second one will be your custom field. This should allow you to choose from those fields in the TO field.
For best practices, you should duplicate the Send Email Message action, and apply the changes there to prevent issues with future upgrades.
To allow your custom fields to be selectable for the CC and From fields as well, your query string would look like this:
AllowedToTypes={84ABDA34-F9B1-4D3A-A69B-E28F39697069}|{YOUR CUSTOM FIELD TYPE GUID}&AllowedCCTypes={84ABDA34-F9B1-4D3A-A69B-E28F39697069}|{YOUR CUSTOM FIELD TYPE GUID}&AllowedFromTypes={84ABDA34-F9B1-4D3A-A69B-E28F39697069}|{YOUR CUSTOM FIELD TYPE GUID}
From there, to send an email to one of the fields on the form, simply choose the field by clicking the little arrow next to the 'To' field
I have noticed a bug in previous version of wffm where it places double brackets around the field name, for example: [[email address]]. If you notice the double brackets [[..]], then erase the entire field and select it again as shown above.
I found there is an issue a with the To: field in certain browsers - not sure if this is related to Bug 402562.
In Chrome I get js error messages but in Firefox I can insert the email field with no issues.
As stated my #amir818, you need to add the field name with brackets in the To field. If you are using Chrome then the arrow may not work due to a javascript error, it works in IE though.
Alternate way to add the field into the message body from the Insert Field droplist and then copy+paste into the TO field. Looking at your field names it should be [Emailaddress].
The double brackets that amir mentioned is a bug, you can get a fix from Sitecore and quote ticket number 402562. When you edit the form again, the field has 2 sets of brackets added which then breaks the send action, e.g. it would end up as [[Emailaddress]] which is incorrect and therefore fails

Sitecore Webform for Marketer Save to Database FormID

I have a form created in sitecore WFFM and exported into ascx control.
My form has 2 save actions:
Save to Database and
Send Mail Message
In Save to Database action, a new row is inserted into WFFM tables.
I want to get the submitted form id (which is the id from form table) and insert into the send mail message as part of the message. Is there a WFFm function that I can call to get the id? I am not talking about this.FormID which is the main form id, but the id of just submitted form instance.
The simple answer to that question is, out of the box, no. The only out of the box values you can use on the send email are the values in the form itself. You can easily do it with a custom data call, but out of the box, no.

'Hiding' form query from URL (Django 1.3)

I have a form with 6-7 fields. After user input, my webapp searches for those fields in a database and displays the results.
Now the issue is, that the URL ends up having all the form field names and their values in it.
result/?name=lorem&class=arc&course=ipsum
Now with the form having 7-8 fields the url ends up looking ugly.
Is there a Django technique to 'hide' these from the URL? Quotes around hide because I'd be okay with a completely different way to pass the objects to my database from the form as well.
Use a POST request. Here's the django docs on forms and a specific example using POST>. HTML-wise, all you need to do is change the method on the form tag.
I do not recommend to use POST requests for search. If you'll use GET it will be easer for user, he can just bookmark a link and save search or share search results with friends.

django - verifying data

in my django application, I allow users to cut and paste some json data into a textbox, to be uploaded to the database for saving.
I would like to verify the json data before saving them into my database. Do I have to write custom code to check every field or is there some form api I can use to check the data ?
Create a form with validation methods
http://docs.djangoproject.com/en/dev/topics/forms/
or use a model form:
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/
If it decodes, then I guess it's json...
However one wonders why you'd put that straight into your database without parsing it? Are you planning to do something like serve it straight back to them later? If so then you can validate it client side.
Since you are allowing user to enter (basically) arbitrary data care must be taken against SQL InJection or XSS. Django has support for both, here's how -
In your template, next to the form place a csrf_token which prevents CSRF.
To prevent SQL Injection attacks, place urlencode filter in your templates wherever applicable. Well not much but it protects against cases like - turns your apostrophes ' into %27, rendering them harmless
Also if you have your form.py like ./app/form.py, then while defining a form -
class RegistrationForm(forms.Form):
username = forms.CharField(label='Username', max_length=30)
def clean_username(self):
username = self.cleaned_data['username']
# do any more processing you need.
Also if you are using JSON, then be sure to use inbuilt modules for this for the risk of reinventing the wheel.