create account

Web development with python #3 : Get method, Query parameter and Navigate Routing by duski.harahap

View this thread on: hive.blogpeakd.comecency.com
· @duski.harahap · (edited)
$23.05
Web development with python #3 : Get method, Query parameter and Navigate Routing
#### Repository
https://github.com/python

#### What Will I Learn?
- Get method and Query parameter
- Navigate routing and access static files

#### Requirements
- Basic Python
- Install Python 3
- Install Flask


#### Resources
- Python - https://www.python.org/
- Flask - http://flask.pocoo.org/
- Jinja2 -http://jinja.pocoo.org/docs/2.10/

#### Difficulty
Basic

### Tutorial Content

This tutorial is a continuation of the previous [tutorial](https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-2-templating-jinja2-and-method-post-on-routing-system-1542987551736) series, so for those of you who want to start, I suggest you look at the curriculum below. in the previous tutorial, we discussed the **POST** method, in this tutorial, we will discuss the **GET** method and the things we can do by templating **jinja2**, we will do the logic in the jinja2 template. let's just start the following tutorial.

### GET Method

In previous [tutorial](https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-2-templating-jinja2-and-method-post-on-routing-system-1542987551736), We have used the **POST** method to take the value from the post method. of course, we can also take the values in the **GET** method, with the get method we can use *queries parameter* to take a value. We can give an example if the following parameter query ```/profile?username=milleaduski```. ```username``` is **key** and ```milleaduski``` is **value**.

- **USE Request.args**

to retrieve values from the **GET** method, we can use the function of the request method, namely ```request.args.get()```. for more details, we can see the following code:

URL: ```'/profile?username=milleaduski'```

**app.py**

```
@app.route('/profile')
def profFunc():
	user = request.args.get('username')
	if not search:
		return render_template('index.html')

	return 'The query params is '+ user
```

- We can create a new routing to make a test. I made a URL ```/profile```.

- We will take the value from the *query parameter* with the key ```username```. to take the value we can use the method ```request.args.get()```. The result of ```request.args.get()``` *is not a string* but a *dictionary.*

- We can test by displaying the value in the **user** variable, If there is no error then we can see the results as below:

