Skip to content

Instantly share code, notes, and snippets.

@prodeveloper
Created June 7, 2017 12:27
Show Gist options
  • Save prodeveloper/c5071298a5755168944acba6fbb5a335 to your computer and use it in GitHub Desktop.
Save prodeveloper/c5071298a5755168944acba6fbb5a335 to your computer and use it in GitHub Desktop.
Introduction to URL Patterns in Django

##Introduction to URL patterns

Django has 6 major pieces

Url patterns are the first point of contact when a request comes in. The module evaluates the request and determines the appropriate view to handle it.

The view is in charge of handling the request and giving an output. For now our view is empty since we are not showing anything yet.

The view once called renders a template. A template is simply a piece of HTML that can get prefiled with the needed data. Since templates are many, typicaly one for each type of request. They will be stored in our main folder app

Finally, for a request to be complete, we need to fetch data from the database. We have already covered that in creating a model.

With this short introduction, let us delve right into url patterns.

##Step 1

Django uses regular expressions to match a url to specific method in your code. This is very useful because it is the only way that your code has of knowing what the user is doing.

While a bit complex, regular expressions offer an amazing amount of power and flexibility when it comes to dealing with your code.

For now, we will work with purely literal matching. Let us start by working with the case of listing all the students.

Looking at the urls file. What you see is a list of calls to the url function. This function takes on 3 parameters.

  • The regular expression e.g. r'^students/$’
  • The view to call e.g akirachix.views.students
  • Name of the view e.g. students

Django will go through the matches sequentially until it finds a match. If none is found. It then returns a 404

##Step 2

With our URL defined. We now need to make sure that views work.

Open the views file on your akirachix module.

In our urls.py file above, we had just created a reference to a method called students. Let us now write the bare bones version of it as follows.

Notice that all view methods MUST accept a request parameter. Django will pass in any information such as POST or GET information on this variable.

Let us now import the HTTPResponse library so that we can give some response to the users and then output a simple text.

##Step 3

Let us see if we can create a new workflow so that now our homepage is the one that shows the welcome message.

We would go through the steps:

  1. Add the url
  2. Add the view
  3. Return the content

See the images for the three steps below.

##Step 4

Now that we can show the welcome message on homepage, let us change the students url back to a short text saying “Here we will show all students”.

In a later class we will be looking at how to do just that.

##Assignment

So far we have been able to work on homepage to show welcome message. That is

/students - > “Here we will show all students”

Now add code that will show the following for the respective url

/teachers -> “Here we will show all teachers”

##References

@faithmumbi
Copy link

django is awesome in ways of coding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment