My Code:
file views.py:
def z(request):
htmltext = "hello <br><br> bye!"
return render_to_response('base.html', {'htmltext' : htmltext})
file main.html:
<html>
.
.
.
<div id="content">
{{htmltext}}
</div>
.
.
.
</html>
MY PROBLEM: as response I get this text in my html page (content):
hello <br><br> bye!
but i want:
hello
bye!
A potentially unsafe way to so this is to change your html code to be {{htmltext|safe}} however, depending on how the string is generated in your Python code, could lead to attacks on your website that inject arbitrary code into your HTML page.
The better option would be to put the HTML code <br> in the HTML file, so use something like {{foo}}<br><br>{{bar}}
Related
I am trying to scrape Instagram page, and want to get/access div-tags present inside of span-tag. but I can't! the HTML of the Instagram page looks like as
<head>--</head>
<body>
<span id="react-root" aria-hidden="false">
<form enctype="multipart/form-data" method="POST" role="presentation">…</form>
<section class="_9eogI E3X2T">
<main class="SCxLW o64aR" role="main">
<div class="v9tJq VfzDr">
<header class=" HVbuG">…</header>
<div class="_4bSq7">…</div>
<div class="fx7hk">…</div>
</div>
</main>
</section>
</body>
I do, it as
from bs4 import BeautifulSoup
import urllib.request as urllib2
html_page = urllib2.urlopen("https://www.instagram.com/cherrified_/?hl=en")
soup = BeautifulSoup(html_page,"lxml")
span_tag = soup.find('span') # return span-tag correctly
span_tag.find_all('div') # return empty list, why ?
please also specify an example.
Instagram is a Single Page Application powered by React, which means its source is just a simple "empty" page that loads JavaScript to dynamically generate the content in the browser after downloading.
Click "View source" or go to view-source:https://www.instagram.com/cherrified_/?hl=en in Chrome. This is the HTML you download with urllib.request.
You can see that there is a single <span> tag, which does not include a <div> tag. (Note: <div> inside a <span> is not allowed).
Scraping instagram.com this way is not possible. It also might not be legal (I am not a lawyer).
Notes:
your HTML code example doesn't include a closing tag for <span>.
your HTML code example doesn't match the link you provide in the python snippet.
in the last line of the python snippet you probably meant span_tag.find_all('div') (note the variable name and the singular 'div').
I am having a method in the Django view which has a loop. Each sentence from the loop is getting processed and the final output is sent to the html. Meanwhile i would like to show that the particular sentence is getting processed on the same html page.
views.py
def get_String(request):
Parameter = request.POST.get('search_Key')
Data1 = data.objects.all()
for item in Data1:
print(item.Sentence)
Output1 = returnResult1(Parameter ,item.Sentence)
Output2 = returnResult2(Parameter ,item.Sentence)
return HttpResponse(json.dumps({"searched_key":Parameter,"Result1": Output1,"Result2":Output2}),content_type="application/json")
The sentence one by one getting processed and the final output is shown. Meanwhile I need to show that, the current item.Sentence is only getting processed in the below HTML which is actually shown at the time of processing.
Loading.html
<html>
<head>
</head>
<body>
{% csrf_token %}
<div id="progress1"><font size="2">
Loading... the processing sentence is: {{ List }}</font>
</div>
</body>
</html>
Kindly let me know how to send the Sentence directly from views.py to Loading.html page without any request.
What you want to do must be done on the client side of your app.
a simple way to do this is to disable the submit input and add a spinner to it through javascript.
assuming you use jQuery and have fontawesome:
$(document).ready(function(){
$("SUBMIT BUTTON SELECTOR").click(function(){
var spinner = $("<i class='fa fa-spinner fa-spin'></spin>");
$(this).attr("disabled", "disabled").prepend(spinner);
});
});
I'm trying to get to grips with web2py/python. I want to get the user to fill in a search form, the term they search for is sent to my python script which should send the query to the blekko API and output the results to them in a new HTML page. I've implemented the following code but instead of my normal index page appearing, I'm getting the html response directly from blekko with '%(query)' /html appearing in it's search bar. Really need some help with this!
HTML form on the default/index.html page
<body>
<div id="MainArea">
<p align="center">MY SEARCH ENGINE</p>
<form name="form1" method="get" action="">
<label for="SearchBar"></label>
<div align="center">
<input name="SearchBar" type="text" id="SearchBar" value="" size = "100px"><br />
<input name="submit" type="submit" value="Search">
</div>
</form>
<p align="center"> </p>
Python code on the default.py controller
import urllib2
def index():
import urllib2
address = "http://www.blekko.com/?q='%(query)'+/html&auth=<mykey>"
query = request.vars.query
response = urllib2.urlopen(address)
html=response.read()
return html
I think you are misunderstanding how string formatting works. You need to put the address and query together still:
address = "http://www.blekko.com/?q='%(query)s'+/html&auth=<mykey>" % dict(query=request.vars.query)
Add a hidden field to your form, call it "submitted". Then reformat your controller function as such:
import urllib2
def index():
if request.vars.submitted:
address = "http://www.blekko.com/?q='%(query)'+/html&auth=<mykey>"
query = request.vars.query
response = urllib2.urlopen(address)
html=response.read()
return html
else:
return dict()
This will show your index page unless the form was submitted and the page received the "submitted" form variable.
The /html doesn't do anything. Glad your question got answered. There is python client code for the blekko search api here: https://github.com/sampsyo/python-blekko
I use xinha as WYSIWYG editor for html-content.
I sent html-articles via post-form to postgresql.
So far so good, they seem ok.
But when I receive and output from pg to an html page, I see double encoded, i.e. broken html code
like this
<p><a href="http://google.com">google.com</a></p> <p> </p> <p>
Any idea on where to search for the issue?
Thanks in advance
import HTMLParser
hp=HTMLParser.HTMLParser()
s="<p><a href="http://google.com">google.com</a></p> <p> </p> <p>"
print hp.unescape(s)
# u'<p>google.com</p> <p> </p> <p>'
I am handling with a Wikipedia-like project. I can convert the text file to html code using the markdown. My problem is, I want to render this html code in a html file. Here is my code,
class articles:
def GET(self):
form_name=web.input()
article_name=form_name.page
article_file_path=os.path.join('articles',article_name)
fp = open(article_file_path,'rU')
text = fp.read()
body = markdown2.markdown(text)
return render.article_files(article_name, body)
I'm passing article_name and body(html code) to article_files.html. The body looks like,
<h1>Hai</h1>
<p>Welcome<em>Ajay</em></p>
The problem is, the body displays as it is. That is the html code is printed in the screen with all tags. I want to render this html code (body) like,
Hai
Welcome Ajay
My HTML file is:
$def with(title,content)
<html>
<head>
<title>$title</title>
</head>
<body>
<form name="form" method="GET">
$content
</form>
</body>
</html>
HTML escaping is on by default in web.py templates. To turn it off, prepend the variable name with a colon:
<form name="form" method="GET">
$:content
</form>
Make sure there is no way for a potentially malicious user to feed arbitrary HTML into your unescaped templates.
You need to specify the mime type of the date you are sending to the browser, otherwise it doesn't know how to display it.
You can do this by adding the following line to your function:
web.header('Content-Type', 'text/html')