Pyramid ignore missing template variables - pyramid

I have a simple form using Pyramid and I want to use it as a new or update. I don't particularly want to have to create empty dictionary values for all the fields in my form, I just want it to ignore them without raising an exception.
<div metal:fill-slot="content">
<form method="POST" action="/studentupdate">
<input name="id" value="${studentid}" type="hidden" />
Name: <input type="text" name="studentname" value="${studentname}"/><br />
Exam Score: <input type="text" name="studentexam" value="${studentexam}"/><br />
Quiz Score: <input type="text" name="studentquiz" value="${studentquiz}"/><br />
Homework Score: <input type="text" name="studenthomework" value="${studenthw}"/><br />
<input type="submit" value="Save"/>
</form>
</div>
How can I do this?
Thanks.

You can workaround like this
${studentexam or ''}
% for i in missing_list or []
<li>${i}</li>
% endfor

Related

Some html tags are missing when i write to a file using python requests

I am trying to login to a website using requests.The website requies token for its login.
so decided to parse the html and write it to a file.txt But the file.txt is missing the token tag.
HTML code:
<form id="pw_form" class="exception_password" action="/409514769/password" method="post" data-xsrf-protection="enabled">
<input type="password" id="password" class="exception_password-input iris_input" name="password" placeholder="Enter password" autocomplete="off" data-validators="required">
<input type="hidden" name="is_review" value="">
<input type="hidden" name="is_file_transfer" value="">
<input type="submit" value="Submit" class="iris_btn iris_btn--primary">
<input type="hidden" name="token" value="4dc82c1a780e11667650f856da9b1d9fd31b176b.e7mu8nmqrb.1587446534"></form>
PYTHON code:
from requests import Session
with Session() as s:
site = s.get("https://vimeo.com/409")
with open('page.txt','w') as out:
out.write(site.text)
This is what the file writes:
<form id="pw_form" class="exception_password" action="/409514769/password" method="post">
<input type="password" id="password" class="exception_password-input iris_input" name="password" placeholder="Enter password" class="password iris_form_text" autocomplete="off" data-validators="required">
<input type="hidden" name="is_review" value="">
<input type="hidden" name="is_file_transfer" value="">
<input type="submit" value="Submit" class="iris_btn iris_btn--primary">
</form>
What is happening here?
Website don't allow request from bot.
One possible solution to this problem is to add headers while making the request.

Trying to create a HTML Mailto form for vacation request, but I can not get it to work.

This is what I've got so far. (Email address is of course not the real one) But I must have lapsed on how to do this. I cannot seem to figure out how to get it to send the info to an email address. Can anyone help me finish this?
<h2>Vacation Request</h2>
<form action="mailto:test#test.com" method="post" enctype="text/plain">
<form>
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
</form>
<form>
Start Date<br>
<input id="date" type="date">
</form>
<form>
End Date<br>
<input id="date" type="date">
</form>
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>
</body>
</html>

How to print string if variable is empty

I have the following HTML/TWIG:
<input type="text" id="username" name="_username" value="{{ last_username }}" />
Now I'd like to use value="Username" if last_username is empty. But I also don't want to make an if for this. So, could I do something like:
<input type="text" id="username" name="_username" value="{{ last_username or 'Username'}}" />
I know that I could set a default value on the logic side of my application - not sure what would be better practice
Use the default filter
<input type="text" id="username" name="_username" value="{{ last_username | default('Username') }}" />

stringByEvaluatingJavaScript to press button

Im using the webview.stringByEvaluatingJavaScript() function to enter some data into a website using this code:
self.webview.stringByEvaluatingJavaScript(from: "document.getElementsByTagName(\"input\")[2].value = \"usename\"") //enters username
self.webview.stringByEvaluatingJavaScript(from: "document.getElementsByTagName(\"input\")[3].value = \"pass\"") //enters password
self.webview.stringByEvaluatingJavaScript(from: "document.getElementsByTagName(\"input\")[5].submit") //press button?
The first 2 commands work fine, and enter the information into the website, the last command is meant to press the Login button, but I can't get it to work. I have tried quite a few different commands, but nothing worked.
The html of the button and fields is:
<input type="text" class="large-12" id="username" name="username" value="" required="">
<label for="password">Password</label>
<label for="username">Membership Number</label>
<input type="password" class="large-12" id="password" name="password" value="" required="">
<input type="hidden" name="redirect" value="https://www.webname.org/">
<input type="submit" name="login" id="submit" value="Login" class="button small login">
What do I need to do to 'press' the button programatically?
Solved my own problem. Solution:
self.webView.stringByEvaluatingJavaScript(from: "document.getElementById('submit').click()")

Empty message with Formt in MODx

Here is my code of send-page:
<p>[[!FormIt?
&hooks=`email,FormItLog,spam,emailUser,redirect`
&emailTo=`heash94#gmail.com`
&emailSubject=`[[+subject]]`
&emailFromName=`[[+name]]`
&emailTpl=`ContactTpl`
&redirectTo=`62` ]]</p>
<div>[[+fi.error.error_message]]</div>
<form action="[[~[[*id]]]]" method="post"><input type="hidden" name="nospam:blank" value="" />
<div>
<label for="name">Name: </label> <input id="name" type="text" name="name:required" value="[[+fi.name]]" /> <span>[[+fi.error.name]]</span>
</div>
<div>
<label for="name">Email: </label> <input id="email" type="text" name="email:email:required" value="[[+fi.email]]" /> <span>[[+fi.error.email]]</span></div>
<div>
<label for="subject">Theme: </label> <input id="subject" type="text" name="subject:required:stripTags" value="[[+fi.subject]]" /> <span>[[+fi.error.subject]]</span></div>
<div>
<label for="message">Text: </label><span>[[+fi.error.message]]</span> <br /> <textarea id="message" name="message:required:stripTags" rows="7" cols="55">[[+fi.message]]</textarea></div>
<div>
<input type="submit" name="Submit" value="Отправить" /></div>
</form>
I tried many examples, but they send to my email empty messages.
Here is my reciver-page code:
This message [[+name]] ([[+email]]) was sand with callback form:
[[+message]]
But in response I have only "This message () was sent with callback form: ".
Modx Revolution.
Sounds like modx is not seeing the name="email:email:required" as 'email' - put your CSS/JS validation in another attribute & also use the &validate attribute for the formit call.

Resources