How can I match the url in html - python

<form action='/[0-9]+' method="POST">
<input type="submit" value="delete question" name="delete">
</form>
what above is the html template I am using for the appengine project. Besides that, i created a web request handler class to handle this request. ('/[0-9]+',QuestionViewer), it is supposed to catch any url in digits. However, turns on that after I click on the delete button above, my page is directed to some url like main/[0-9], I dont know if I can use regex in the django template, or is there a away that my QuestionViewers class can catch the url in digits? since my url associated with the html page is dynamic, like the parts after / ,like /13,are changing accordingly and I cant do that only works for page 13 but not for /14 or something like these. Hope I make it clear. any helps? Thank you a lot.

That doesn't really make sense. You want to submit your form to a regex rule? What would it match against?
No, the form needs to submit to a specific url. Right now, it's trying to submit to /[0-9]+
If I understand what you are saying, and you want to submit from a url such as /13/ to your QuestionViewer at /[0-9]+, simply submit without the action attribute or set it to "" to post to the current url.
Note that if you want to use the digit captured in your regex, you need to surround your regex in parenthesis such as '/([0-9]+)/$', QuestionViewer or use a named regexp /(?P<id>[0-9]+)/$ to pass in an argument of id equal to the matched regex to QuestionViewer.
http://docs.djangoproject.com/en/1.0/topics/http/urls/#how-django-processes-a-request

The value of the action attribute must be a valid URL.
I think what you want is to generate an actual number for the action url; a number that is the number of the question that you want to delete. For example:
<form action="/1234" method="POST">
You will need to change your code to make sure you do this.

Related

how to pass value into html form in Bottle?

I’m novice to python and Bottle but I’m trying to develop a simple web application which will inventory items in boxes that company receives.
Using Bottle I was able to create a form which has 2 text boxes and one ‘Save’ button. I scan box ID and it get into text box1. Then I scan item ID and it get into text box2. Then I click on Save button.
It works … but after I click on ‘Save’ the form get reloaded i.e. it open blank page and I have to move back page, delete the content from text box1 and do it again until I switch to the next Box which will start with empty box1 and box2
My request: I want that every time I click on ‘Save’ button it submitted data into my database but the form stay intact i.e. not reloaded and the content of text box1 get empty. Then I could just scan next item and so on until I complete all items.
Could please someone help me with that?
Here’s how my code look for now in Bottle template:
<form action="/accession" method="GET">
Scan Box: <input type="text" size="18" name="package"> &nbsp &nbsp &nbsp
Scan Item: <input type="text" size="13" name="sample">
<input type="submit" name="save" value="Save" >
**
I slightly changed the form and now it behaves differently i.e. when I click on "Save" it stays on the same page ( which is OK ) but it empties the content of both text boxes.
I need that only one text box be cleared but another one keep the content. How could I do it?
Thanks
**
I noticed that I could use 'value' attribute with "text" box .. like this:
Scan Box: <input type="text" value="123" name="package">
In my case the value "123" should be dynamic. I do have the value in my python script that I want to replace with "123" but I don't know how to pass it into the form.
Could someone help me?
thanks
You should use a template. Here are the docs for Bottle's built-in templating; I happen to prefer Jinja2 but you can decide which to use once you've mastered the concept.
Basically, you'll create a template file that is the html you want to return. It will include something like this:
Scan Box: <input type="text" value="{package}" name="package">
And your Bottle function (which you haven't posted, so I'm making a guess here) will look something like this:
#route('/myform')
def submit():
the_package = zzz # get the value however your application chooses
return template('form1', package=the_package) # your template file is form1.tpl
The value of the_package will automatically be substituted where {package} appears in your template file.
Please try the template examples in the Bottle documentation and let us know if you have any more questions.

Sanitize user input for inclusion in href attribute?

