Skip to content

Commit afbe8c2

Browse files
committed
Updates per Adrian
Address issue microsoft#1
1 parent 7b7d6cf commit afbe8c2

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

HelloFlask/templates/hello_there.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
<html>
33
<head>
44
<meta charset="utf-8" />
5-
<link rel="stylesheet" type="text/css" href="/static/site.css" />
5+
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='site.css') }}" />
66
<title>{{ title }}title>
77
head>
88
<body>
9-
<span class="message">{{ message }}span> It's {{ date }}.
9+
<span class="message">Hello there, {{ name }}!span> It's {{ date.strftime("%A, %d %B, %Y at %X") }}.
1010
body>
1111
html>

HelloFlask/templates/layout.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="utf-8" />
55
<title>{{ title }}title>
6-
<link rel="stylesheet" type="text/css" href="/static/site.css" />
6+
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='site.css') }}" />
77
head>
88

99
<body>

HelloFlask/views.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,44 @@
1-
from flask import Flask
2-
from flask import render_template
1+
from datetime import datetime
2+
from re import match
3+
4+
from flask import Flask, render_template
5+
36
from HelloFlask import app
47

8+
59
@app.route('/')
610
def home():
7-
return render_template("home.html", title = "Home")
11+
return render_template("home.html", title="Home")
812

913
@app.route('/about')
1014
def about():
11-
return render_template("about.html", title = "About us")
15+
return render_template("about.html", title="About us")
1216

1317
@app.route('/contact')
1418
def contact():
15-
return render_template("contact.html", title = "Contact us")
19+
return render_template("contact.html", title="Contact us")
1620

1721
@app.route('/hello/')
1822
def hello_there(name):
19-
from datetime import datetime
20-
now = datetime.now()
21-
2223
return render_template(
2324
"hello_there.html",
2425
title ='Hello, Flask',
25-
message = "Hello there, " + name + "!",
26-
date = now.strftime("%A, %d %B, %Y at %X")
26+
name = clean_name(name),
27+
date = datetime.now()
2728
)
2829

2930
@app.route('/api/data')
3031
def get_data():
31-
return app.send_static_file('data.json')
32+
return app.send_static_file('data.json')
33+
34+
def clean_name(name):
35+
# Filter the name argument to letters only using regular expressions. URL arguments
36+
# can contain arbitrary text, so we restrict to safe characters only.
37+
match_object = match("[a-zA-Z]+", name)
38+
39+
if match_object:
40+
clean_name = match_object.group(0)
41+
else:
42+
clean_name = "Friend"
43+
44+
return clean_name

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
This sample contains the completed program from the tutorial, [Using Flask in Visual Studio Code](https://code.visualstudio.com/docs/python/tutorial-flask). Intermediate steps are not included.
2+
3+
Contributions to the sample are welcome. When submitting changes, also consider submitting matching changes to the tutorial, the source file for which is [tutorial-flask.md](https://github.com/Microsoft/vscode-docs/blob/master/docs/python/tutorial-flask.md).
4+
15

26
# Contributing
37

0 commit comments

Comments
 (0)