I have a flask html page that retrieve applicants information from the database sqlalchemy, I want to click on a name of applicant from the first page and another page open with specific information for this applicant.
I sent a query for the first page and it works like I want, but I struggled in the second page, I did not know how to send that one record
this is the code of the first page, applicant-report page is the second page
{% for report in reports %}
<div class="rec_box">
<a class="text" href="/applicant-report">
{% for a in applicant %}
{% if a.phone == report.applicant_phone %}
{{a.name}}
{% endif %}
{% endfor %}
</a>
<p class="rec_small">O: {{(report.op*100)}}%</p>
<p class="rec_small">C: {{report.co*100}}%</p>
<p class="rec_small">E: {{report.ex*100}}%</p>
<p class="rec_small">A: {{report.ag*100}}%</p>
<p class="rec_small">N: {{report.ne*100}}%</p>
</div>
{% endfor %}
Pass parameters between pages is usually using the URL, since this method is showed to the end user, I suggest not include any passwords or something like that
In the link you will redirect the user ends in something like
<a class="text" href="/applicant-report?phone={{a.phone}}">
Where the a.phone is the value of your variable. To read this url in the other page, you use something like
applicant_phone = request.args.get('phone')
applicant = session.query(Applicant).filter_by(phone=applicant_phone).first()
If you want to add another parameter to the url you concat using & between them, like
<a class="text" href="/applicant-report?phone={{a.phone}}&{{a.contact}}">
And so on.
Hope you find this usefull
Related
Here I iterate data from my database in mongodb i just want that when my add to cart button is clicked it gets stored into another database named cart and gets displayed on another page
{% for data in product_data %}
<div class="shop-item" >
<span class="shop-item-title" id="title-item">{{ data.title }}</span>
<input type="image" class="shop-item-image" id="image-item" src={{ data["img_file"] }} onclick="takethatpage();">
<div class="shop-item-details">
<span class="shop-item-price" id="price-item">{{ data["price"]}}</span>
<button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
</div>
</div>
{% endfor %}
I will not spoon feed but let me tell you the approach for it.
Firstly you would need an API implemented at the server end to accept products to be added to the cart for the respective user.
Secondly, for your template there needs to be an AJAX request fired each time the person click on the add to cart button.
I have a template where the users pass a query and select a couple of checkboxes (these can range from 1-100). Then, my view does the following:
def search(request):
results_list = search(request.GET.get("q", ""), request.GET.getlist("c"))
# Pagination
paginator = Paginator(results_list, 10)
page = request.GET.get("page")
results = paginator.get_page(page)
return render(
request,
"web/search/show.html",
{
"query": query,
"results": results,
},
)
The issue arrises because both the search and the presentation of the results happen on the same page. Therefore, when I want to include pagination, in my template, I have to do the following:
<div class="pagination">
<div class="step-links">
{% if results.has_previous %}
Previous
{% endif %}
{% if results %}
<span class="current">
Page {{ results.number }} of {{ results.paginator.num_pages }}
</span>
{% endif %}
{% if results.has_next %}
Next
{% endif %}
</div>
</div>
Please consider the usage of request.GET.urlencode because when the user wants to either go to the previous or next page the same query and checkboxes need to be passed. However, this creates a bug when the user goes past the second page, because the ?page=3&page=2 keep on piling up. Can someone point me in the right direction for solving this issue?
An easy solution would be to repeat every GET parameter you need to handle in the link rather than use the full querystring, something like:
Next
A more elegant solution would be to create a Django template tag to build the URL based on the current state of the querystring, something like what is described in this article: Dealing With QueryString Parameters.
I'm building a Netflix like website for my Devops course. I made a Python list of dictionaries (Mockfilms) to define my films, and want to populate a database (Ratings) with reviews in preparation for sending data in the format :filmid: :userid: :rating: to a recommendation engine.
My index page is a list of film images with a link to a review form under each one. I want each review form to appear on a different url (/review/ID where ID is saved in mockfilms as oid). In order to do this I want to access mockfilms.oid, then pass it to the view function to make the url for the form. Once the form is complete I then want to add this ID to the Ratings database. Here is what I have so far:
Index:
{% extends "base.html" %}
{% block content %}
<h1>Hello, {{ current_user.username }}! Welcome to our extensive video library:</h1>
{% for film in mockfilms %}
{% set ID = film.oid %}
<div>
<a href = {{ film.video }}>
<img src = {{ film.image }} alt = "doh" style = "width:200px;height:200px;border:0;">
</a>
</div>
<div>
">Leave a review here!
{% endfor %}
{% endblock %}
Route:
#app.route('/review/<ID>', methods = ['GET', 'POST'])
#login_required
def review(ID):
form = ReviewForm()
if form.validate_on_submit():
review = Ratings(User_id = current_user.id, Score_given = form.score.data, Film_id = ID)
db.session.add(review)
db.session.commit()
flash('Thanks for your review')
return redirect(url_for('index'))
return render_template('review.html', title='Review Page', form=form)
The following error is what I get when I run it:
File "/home/jc/Desktop/Lokal/DevopsAssig/microblog/Kilfinnan/lib/python3.5/site-packages/werkzeug/routing.py", line 1768, in build
raise BuildError(endpoint, values, method, self)
werkzeug.routing.BuildError: Could not build url for endpoint 'review'. Did you forget to specify values ['ID']?
From this I assume that the issue is with the ID variable within this template. My searchings and learnings led me to believe that {% set %} in the index template would let me declare the ID variable and then use it in the dynamic.
Try this:
{% block content %}
<h1>
Hello, {{ current_user.username }}!
Welcome to our extensive video library:
</h1>
{% for film in mockfilms %}
<div>
<a href="{{ film.video }}">
<img src="{{ film.image }}" alt="doh" style="width:200px;height:200px;border:0;" />
</a>
</div>
<div>
<a href="{{ url_for('review', ID=film.oid) }}">
Leave a review here!
</a>
</div>
{% endfor %}
{% endblock %}
Ultimately your solution was quite close, but it is not necessary to use the Jinja set command when you need to pass the variable into url_for() function using the keyword for the parameter. You could still do it using {% set ID = film.oid %} but it would be a bit superfluous.
Try to provide key=value arguments into your url_for function.
Something like this
">Leave a review here!
Also Flask have a great documentation, Flask docs
So I have been tinkering for a while with jinja2 and google app engine. I am just writing a small toy app on my spare time; the app has a webpage that displays the ten most recent posts along with its comments.
All of the blog posts print fine onto the page by using the following within the google data store, after the Post object is created and stored in the database of course. I use the following query to get the ten posts to be displayed.
recent_blog_posts = ndb.gql("SELECT * FROM Posts ORDER BY created_at
DESC LIMIT 10;")
The blogpage.html code below:
{% block content %}
{% for post in recent_blog_posts %}
<div>
<h3>{{post.title}}</h3>
<pre>
<p style="max-width: 100%;">{{post.post}}</p>
</pre>
<p>By: {{post.by_user}}</p>
<!-- this is where I want the comments to go (explained below)-->
<h4>Leave A Comment:</h4>
<form method="post">
<textarea name="comment" value="{{comment}}" style="height: 50px; width: 200px;"></textarea>
<input type="hidden" name="post_key" value="{{post.key}}">
<br>
<button>Comment</button>
</form>
</div>
<hr>
{% endfor %}
{% endblock %}
I just iterated over the ten objects in the query above to print all of the blog posts. However, this is where it gets tricky for me.
I create a new Comment instance with the following:
new_comment = Comments(comment = comment,
user = user.name, parent = ndb.Key(Posts, int(post_key)))
new_comment_key = new_comment.put()
When I print the new Comment instances onto the screen, just to see, they all print out correctly with the right parent and their own ids.
Now this is where I am not sure on how to take each Comment instance and print it with its corresponding post. How can I accomplish that?
I have searched everywhere, and even added this to the html template above. (In place of the comment from the html template above)
{% for comment in comment_query %}
{{comment.comment}}
{% endfor %}
With the query below:
recent_comments = Comments.query(ancestor=ndb.Key(Posts, int(new_string))).order(-Comments.created_at).fetch(limit=3)
This obviously just prints out all of the Comments instances for all of the Posts instances on the page.
Thanks In Advance
Just form the output list in the backend itself.
recent_blog_posts = ndb.gql("SELECT * FROM Posts ORDER BY created_at
DESC LIMIT 10;")
posts_with_comments = []
for post in recent_blog_posts:
recent_comments = Comments.query(ancestor=post.key).order(-Comments.created_at).fetch(limit=3)
posts_with_comments.append([post, recent_commmnets])
Then iterate over posts_with_comments in the template like
{% for post,comments in posts_with_comments %}
<div>
<h3>{{post.title}}</h3>
<pre>
<p style="max-width: 100%;">{{post.post}}</p>
</pre>
<p>By: {{post.by_user}}</p>
<p> Comments: </p>
{% for commnet in comments %}
{{ comment }}
{% endfor %}
{% endfor %}
I would like to build my personal web pages in Pelican, but I am missing one functionality. I would love to have an introductory page for some/all categories on the pages.
For example - I would like to build a page for my grant project, where posts are related to activities and/or published papers, but I would also like a single page saying something about the grant project and keep this page as the title page of this category.
Is this possible (easily) in Pelican framework? If not, can you suggest better static pages framework that works in combination Markdown+Python?
This is actually very easy with pelican. The plugin Auto Pages defines three extra content folders: One for authors, one for categories and one for tags.
Say you, John Smith, wanted to have extra information about your person when clicking on your name. Then, you would add a file called authors/john-smith{.rst|.md} with this extra information. No HTML, but only the contents you want to provide about your person. These contents are then read in, transformed and presented to the template engine as author.page.
Now it is about your templates to also use this variable.
In my theme, I simply modified theme/templates/author.html to not show the combination of "featured article" and "other articles" related to my author, but to show author.page.content and "all articles" related to my author instead.
Short extract of my theme/templates/author.html:
<aside id="featured" class="body">
<article>
<h1 class="entry-title">{{ author }}</h1>
{{ author.page.content }}
</article>
</aside>
<section id="content" class="body">
<!-- removed the apostrophe for SO highlighting reasons-->
<h1>Authors articles</h1>
<hr/>
<ol id="posts-list" class="hfeed" start="{{ articles_paginator.per_page - 1}}">
{% for article in articles_page.object_list %}
<li><article class="hentry">
<header>
<h1><a href="{{ SITEURL }}/{{ article.url }}" rel="bookmark"
title="Permalink to {{ article.title|striptags }}">{{ article.title }}</a></h1>
</header>
{% include 'article_infos.html' %}
{{ article.summary }}
<a class="readmore" href="{{ SITEURL }}/{{ article.url }}">read more</a>
{% include 'comments.html' %}
</article></li>
{% endfor %}
</ol>
{% if articles_page.has_other_pages() %}
{% include 'pagination.html' %}
{% endif %}
</section>
You can do the exact same thing for categories and tags using the procedure described above. For the template, just use the existing index.html and adapt it to your needs.