Replace HTML Form Element? - regex

I have this HTML code:
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<form action="http://www.aweber.com/scripts/addlead.pl" method="post">
<input id="email" class="text" type="text" size="20" value="Enter your best email" name="email" onblur="if (this.value == '') {this.value = 'Enter your best email';}" onfocus="if (this.value == 'Enter your best email') {this.value = '';}" />
<input type="hidden" name="meta_web_form_id" value="">
<input type="hidden" name="meta_split_id" value="">
<input type="hidden" name="listname" value="">
<input type="hidden" name="redirect" value="">
<input type="hidden" name="meta_adtracking" value="">
<input type="hidden" name="meta_message" value="1">
<input type="hidden" name="meta_required" value="email">
<input type="hidden" name="meta_tooltip" value="">
<div style="padding-top:5px;">
<center><input class="button1" id="submit" type="image" src="red_getwaitinglist.png" name="submit" value="Get Instant Access"/></center>
</div>
</form>
</BODY>
</HTML>
and I'm using this Function to replace the element using RegEx:
Function StripTags(ByVal html As String, ByVal replace As String) As String
Return Regex.Replace(html, "<form.+?</form>", replace)
End Function
but it doesn't seem to work. It returns the same code (no replacement).
I don't know much of regex so I'm assuming the RegEx syntax might be wrong.
Can someone help me with that or show me where I'm wrong?

Append the RegexOption Singleline. Without this . will not match newlines.
Return Regex.Replace(html, "<form.+?</form>", replace, RegexOptions.Singleline)
See RegexOptions Enumeration for more details.

Related

Show model property if model exists and some string if not

To make it clear I want do the following with just one line:
{%if model %}
<input type="text" class="form-control" id="title" value="{{model.title}}" placeholder="Enter Title">
{% else %}
<input type="text" class="form-control" id="title" value="" placeholder="Enter Title">
{% endif %}
I tried this:
<input type="text" class="form-control" id="title" value="{% model.title if model else "" %}" >
And it didn't work:
Invalid block tag on line 15
I don't think I have to make a custom template tag for this simple kinda things.
Thanks in advance
You may try the following:
<input type="text" class="form-control" id="title" value="{%if model %} {{ model.title }}{% endif %}" placeholder="Enter Title">

Django: insert multiple input text and file

I have recently tried to learn Django and I want to know it's possible to insert multiple input with this form structure to table database? if it's possible, can you tell me how it is? Thank you!!!
<form class="log-in" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div>
<input type="text" name="title">
<input type="file" name="image">
</div>
<div>
<input type="text" name="title">
<input type="file" name="image">
</div>
<div>
<input type="text" name="title">
<input type="file" name="image">
</div>
<button type="submit" class="btn btn-info">Submit</button>
</form>
What you are looking for are formsets: docs.djangoproject.com/en/3.0/topics/forms/formsets

How to catch errors from form in Django?

I have a collapse div with login/sign-up forms. How I can catch errors if they are and show them on page?
My form in html:
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<p><label for="id_username">Username:</label> <input
id="id_username" type="text" name="username" maxlength="30" /></p>
<p><label for="id_password">Password:</label> <input
type="password" name="password" id="id_password" /></p>
<input type="submit" value="Log in" />
<input type="hidden" name="next" value="{{ request.get_full_path }}"
/>
</form>
Dude use the inbuilt login, it offers you with encryption of password. the method your using is little messy i would recommend to go with django inbuilt authentication. here's the link,
https://simpleisbetterthancomplex.com/tutorial/2016/06/27/how-to-use-djangos-built-in-login-system.html

django html form that is showing error this field is required

the post method is not getting the username record. the get method is working fine but html form is showing the following error:
This field is required
<h2>Users</h2>
<ul>
<form method="post" action=""><input type='hidden' name='csrfmiddlewaretoken' value='8gQo0iGRTDE7kayhFJqj2fOt7UkejlkG' />
<li>
<input type="text" value= mayur><br>
<input type="submit" value="follow" />
</li>
<li>
<input type="text" value= mayurnitrr><br>
<input type="submit" value="follow" />
</li>
<li>
<input type="text" value= lokesh><br>
<input type="submit" value="follow" />
</li>
</form>
</ul>
<p align="center">Back</p>
You haven't shown how you handle these but none of your inputs have a name parameter which you need to provide
<input type="text" name="somethingdescriptive" value="lokesh"><br>
request.POST should now contain the key "somethingdescriptive"

password vailidation failing using mechanize/python

I'm trying to log on onto a website with mechanize for python; however when presented with correct credentials in the form it fails to log in.
Here's what I'm doing following the mechanize tutorial:
def logon(self):
baseurl = "https://www.website.com/login"
br = mechanize.Browser(factory=mechanize.RobustFactory())
br.open(baseurl)
br.select_form(nr=0)
br.form["username"] = self.username
br.form["password"] = self.password
response = br.submit()
print response.geturl()
That should take me to the website.com/dashboard but it's saying at the website.com/login page as though it failed. I can log in manually, and everything passed through the debugger so I'm not sure what I'm doing wrong. I did some digging, and I checked through the form HTML for javascript, and the form doesn't seem to have any attached to it.
Here's the form:
<form method="post" class="login-form">
<fieldset>
<div class="row form-section">
<label for="email">Email Address</label>
<span class="input-text"><input class="required-email" type="text" id="email" value="" name="username" /><span class="required">Required Info</span></span>
<!--<input type="text" id="email" name="username" placeholder="Enter Email Address" class="text" aria-required="true" required="required" style='float:right;' size="30" value="" /><p> -->
</div>
<div class="row form-section">
<label for="pass">Password</label>
<span class="input-text"><input class="required-text" type="password" id="pass" value="" name="password" /><span class="required">Required Info</span></span>
<!--<input type="password" placeholder="Enter Password" class="text" size="30" autocomplete="off" value="" id="password" name="password" style='float:right;' aria-required="true" required="required" />-->
</div>
<div class="row row-btn">
<input type="submit" value="Log in" />
<span>Don't have an account?<br><br>Forgot your password? Didn't get your activation email?</span>
</div>
</fieldset>
<input type="hidden" value="com_oms" name="option">
<input type="hidden" value="login" name="task">
<input type="hidden" name="return" value="">
<input type="hidden" name="9402951e32acb8d73d4a6972ae540c63" value="1" />
</form>
I know there were some similar questions asked but there haven't seemed to be any clear answers that my searching has brought up. Can anyone shed some light on what is going on?
Would like to add as a comment but am not able to:
I doubt if the few hidden inputs especially the string serve as a verification to prevent CSRF attacks. Did you try submitting the hidden fields back along with email/password? That might help you out.