What is PowerShell?

powershellPowerShell

Microsoft PowerShell is a free and open source command shell, scripting language, and remote management framework. PowerShell is intended for network administrators, cloud hosting administration, not professional web programmers, and has replaced the old CMD shell and VBScript as the most popular shell and scripting language for Windows. PowerShell is also available for GNU/Linux and Mac OS on GitHub (https://github.com/PowerShell/PowerShell).

Strictly speaking, PowerShell is part of the Windows Management Framework (WMF), so Microsoft and others often refer to “WMF” instead of to “PowerShell”, but this is a branding and marketing mistake, so manual will almost always just refer to PowerShell and rarely to WMF.
Similar to the CMD shell, you can work in PowerShell interactively, manually typing and executing commands as needed while “in” the shell, or it can run uncompiled scripts written in a scripting language specifically for it. PowerShell scripts end with the “psi” filename extension, not “bat” or “cmd”, and are simple text files.

Unlike the CMD shell, however, PowerShell is built on top of the .NET Framework and is itself a compiled .NET program. PowerShell does not pipe text streams, as virtually every other Windows- or UNIX-based shell does, but complete .NET objects, including all of their properties and methods!

These properties and methods are accessible to your PowerShell scripts, and PowerShell was specifically designed around them. This object-orientation in a shell scripting language is sometimes disorienting to administrators with prior shell language experience.

How to Create views and templates in Django

When you’re first beginning to create websites, having static pages seems only natural. But what if you want to get a little creative and have data change per user or per page?

That’s where using templates can ease the process of developing apps. Templates give you the option to use the same page multiple times throughout the site, with sections of data that can continuously change depending on the request.

This tutorial shows you why creating dynamic pages is worth the time. It also shows you how to easily create and manage them.

In this tutorial, you’ll learn how to:

  • Work with views.
  • Use template variables and tags.
  • Add dynamic data to Django templates.
  • Use template inheritance.

In this module, we’ll work on a website for dog shelters. This project focuses on collecting information for all existing dog shelters and the dogs they’re hoping to place across the country. The fictional hope for this app is that dogs will be able to find suitable homes faster because they’ll have individuals looking to adopt them from around the United States and not just their local area.

DjangoFDJ is a good framework for this project because it provides a route for quickly developing a customer-facing app. It also provides an established database and admin function that employees can easily access for quick updating. We’ve created the initial setup for the project, so we can focus on the concepts for this module.

Requirements

To complete the exercises, you’ll need the following items installed on your computer:

Clone the starter repository

  1. Open a command window or terminal.
  2. Run the following commands to clone the starter repository and change to the project’s directory.
git clone https://github.com/MicrosoftDocs/mslearn-django-views-templates
cd mslearn-django-views-templates/starter

 

l

Open the project in Visual Studio Code

We’ll use Visual Studio Code as our code editor.

In the same command or terminal window, enter the following command to open the folder in Visual Studio Code:

code .

Create the virtual environment

We’ll follow the best practice of working with virtual environments for our project.

  1. In Visual Studio Code, open the terminal window by selecting View > terminal.
  2. In the new terminal window, run the following commands to create and activate a virtual environment:
# Windows
py -3 -m venv venv
.\\venv\\Scripts\\activate

# macOS or Linux
python3 -m venv venv
source ./venv/bin/activate

h

Install Django

The starter project uses a requirements.txt file to contain the list of all necessary packages. We can install them by using pip.

In the same terminal window, run the following command to install the required packages:

pip install -r requirements.txt

;

Start the server

Django can host the application locally. We’ll do this by using the built-in terminal window in Visual Studio Code.

Enter the following command in the same terminal window:

python manage.py runserver

\\

;

In programming, a view is typically a component that displays information to the user. The tasks that a view performs can vary depending on the framework and conventions, including responsibility for loading data.

In Django, a view is typically responsible for:

  • Validating a user’s request.
  • Loading or modifying the appropriate data.
  • Returning an HTML template with the information to the user.

We’re going to begin our exploration by discussing creating views manually and configuring the appropriate URLconfs. A URLconf is a list of paths that indicate the pattern to match, the function to call, and optionally a name. In a later unit, we’ll see how Django can automatically handle some of the code that we wrote by hand.

Create a view

To create a view from scratch in Django, you typically create a function. The function commonly contains the appropriate code to:

  • Perform the task that the user has requested.
  • Return a template with the appropriate data to display to the user.

View functions always take at least one parameter named request, which represents the user’s request. You can provide more parameters as needed if you’re expecting more information from the user in the URL, such as the name or ID of an item. You’ll register those when creating the route, which we talk about later in the unit.

Loading data

You can use the Django ORM to load any data that you need from the registered database.

The project that we’re building has two models, Shelter and Dog. We can load all objects or perform other queries by using the models that we created. To load all shelters, for example, we would use Shelter.objects.all(). We can load an individual shelter by using Shelter.objects.get(pk=1).

404 errors

A 404 error in web applications means “not found.” As a best practice, you should return a 404 whenever a request is made for an object that doesn’t exist.

Django provides shortcuts for trying to load data:

  • get_object_or_404 and get_list_or_404: Loads an object by a primary key, or returns a 404 to the user if an object is not found.
  • get_list_or_404: Performs the same operation as the other shortcut, except that it accepts a filter parameter.

We’ll use get_object_or_404 in our exercise.

Rendering the template

Django’s templating engine will take the HTML template that we build, combine it with any data that we provide, and emit the HTML for the browser. The helper function to perform this task is render.

The render function needs the object that represents the request, which is the request parameter that we highlighted earlier. You also pass in the name of the template, typically an HTML file that will reside in a folder named templates.

To pass data into the template, you provide render with a context dictionary object. The context object contains a set of key/value pairs, where each key becomes a variable in the template.

Example

To create a view to display all shelters, you might use the following code:

def shelter_list(request):
    shelters = Shelter.objects.all()
    context = { 'shelters': shelters }
    return render(request, 'shelter_list.html', context)

[

Register a path

Almost any web framework uses paths to process user requests. Paths convert the portion of the URL after the name of the domain and before the query string (which comes after the question mark) into a function call.

A call to www.contoso.com/shelters might call a function to list all shelters, whereas www.contoso.com/shelters/1 might call a function to display a shelter with an ID of 1. You register paths in Django by creating a URLconf.

Let’s say we have a module called views, which is a Django convention. We can route index traffic where a path isn’t specified (such as www.contoso.com) to a function in views called index, and give it a name called index, by using the following path:

path('', views.index, 'index')


We can also create virtual folders for specific requests. For example, if we wanted to list all shelters if someone requests /shelters, we could use the following command:

path('shelters', views.shelter_list, 'shelter_list')

URL parameters

It’s a common practice to pass parameters to an application as part of the URL, such as an ID or a name. Because these values will change, we don’t want to hard code them into our path. In Django, you can specify a parameter by using a special syntax. In that syntax, you can indicate the type of data you’re expecting, such as an integer, and a name.

For example, to create a path for someone to request a specific shelter by an ID, we would want a parameter of type integer. (The reason is that our primary key is an integer.) We can then provide the name that we want to use for the variable, which will then be passed in as a parameter to the view function. The syntax for identifying this parameter would be <int:pk>. Notice the type declaration, the colon, and then the name of the variable.

The full path might look like this:

path('shelter/<int:pk>', views.shelter_detail, name='shelter_detail')

The associated view function would have the following signature:

def shelter_detail(request, pk):
# code

The pk part of the path signature is passed into shelter_detail as a parameter, just as if we were calling it like a normal Python function.

(Continue)

Build a Basic Web App with Visual Studio Code

In the last post, we created a simple, auto-generated HTML file named index.html. Let’s pick up where we left off and make a web app we can run on a local server.

First test web app : Hello World

For a first test to see the minimal code we can run, we’ll add some content to display. When we left our new index.html file in the last unit, it looked like the following.

html file

Now edit this code to look like the following screen shot (pertinent content is highlighted with red outline).

WEB APP

Notice how the HTML IntelliSense and color coding extensions are working for you throughout. You might explore this more in future modules, but feel free to go ahead and experiment on your own. If you do, be sure to also try the auto-completion features.

Publish the web page locally

Now it’s time to run the HTML locally. Select the GoLive icon in the bottom right status bar.

GO BUTTON

This will tell the Live Server extension we installed earlier to start up and launch your default web browser to serve up the content in the current file. You should see Hello World Test Page in the browser’s title bar and Hello, World! in a large heading font.

Congratulations! You’ve just served up your first web page. Of course, this is content only you can see, because it’s being served by a locally running web server (the Live Server extension). You’ll notice the web address is the IP address reserved for the local machine. If you want this web page to be available online, you’d need to deploy it to a good web host , connected to the internet. This is beyond the scope of this lesson module, but you can learn about it in other modules in this series.

Next: Visual Studio Code for web developers