I would like to accept a user inputted url and display it in the href attribute part of the link tag. ie.
My link name
But I would like to make sure that it doesn't have any malicious content as far as inject script tags and the like. What is the best approach to sanitizing the user_input part?
From what I can tell:
django.utils.html.escape would escape &'s which is bad.
django.utils.http.urlquote and django.utils.http.urlquote_plus would escape the : part of http:// amoungst other things which seems bad.
Perhaps the best approach is urlquote_plus with some safe characters specified?
You can use the template tag: safe.
Let's say that your post context variable is:
user_input = some_valid_url
Grab user_input, and add the html to make it a link and reinsert it when saving the post. So the saved post is:
link_text = <a href=user_input>Link</a>
And then use safe in your html template:
{{ link_text|safe }}
Here is the documentation link for safe template tags:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#safe
I was over thinking the problem. It turns out that using django.utils.html.escape is fine as it results in HTML that has link tags with an href attributes which might have & in them instead of & but the browser handles this fine.
I thought I needed to find a way to have & in there as urls don't have & in them.
My final code is:
from django.utils.safestring import mark_safe
from django.utils.html import escape
....
output = '<li>%s</li>' \
% (escape(entry['url']), escape(self.link_display(entry)))
return mark_safe(output)

Django Clean URLS and Form Data

I must have some sort of misconception of how GET variables can be manipulated in django, but here goes:
(1) I have a search form that has two required parameters, and one optional parameters.
The form sends to /search/ like so :
<form action="/search/" method="GET">
However, the urls that result from this look something like
http://mylifeforregex:8000/search/?keyword=keyword&columns=name&exact=on
I'm not able to match it in my urlpatterns with the following line :
url(r'^search/(?P<keyword>\w+)(?P<columns>\w+)(?P<exact>\w+)?$', 'home', name='searched')
So if someone could tell me what exactly I'm doing wrong with this regex, it'd be much appreciated.
(2) The more interesting question I have personally is : is there any way I can change the way the form data will be presented? Is it possible to have the form query result in a url that looks like
http://halp:8000/search/keywordquery/columnquery/optionmarked
In this case, none of the querystring parameters that are appended to the URL will match your pattern, because the pattern is simply not the same.
You would need to pick those up in your 'home' view as such:
[variable] = request.GET.get([key])
To get the url pattern you're expecting, you'd have to do a POST to your view that handles the form to get the values, and then redirect to '/search/[keyword]/[column]/[exact]/'

How to manage multiple HTML elements with same name using WTForms, Jinja & Python

I had defined an HTML template with Jinja, where I had defined three (3) textfields with same name. Then in the backend using the get_all method I collected all the values.
Then in order to add form validation, etc, I added WTForms library. Then I defined a Form as:
class RoleForm(BaseForm):
name = fields.TextField(_('Name'))
And in the HTML page I rendered this element three (3) times.
Now, when I submit the form and the validation fails, I re-render the template using the form as input. But instead each element has the value I had entered, all textfields have the value of the 1st textfield.
Moreover if validation is ok, I use form.name.data, which does not give me all the data from the three textfields, but only the first one.
Do you know how I can handle such situation?
Thanks in advance!
You are looking for the wtforms.fields.FieldList field enclosure:
class RoleForm(BaseForm):
name = fields.FieldList(fields.TextField(_('Name')), min_entries=3)
What you need to do is to create HTML inputelements like this for instance:
<input name="row-{{ loop.index0 }}" type="checkbox">
It will render inputs like the ones below:
<input name="row-0" type="checkbox">
<input name="row-1" type="checkbox">
Inside a loop or something like that. Then you'll be able to retrieve the Form contents:
class ListForm(Form):
row = FieldList(fields.TextField('Row'))

With flask/jinja, what is a viable way to safely render a link inside a user generated block of text?

Think twitter where you paste a link next to some plain text and when your tweet is rendered, that url is now a clickable link.
Do I:
replace jinja's autoescape with my own by scanning the text for html tags and replacing them with the html entity code
use a regular expression to detect a url contained in the text and replace it within an a href=
what would this expression look like to detect any # of .tld's, http/https, www/any subdomain?
and render this all as ¦safe in the template?
Or is there a python/flask/jinja 'feature' that can better handle this kind of thing?
Jinja has a filter built-in called urlize that should do exactly what you want.

Categories