I have a web2py database with 20 entries and I would like to show only 5 entries per page.
My controller is-
def viewallposts():
if not request.vars.page:
redirect(URL(vars={'page':1}))
else:
page = int(request.vars.page)
start = (page-1)*5
end = page*5
user = auth.user_id
image=db().select(db.posts.ALL, orderby=~db.posts.created_on, limitby=(start,end))
return dict(user=user, image=image)
The next button in my .html file is -
<button><a href={{=URL(vars={'page':int(request.vars.page)+1})}}>Next</a></button>
But, I only see the first 5 entries and when I click next, it doesn't go to the next page. Where have I gone wrong?
The <button> in your View might be the issue. Try it this way:
<a class="btn btn-default" href="{{=URL(vars={'page':int(request.vars.page)-1})}}">Next</a>
Related
I'm learning to use Flask but I did not found a "easy solution" (by "easy solution" I mean easy for my noob level, few codes line) for this so I'm asking here for help.
How I can create a "back" button on the new generated page in flask?
This is my code:
calculator.py
from flask import Flask, request, render_template
app = Flask(__name__)
#app.route("/")
def my_form():
return render_template("calc.html")
#app.route("/", methods=['POST'])
def my_form_post():
num_1 = request.form['Number 1']
num_2 = request.form['Number 2']
result = int(num_1) + int(num_2)
render = render_template("calc_result.html")
#return render_template("calc_result.html")
return "The result from " + str(num_1) + " plus " + str(num_2) + " is: " + str(result) + "."
app.run(host='127.0.0.1', port=5000)
calc.html
<html>
<body>
<form method = "POST">
<input name = "Number 1">
<input name = "Number 2">
<input type = "submit">
</body>
</form>
The code is working, is receiving the numbers and doing the math but the problem is, I'm not able to generate a "back" button in the new generated page with the result.
If I put for example 10 + 10 in the calc.html, I will receive:
The result from 10 plus 10 is: 20.
I would like to put a "back" button on this page or learn how to generate a new button in the new gelerated page, so I would receive something like:
The result from 10 plus 10 is: 20.
Back
Sorry the english.
Edit:
This is what user see on the first page:
What user see in first page
After put the two numbers to sum, they see:
Result page
I want allow user to come back to first page and be able to do another sum.
In both pages the link are the same: http://127.0.0.1:5000/
in your 'calc_result.html' file, make a link or button and use the 'url_for' command to bring you back to your 'calc.html' page. Flask allows you to insert Python code into your HTML via jinja templates. Check out the quickstart guide here. So:
Back
You can use the following code:
<button onclick="goBack()">Go Back</button>
<script>
function goBack() {
window.history.back();
}
</script>
Please refer this.
<a href="/">
<form action="/">
<input class="btn" type="button" value="HOME" />
</form>
</a>
I was trying to display photos and pdf files in a django project using <embed> but pdf file is not displaying
Here is what i was doing
This is view function
#login_required
def view_documents(request):
students = Student.objects.filter(user=request.user)
return render(request, 'student/view_documents.html',{'students':students})
and then I used for tag in template
{% for student in students %}
#lines of code
{% endfor %}
and to display the pdf file i have used
<embed src="{{student.adhar_card_pdf.url}}" width="800px" height="400px" type="application/pdf"/>
but it was showing some kind of error like localhost refused to connect
i also used iframe
<iframe src="{{student.adhar_card_pdf.url}}" style="width:718px; height:700px;" frameborder="0"></iframe>
But image is displaying in the browser using same code without any error
<embed src="{{student.photo_jpg.url}}" width="230px" height="250px" />
Why pdf file is not displaying ?? Please help me
Thanx in advance.
Edited: In console it is showing -- Refused to display '' in a frame because it set 'X-Frame-Options' to 'deny'.
This is how it is displaying pdf
X_FRAME_OPTIONS = 'SAMEORIGIN'
Insert the above line in settings.py just before the MIDDLEWARE section
and for the file path in embed tag write path as
{{student.file.get.photo_jpg.url}}
And then give a hard reset in Chrome or any browser you are using.
It worked for me I hope it works for you as well.
I'm using mechanize in my Python script to access web page sources. One of the pages I want to access uses two buttons to ask if I am 21 or not. I'm very new to python, and after searching all day, I can't figure out how to select the correct button in the same form because they have the same name. Printing the forms yields:
<SubmitButtonControl(over21=no) (readonly)>
<SubmitButtonControl(over21=yes) (readonly)>>
The HTML for the buttons, which are in the same form:
<button class="c-btn c-btn-primary" type="submit" name="over21"value="no">no thank you</button>
<button class="c-btn c-btn-primary" type="submit" name="over21"value="yes">continue</button>
What I've tried:
thisone = br.select_form(name='over21=yes')
br.submit(thisone)
Which yields error:
File "build/bdist.linux-armv7l/egg/mechanize/_mechanize.py", line 524,in select_form
mechanize._mechanize.FormNotFoundError: no form matching name 'over21=yes'
I've also tried
br.submit(nr=1)
which was suggested by another post, but I believe that is selecting a whole different form for me.
Any advice welcome! And yes, I am over 21! Thanks,
On my html page I have a button, and a number being displayed. I want the number to increase by 1 every time the button is pressed. This is the code that I have so far, but for some it doesn't work.
mypage.html
<form method='get' action='#'>
<input type="submit" value="add" name="add"/>
</form>
<h1>{{ p }}</h1>
views.py
p = 1
def mypage(request):
global p
my_dictionary = {
"p" : p,
}
if request.GET.get('add'):
p = p+1
my_dictionary = {
"p" : p,
}
return render(request, "mypage.html", my_dictionary)
On every request a new instance of the python script is run which would result in your global variable not being retained. You should store the count in a database and recall the value on every request. There are options other than a database but the basic idea of needing some sort of persistent storage still exists.
Alternatively, you can have the requester keep track of the count and provide the current count in the request. You would then add one to the value provided in the request. This would require some additional code on the front end to maintain the count, and even then, if the user closes the browser the count will be reset.
As dm03514 pointed out, you can can also use sessions like so:
if 'p' in request.session:
request.session['p'] = request.session['p'] + 1
else:
request.session['p'] = 1
The p value needs to be kept track of across requests.
2 possibilities are
resend the data every request
store the data in a session and just make increment /decrement requests
Should the variable be shared between all users? Or just the current user?
It should work if there is a single instance of django running (although it is not the recommended approach like other's have suggested).
It didn't appear to work is because you used action='#', and the browser refused to resubmit the form when your URL already ends with #. Instead you can use the following template:
<form method='get' action='.'>
<input type="submit" value="add" name="add"/>
</form>
<h1>{{ p }}</h1>
I'm making an application that receives images.
I'm making it in a pretty hacky way, where this is the HTML:
<body onload="javascript:setTimeout('location.reload(true);', 1000);" >
<div class="container">
<img class="img0" src="{{ uno }}"/>
<img class="img1" src="{{ dos }}"/>
<img class="img2" src="{{ tres }}"/>
</div>
</body>
And this is in views.py:
def main(request):
x = get_newest_pics()
uno = x[-1]
dos = x[-2]
tres = x[-3]
context = { 'uno' : uno, 'dos' : dos, 'tres' : tres }
return render(request, 'index.html', context)
I'm sure there's a better way to go about this, but I'm very new to Django and I don't know how. At this point, the page is just flickering every second showing the same images, when really I just want it to refresh whenever there is a new image. Is there a way to consistently call get_newest_pics() and refresh just the images, rather than the whole page? Or even just a way to make the page stop flickering?
The way to do this is to implement ajax on your front end, and then request for new images at an interval, once a new image is found, update the container where you are showing your images and add the new (available) image.
Have a look at the django-dajaxice library to help you with the "wiring" of your front end to django correctly.
The way you have written your code, all three images are sent at once to your page, and your javascript snippet is in effect just refreshing the page very quickly, which is why you see the flickering effect.
You could do a "hack" and create a separate view for each image in django, then call each view on an interval using javascript - it would have the same end result but really inefficient in terms of code.