Developing MVC using Django and Visual Studio 2015 Part 2

Download from github

Developing MVC using Django and Visual Studio 2015 Part 2Developing MVC webapp using Django and VisualStudio2015 [Part 1]

Introduction

Django is a free and open source web framework, written in Python. It follows the MVC pattern, and it is the most popular python web framework. Django provides all basic features that are part of a generic web application: authentication, security and data management.

Here’s the link to the Part 1,  it explains basics of Django ORM and the models implementation.

Routing in Django

Routing mechanism is a common feature of MVC applications, it helps you to define a URL structure and map the URL  to some specific physical file stored on web server.

Routing mechanism is very useful for different reasons:

  1. Remove dependencies between URLs  and file extensions(for example: .html, .aspx, .rb);
  2. Remove dependencies between URls and file locations;
  3. Physical URLs aren’t readable;
  4. The user shouldn’t need to know whether you’re using HTML or ASP or other technologies;
  5. Search engine optimization;

Configuring routing

By default, routing configurations are written in <project_name>\urls.py file.

Django provides two main methods to configure routing: the first is patterns() method which accepts a multiple number of URL patterns, the second method is url() which is used to define an URL pattern that will be passed to patterns() method:

The url() method accepts:

  • regular expression to match(for more information about regular expression: http://www.regxlib.com/);
  • the destination method to call when the regular expression matches;
  • the optional name of the rule;

For example:

How do we add a parameter?

URLs usually contains some parameters about the page which will be displayed. The parameters can be managed using routing configs by following some syntax rules:

  1. The parameter section needs to be contained in parenthesis;
  2.  “?P” indicates we’re specifying a parameter;
  3. The name of the parameter is in angle brackets < >;
  4. The regular expression for the parameter is after the name;

For example, to indicate a numeric id we can use:
url(r'^books/(?P<id>\d+)/$', 'app.views.books_detail', name='books')

Views

A view is a function that takes an HTTP request and returns an HTTP response.  A view always receives, as its first argument, the HttpRequest
created by Django, and it should always return an HttpResponse, unless something went wrong.

For example, let’s take the previous URL config:
url(r'^books/(?P<id>\d+)/$', 'app.views.books_detail', name='books')

The view  app.views.books_detail accepts two parameter: the HttpRequest and the id of entity; At the end of execution, the view returns the HttpResponse:

View decorators

Decorators are a great way to wrap several functions in some common code that can be written once and tested easily, which reduces bugs. Since views are typically just standard Python functions, decorators can be used here as well.

Decorators are contained in django.views.decorators package, here’s the documentation about common views decorators.

Class-based views

You can also define your views as classes, which provides a few key advantages over traditional functions.

  •  Higher degree of configurability;
  •  Easier customization for specialized applications;
  •  Reuse of objects that may be used for other purposes;

The easiest way to get the basics into your class is to subclass django.views.generic.base.View.

Implement class-based views

To implement a class-based view, you need to modify the urls.py file and the views.py file.

The urls.py file defines a new URLs  pattern which maps an url to the instance of the view class:

The views.py file contains the implementation of the class:

GreetingView, subclass django.views.generic.base.View, and it implements two different method, both return an HttpResponse, but they call different views: the get method renders the greeting-get.html and  the post method renders the greeting-post.html.

Conclusion

Routing and Views are the core of Django applications they are a connection between the business model and the user interface.

You can find the project source on Github.  The next post will explain the basics of templating and forms.