Flask | HTML not rendering data sent via render_template - html

I am making a query to my database, creating an object, and sending it to my HTML via render_template.
The issue I am having is that the HTML is not displaying the information.
Relevant code:
#app.route('/profile', methods=['POST','GET'])
def profile():
if 'sno' in session:
if request.method == 'GET':
user = session['sno']
print(user)
cursor = conn.cursor()
cursor.execute("""SELECT * FROM `users` WHERE `sno` LIKE '{}'.format(user))
r = cursor.fetchall()
print(r)
return render_template('profile.html', r=r)
return redirect('/')
<form class="form" method="POST" action="/edit" name="Name">
<label>First Name</label><br>
<input type="text" class="form-control" name="fname" value="{{r[1]}}" >
<label>Last Name</label><br>
<input type="text" class="form-control" name="lname" value="{{r[2]}}" >
<label>Age</label><br>
<input type="text" class="form-control" name="age" value="{{r[3]}}">
<label>CNIC</label><br>
<input type="text" class="form-control" name="cnic" value="{{r[4]}}">
<label>gender:</label>
<input type="text" name="gender" class="form-control" value="{{r[5]}}"><br>
<label>Email</label><br>
<input type="email" class="form-control" name="uemail" value="{{r[6]}}">
<label>Password</label><br>
<input type="text" name="upassword" class="form-control" value="{{r[7]}}"><br>
<input type="submit" class="btn btn-primary btn-block btn-lg" name="submit" value="Edit">
</form>
Is there something I am missing or doing wrong?

I just did this
I had kept method = "POST".I changed it to method="GET"
It worked like boom

Related

Post method is not working in my HTML forms

my form is like this
<h1>Signup below</h1>
<form action="result" method="post">
{% csrf_token %}
<h3>Username</h3>
<input type="text" name="username" />
<h3>Email</h3>
<input type="email" name="email">
<h3>Password</h3>
<input type="password" name="password">
<h3>Rententer your password</h3>
<input type="password" name="password2">
<input type="button" />
</form>
and in my view folder code for this url is this
def register(request):
username=request.POST['username']
email=request.POST['username']
password=request.POST['username']
verify_password=request.POST['verify_password']
return render(request,'result.html')
but whenever I run this code it shows
screenshot of the error message
you should add in def
def register(request):
if request.method == 'POST':
.....
also when you want to return a response check this valid response form django
finally make sure you are using a valid URL in urls.py
<h1>Signup below</h1>
<form action="/result/" method="post">
{% csrf_token %}
<h3>Username</h3>
<input type="text" name="username" value="{{form.username}}" />
<h3>Email</h3>
<input type="email" name="email" value="{{form.email}}">
<h3>Password</h3>
<input type="password" name="password" value="{{form.password}}">
<h3>Rententer your password</h3>
<input type="password" name="password2" value="{{form.password2}}">
<input type="button" />
</form>
Note: form is context name from view so that's why I have written
{{form.username}}
And in views.py:
try this:
def register(request):
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid:
form.save()
username = form.cleaned_data.get('username')
# messages.success(request,f'Welcome { username }')
return HttpResponseRedirect('/users/')
else:
form = RegisterForm()
return render(request,'users/register.html',{'form':form})

Is it possible to keep the search text in form after search is done?

After search result is shown on the same page below the form, I want the search term to still stay in the search box, not to be wiped out. My search form template:
<form class="search" method="post">
<input name="query" type="text" style="max-width:700px" placeholder="Search over wbkg.." id="query" autocomplete="on" required>
<button type="submit"><i class="fa fa-search"></i></button>
<input type="radio" name="options" id="kmatch" value="kmatch" checked="checked">match </input>
<input type="radio" name="options" id="kextraction" value="kextraction"> extraction </input>
</form>
The flask view function:
#bp.route('/wbkg', methods=('GET', 'POST'))
def wbkg():
if request.method == 'POST':
query = request.form['query']
search_type = request.form['options']
results = search(query, search_type)
return render_template('nlp/wbkg.html', items=results)
return render_template('nlp/wbkg.html')
Is that possible?
Yes, just pass it back along with search results:
return render_template('nlp/wbkg.html', items=results, query=query)
And then display in template if it exists:
<input name="query" type="text" id="query" autocomplete="on" value="{{query}}" required>

Fetching data from template to send email

please this is the html file i am trying to fetch the data to be able to send it to my settings email
<form action="{% url 'send'%}" method="post" role="form" class="contactForm">
{% csrf_token %}
<div class="form-group">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
<div class="form-group">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
<div class="validation"></div>
</div>
<div class="form-group">
<textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
<div class="validation"></div>
</div>
<div class="text-center"><button type="submit">Send Message</button></div>
</form>
and this is my views.py which i collected the data from the html file
def sendEmail(request):
if request.method == 'POST':
name = request.POST['name']
email = request.POST['email']
subject = request.POST['subject']
message = request.POST['message']
send_mail(
"Contact Form",
message,
email,
settings.EMAIL_HOST_USER,
fail_silently=False
)
return render(request, 'app/index.html')
and my settings for the email which i am using gmail
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '*****#gmail.com'
EMAIL_HOST_PASSWORD = '******'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
and my urls.py is which i sent it to my url to the views
urlpatterns = [
path('',views.index,name='index'),
path('sendmail/', views.sendEmail, name='send'),
]

Specify dynamic url in form action

I am trying to specify a dynamic url in the actions properties of form.
The url will be dependant on what the user enters in the textbox. For example, if the user enters "Fred" as firstname and 1234 as courseId, then the url should be "/users/Fred/course/1234"
This is what I have so far but it is not reading the firstname and courseId.
<form action="/users/{{firstname}}/course/{{courseId}}" method="POST">
<input type="text" placeholder="firstname" name="firstname">
<input type="email" placeholder="email" name="email">
<input type="text" placeholder="courseId" name="courseId">
<input type="submit" value="Submit">
</form>
No php and ajax can be used.
You need javascript to do this
const template = (firstname, courseId) => `/users/${firstname}/course/${courseId}`;
document.getElementById('myForm').addEventListener('submit', function(s) {
s.preventDefault();
this.action = template(this.elements["firstname"].value, this.elements["courseId"].value);
this.submit();
});
<form action="placeholder" method="POST" id="myForm">
<input type="text" placeholder="firstname" name="firstname">
<input type="email" placeholder="email" name="email">
<input type="text" placeholder="courseId" name="courseId">
<input type="submit" value="Submit">
</form>

Integrating Neteller to my Asp.net website

I am banging my head for 2 days to integrate neteller into my website but could not get it,
Returned response in xml contain error saying Invalid merchantid/merchant key ,how can i get them?
<form method="post" action="https://test.api.neteller.com/netdirect">
<input type="text" name="version" value=" 4.1">
<input type="text" name="amount" size="10" value="3443" maxlength="10">
<input type="text" name="currency" value="USD" size="10" maxlength="3">
<input type="text" name="net_account" size="20" value="" maxlength="100">
<input type="text" name="secure_id" size="10" value="" maxlength="6">
<input type="hidden" name="merchant_id" value="43646">
<input type="hidden" name="merch_key" value="456453">
<input type="hidden" name="merch_transid" value="46436436" maxlength="50">
<input type="hidden" name="language_code" value="EN">
<input type="hidden" name="merch_name" value="fdghdfhgf">
<input type="hidden" name="merch_account" value="436346" maxlength="50">
<input type="hidden" name="custom_1" value="test123" maxlength="50">
<input type="hidden" name="custom_2" value="test123" maxlength="50">
<input type="hidden" name="custom_3" value="test123" maxlength="50">
<button type="submit" name="submit">Make Transfer</button>
</form>
At first You should not put your merchant credentials in hidden inputs for security reasons. Everyone can see your sensitive data. Make a POST on server side not client side.
merchant API key You can generate on merchant neteller panel in the Developer tab.