Django

gae-django
Articles
Resources
Frameworks
Why django

Basic
Installation
Deployment
Database
Model
Testing

python --version
pip install virtualenvwrapper-win
mkvirtualenv myproject
workon myproject // If we open a new command prompt
pip install django
django-admin --version

It seems that there are some debates on the role of the Django's view in MVC's perspective. However, in general, Django adheres to the MVC framework. The three pieces, data access logic, business logic, and presentation logic are the components called the Model-View-Controller (MVC) pattern of software design. In this pattern, Model refers to the data access layer, View refers to the part of the system that selects what to display and how to display it, and Controller refers to the part of the system that decides which view to use, depending on user input, accessing the model as needed.

Note that in Django the View not only displays but also decides what to display, which is a little bit different from other MVC used in Ruby on Rails and other frameworks. Here is the brief description on MVC:

  1. M: the data-access portion, is handled by Django's database layer.
  2. V: the portion that selects which data to display and how to display it, is handled by views and templates.

#C: the portion that delegates to a view depending on user input, is handled by the framework itself by following our URLconf and calling the appropriate Python function for the given URL.

// HelloWorldApp/models.py:
from django.db import models

class Line(models.Model):                    # model - class    - table
    text = models.CharField(max_length=255)  # field - instance - row

Here is the view we used in HelloWorldApp/views.py:

from django.shortcuts import render_to_response
from models import Line

def foo(request,):
    return render_to_response("helloDJ/home.html",
                               {"lines" : Line.objects.all()})

Actually, template comes in here and intermediates the view regarding "how to display ('lines')". In Django, the View works as the bridge between models and templates. In other words, view takes in HTTP requests, interact with the models and then pass the models onto the templates.

Note that the code also selects which one to display, here "Line.objects.all()"

Here is the url configuration we used in HelloWorld/urls.py:

from django.conf.urls import patterns, include, url
from HelloWorldApp.views import foo

#from django.contrib import admin
#admin.autodiscover()

urlpatterns = patterns('',
    # Examples:

    # url(r'^blog/', include('blog.urls')),

    # url(r'^admin/', include(admin.site.urls)),

    url(r'^$', foo, name='home'),
    url(r'HelloWorldApp/$', foo),
)
from django.shortcuts import render_to_response
from models import Line

def foo(request,):
    return render_to_response("helloDJ/home.html",
                               {"lines" : Line.objects.all()})

How can we import libraries for Eclipse to recognize?

After you set up a project, you might choose a library that your code will rely on. You will need to import the library into your project and configure your project to load that library, but you will also need to instruct Eclipse to use that library. To have Eclipse recognize your imported library:

  1. Import the library to your project.
  2. Edit your Eclipse project’s properties: Right-click the project and select Properties.
  3. Select PyDev - PYTHONPATH in the navigation pane of the dialog.
  4. If you manage your libraries in a child folder of your project, such as lib\, click Add source folder.

Eclipse now adds your specified libraries onto the Python path for resolving imports.

How can we launch our application in debug mode?

Running your app in debug mode in Eclipse allows you to set breakpoints in your code. When those lines are executed, processing pauses temporarily, allowing you to inspect your code and step through the execution. This functionality is extremely helpful during development of your app. To set up a debug configuration:

The first time you launch your app in debug mode, it will create a debug configuration that you can execute later or modify to add parameters for your build.

  1. Right-click your project name in the project explorer and select Debug As > PyDev: Google App Run. The console view should open and display the system output of the development server, which includes the URLs that your app is running.
  2. Verify that your app is running by visiting the URL shown in the console. By default, the app runs from http://localhost:8080. If you used the Hello World starter template, you should see "Hello, world!" printed to the browser.

Debug mode is now running within Eclipse. You also created a Debug configuration that you can run again later if you leave Eclipse or need to stop and restart the debugger.

What does django community call a request handler?

Django's request handlers, called views, are just Python functions.

What is generic view?

One of Django's greatest strengths is its adherence to the DRY principle (don't repeat yourself). The architects of Django recognized several common code patterns and have created various shortcuts to simplify repetitive tasks and common patterns. To this end, Django has the concept of generic views, which not only map results to templates, but also simplify the development process by abstracting a lot of recurring code.

In our case, the generic view direct_to_template is used in order to render the fetched Greeting objects and the form. Also, it takes care of adding useful extra variables like the current request and user to the view. This step can be configured via so-called context processors which let users add, modify, or delete variables that eventually get sent to the template. In general, generic views provide functionality for common tasks like creating/editing or displaying model instances.

What is the alternative? You would have to create custom views for all user output, giving yourself and your app a high probability of repeating yourself. Also note that direct_to_template is the simplest available generic view. You'll save a lot more time (and code) with more advanced generic views like object_list that takes care of paginating a query and displaying current results pages — you really don't want to be repeatedly writing such common functionality by hand.

What is a view and what is a template?

A view is a python function. It is responsible for choosing the right template. A template contains mainly HTML code, mixed with some template constructs which are necessary to implement conditional logic or looping logic if necessary.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License