![ezgif.com-video-to-gif (4).gif](https://ipfs.busy.org/ipfs/QmWLex66PLfh8rpqQSMCQPptg7YHvaBzMRbJhviuDYcSfu)


### Logic on jinja2

When we create an HTML template in a view, we often want to use the same template but in different conditions to be able to use the same template with slightly different content, for that we need to do some logic in our template, in this tutorial we will give the logic of the jinja2 template. to separate logic templates and HTML scripts we can use ```{%%}```. we will pass values through routing as follows:

**app.py**

```
@app.route('/profile')
def profFunc():
	user = request.args.get('username')
	return render_template('index.html', user = user)
```

as we have learned in the previous [tutorial](https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-1-flask-initialization-and-routing-system-1542726589553) we can pass a value to the template in this way ```render_template('index.html', user = user)```. 

after we pass the value we can use it in the **index.html** template. We can see how to use it as an example below:

```
<!DOCTYPE html>
<html>
<head>
	<title>Template Jinja2</title>
</head>

<body>
	<h1 style="color:blue;">Welcome to my profile</h1>
	{% if not user%}
		<h2>This is profile page</h2>
	{% else %}
		<h2>This is page of {{user}}</h2>
	{% endif%}
</body>
</html>
```

by using ```{%%}``` we can separate HTML scripts with **jinja2** logic. Now, we can make logic if as usual, we will choose what element is rendered in the template when we do the **GET** method. If there is no error then we can see the results as shown below:

![ezgif.com-video-to-gif.gif](https://ipfs.busy.org/ipfs/QmY2i2xB9g1JPL1rrH327qAJbNcwK9zPcRsx716uZeeUV6)

as we saw in the picture above we can get the value of the parameter query using the function ```request.args.get()```

### Directory asset in Flask

We have learned the **GET** and **POST** methods, then we will learn how to directory systems on Flask. in a website, of course, we need to load static assets, such as CSS, Javascript, and other files. Now how to do this in the Flask framework. to do that we can use the ```url_for()``` method. ```url_for()``` is used for matters relating to URLs. in this section we will see its use in our application:

- **Use ```Url_for()``` to access static files**

We will see an example of ```Url_for()``` to access static files like **CSS** or **javascript**. We can see an example of its use in the code below:

**index.html**

```
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Template Jinja2</title>
	<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css')}}">
</head>
<body>
	<h1 class="title">Welcome to my profile</h1>
	{% if not user%}
		<h2 class="sub-title">This is profile page</h2>
	{% else %}
		<h2 class="sub-title">This is page of {{user}}</h2>
	{% endif %}
</body>
</html>
```

- We can access our css file using url for like this ```href="{{ url_for('static', filename='styles.css')}}"```, **static** is the folder name, **static** is a provision  so you *cannot* change it and then we can set the file that we rendered in HTML, that is **styles.css**. We can see in the image below **url_for** will be rendered in HTML like this:

- ![Screenshot_3.png](https://ipfs.busy.org/ipfs/QmUvutC25U55UjScS8iG3whzW4BvseEa44KzwJ7XktuwoV)

**styles.css**

The following is the contents of ***styles.css***. here I will make a simple CSS file just as an example:

```
.title {
	color: red;
}
.sub-title {
	color: green;
}
```

and we can see the results like the following, *please note* there will be a little *cache* when we edit your **CSS** file, you should use ***CTRL + F5 to refresh the page and Cmnd + F5 on Mac OS.***

![ezgif.com-video-to-gif (1).gif](https://ipfs.busy.org/ipfs/QmNWAd445dQF7fMuJWKiEZJFUrtEnfjt87ai8qLi9mpfQk)

We see in the example above, there is a little cache when we load the CSS but this can be overcome with CTRL + F5. now we have connected our static file with the page. we'll see another example of using url_for.
<br>
- **Use ```url_for()``` for navigation links**

We can use **url_for** to navigate the links on our application web, we will see an example like the code below:

**index.html**

```
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Template Jinja2</title>
	<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css')}}">
</head>
<body>
	<a href="{{ url_for('homeFunction')}}">Home</a>
	<a href="{{ url_for('profFunc')}}">Profile</a>
	<a href="{{ url_for('login')}}">Login</a>
	<h1 class="title">Welcome to my profile</h1>
	{% if not user%}
		<h2 class="sub-title">This is profile page</h2>
	{% else %}
		<h2 class="sub-title">This is page of {{user}}</h2>
	{% endif %}
</body>
</html>
```

in the index.html file I made 3 links that point to different URLs, to use ```url_for()``` for navigating links, we can write it like this ```<a href="{{ url_for('homeFunction')}}">Home</a>```. **The parameter** that we pass *is not the routing but the function of the routing*. the following is the routing function, We can see what will be rendered in the HTML element in the following image:

![Screenshot_4.png](https://cdn.steemitimages.com/DQmVWj8ofqaWPxF5uEGrpfJGdkzh3pzfdn2aMFAzwopSDeD/Screenshot_4.png)


**homeFunction()**

```
@app.route('/welcome')
def homeFunction():
		return render_template('index.html')
```
```homeFunction()``` is a function that is run in routing ```/welcome```, so when we go to routing it ***urlfor*** will look for the name of the function that represents the routing. If there is no error then we can see the results as shown below:

![ezgif.com-video-to-gif (2).gif](https://ipfs.busy.org/ipfs/QmbPr1xci3uNyfXVZ4H2UgCC8xBFCB4mgfF2ipRrjk6n2y)

We can see in the picture above we managed to make the navigation link using ulr_for and we also learn how to capture parameter queries on the GET method, this tutorial is your basis for building web applications and gives you an idea of how to start ?, I hope you get lessons that you can use to create your web application using Flask, thank you for following this tutorial.


#### Curriculum

[Web developement with python #1 : Flask initialization and Routing system](https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-1-flask-initialization-and-routing-system-1542726589553)

[Web development with python #2 : Templating jinja2 and Method POST on routing system](https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-2-templating-jinja2-and-method-post-on-routing-system-1542987551736)

#### Proof of work done

https://github.com/milleaduski/python-web-app
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 12 others
properties (23)
authorduski.harahap
permlinkweb-developement-with-python-3-get-method-query-parameter-and-navigate-routing-1543411189705
categoryutopian-io
json_metadata{"app":"steemit/0.1","format":"markdown","image":["https://ipfs.busy.org/ipfs/QmWLex66PLfh8rpqQSMCQPptg7YHvaBzMRbJhviuDYcSfu","https://ipfs.busy.org/ipfs/QmY2i2xB9g1JPL1rrH327qAJbNcwK9zPcRsx716uZeeUV6","https://ipfs.busy.org/ipfs/QmUvutC25U55UjScS8iG3whzW4BvseEa44KzwJ7XktuwoV","https://ipfs.busy.org/ipfs/QmNWAd445dQF7fMuJWKiEZJFUrtEnfjt87ai8qLi9mpfQk","https://cdn.steemitimages.com/DQmVWj8ofqaWPxF5uEGrpfJGdkzh3pzfdn2aMFAzwopSDeD/Screenshot_4.png","https://ipfs.busy.org/ipfs/QmbPr1xci3uNyfXVZ4H2UgCC8xBFCB4mgfF2ipRrjk6n2y"],"tags":["utopian-io","tutorials","python","flask","web"],"links":["https://github.com/python","https://www.python.org/","http://flask.pocoo.org/","http://jinja.pocoo.org/docs/2.10/","https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-2-templating-jinja2-and-method-post-on-routing-system-1542987551736","https://steemit.com/utopian-io/@duski.harahap/web-developement-with-python-1-flask-initialization-and-routing-system-1542726589553","https://github.com/milleaduski/python-web-app"]}
created2018-11-28 13:19:51
last_update2018-11-28 15:03:48
depth0
children4
last_payout2018-12-05 13:19:51
cashout_time1969-12-31 23:59:59
total_payout_value17.339 HBD
curator_payout_value5.713 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9,024
author_reputation60,094,717,098,672
root_title"Web development with python #3 : Get method, Query parameter and Navigate Routing"
beneficiaries
0.
accountutopian.pay
weight500
max_accepted_payout100,000.000 HBD
percent_hbd10,000
post_id76,041,377
net_rshares39,143,690,084,948
author_curate_reward""
vote details (76)
@portugalcoin ·
$7.84
Thank you for your contribution @duski.harahap.

- We really like your tutorial to show screen captures with the results of what you are explaining. This helps a lot the reader.

- We suggest you improve your writing of your tutorial. It is very important to have the text without errors.

- Be careful with the punctuation of the text.

Thanks for your work on developing this tutorial. We are waiting for more tutorials.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/21211333).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , , , , ,
properties (23)
authorportugalcoin
permlinkre-duskiharahap-web-developement-with-python-3-get-method-query-parameter-and-navigate-routing-1543411189705-20181128t222240341z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["duski.harahap"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/21211333","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-11-28 22:22:39
last_update2018-11-28 22:22:39
depth1
children1
last_payout2018-12-05 22:22:39
cashout_time1969-12-31 23:59:59
total_payout_value5.923 HBD
curator_payout_value1.913 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length914
author_reputation599,460,589,822,571
root_title"Web development with python #3 : Get method, Query parameter and Navigate Routing"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id76,064,815
net_rshares12,850,917,259,913
author_curate_reward""
vote details (11)
@utopian-io ·
Thank you for your review, @portugalcoin! Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-duskiharahap-web-developement-with-python-3-get-method-query-parameter-and-navigate-routing-1543411189705-20181128t222240341z-20181201t004231z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
created2018-12-01 00:42:33
last_update2018-12-01 00:42:33
depth2
children0
last_payout2018-12-08 00:42:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length64
author_reputation152,955,367,999,756
root_title"Web development with python #3 : Get method, Query parameter and Navigate Routing"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id76,173,646
net_rshares0
@steem-ua ·
#### Hi @duski.harahap!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
properties (22)
authorsteem-ua
permlinkre-web-developement-with-python-3-get-method-query-parameter-and-navigate-routing-1543411189705-20181129t001340z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
created2018-11-29 00:13:42
last_update2018-11-29 00:13:42
depth1
children0
last_payout2018-12-06 00:13:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length292
author_reputation23,214,230,978,060
root_title"Web development with python #3 : Get method, Query parameter and Navigate Routing"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id76,068,804
net_rshares0
@utopian-io ·
Hey, @duski.harahap!

**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Get higher incentives and support Utopian.io!**
 Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via [SteemPlus](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-web-developement-with-python-3-get-method-query-parameter-and-navigate-routing-1543411189705-20181129t062243z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
created2018-11-29 06:22:45
last_update2018-11-29 06:22:45
depth1
children0
last_payout2018-12-06 06:22:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length595
author_reputation152,955,367,999,756
root_title"Web development with python #3 : Get method, Query parameter and Navigate Routing"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id76,080,988
net_rshares0