In this post, we will see how we can easily generate controller, methods as well as their views.

A controller is a class that have methods representing URLs in our web application. This means, if we want to have a URL like:

http://mysite.com/about-us

in our web application, then we need to create a controller that contains a method representing the above URL about-us.

Let us check an example for creating controllers and actions.

First you need to create a new Django project by following this link

Let us say we want to create a controller for our website static pages like about, contact us etc.

  1. Create a new file named static.py in the app\views folder:
    app\views\static.py
  2. Now add following code in this file:
    from django.shortcuts import render
    
    def about(request):
        return render(request, 'about.html')
    
    def contact(request):
        return render(request, 'contact.html')

    The methods about and contact are called actions. The request variable contains information that we pass through our URLs.

  3. Next, we will create two files named about.html and contact.html in our templates folder. These files will be rendered (displayed) when someone visits our about and contact pages.
    templates\about.html
    templates\contact.html
  4. Next, we update our app\urls.py file to define our URLs. Make it look similar to this:
    from django.conf.urls import include, url
    from .views import home,static
    
    urlpatterns = [
     url(r'^about-us', static.about),
     url(r'^contact-us', static.contact),
    ]

Now, start your server and navigate to the URLs:

http://localhost:8000/about-us
http://localhost:8000/contact-us
Passing data in URL

We can pass data in our URLs. We can read the passed data in our controller and can act accordingly.
In the following example, we will pass year to our controller. The controller will then check this value and prints whether the year is leap year or not.

  1. Create a new file named calculate.py in app\views folder
     app\views\calculate.py
  2. Next, add following code to this file:
    from django.http import HttpResponse
    
    def leapyear(request, year):
    
        year = int(year)
    
        if year%100==0:
            if year%400==0:
                return HttpResponse("Leap year")
            else:
                return HttpResponse("Not leap year")
        elif year%4==0:
            return HttpResponse("Leap year")
        else:
            return HttpResponse("Not leap year")

    In the above action, we will pass a single value named year. The name year will be defined in our urls.py file.

  3. Update the urls.py file and make it look similar to this:
    from .views import home, calculate
    
    urlpatterns = [
        url(r'^check-leap-year/(?P<year>\d+)/$', calculate.leapyear),
        url(r'^', home.index)
    ]

    When we wish to pass data through URL, we have to specify the data with a name and format. The syntax for this is:

    (?P<name_of_passed_data>format_of_data)/

    So, in the above configuration, year is the name of passed data.
    \d+ specifies that we are going to pass a number.

  4. Now start your server and navigate to the URL:
    http://localhost:8000/check-leap-year/2012

    Change the value 2012 to some other years to confirm the result.

Let us add one more method to calculate.py to find out the number of days in a month in a particular year. Add the following code to calculate.py:

def days_in_month(request, year, month):

    year = int(year)
    month = int(month)

    if month==1 or month==3 or month==5 or month==7 or month==8 or month==10 or month==12:
        return HttpResponse("31 days")
    elif month==4 or month==6 or month==9 or month==11:
        return HttpResponse("30 days")
    elif month==2:
        if year%100==0:
            if year%400==0:
                return HttpResponse("29 days")
            else:
                return HttpResponse("28 days")
        elif year%4==0:
            return HttpResponse("29 days")
        else:
            return HttpResponse("28 days")
    else:
        return HttpResponse("Invalid month")

Add following in your URL configuration:

url(r'^days-in-month/(?P<year>\d+)/(?P<month>\d+)/$', calculate.days_in_month),

Now navigate to the URL:

http://localhost:8000/days-in-month/2012/2