Herramientas de usuario

Herramientas del sitio


fw:django

¡Esta es una revisión vieja del documento!


Django

Basic

First steps

Creating your project

$ django-admin.py startproject mysite

It will create the next file tree:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

Where important files are…

  • manage.py is a script for running django commands.
  • settings.py are the settings for this project.
  • urls.py an index of the content of your project for django engine.

To run the server you'll need to do…

$ python manage.py runserver

You could run python manage.py runserver 8080 to run it on port 8080, or python manage.py runserver 0.0.0.0:8000 to run it on each network interface.
Each time you change your database configuration or some of the django installed apps you'll need to run:

$ python manage.py syncdb

Other django commands are:

  • validate: check errors in the models.
  • sqlcustum appname: output sql statements for the application.
  • sqlclear appname: remvoe tables for the application.
  • sqlindexes appname: create indexes for the app.

Basic knowledge for creating applications

For django an application is like a module, something that have a concrete purpose and performs actions for that. An application can be used in several projects because a project is a group of applications.

$ python manage.py startapp polls

This command will create a polls application structure in your django project folder. It will create next structure folder:

  • admin.py: to edit about an app interface.
  • models.py: model classes.
  • tests.py
  • views.py

To use it you'll need to add it on the settings.py file, on INSTALLED_APP block.
To add to the administration interface a concrete interface for an element in the model we will add something like that in admin.py:

from django.contrib import admin
from polls.models import Poll
admin.site.register(Poll)

Shell

Running the command $ python manage.py shell will open a Python shell to interact with data inside the project.

Configuration

Database

Basic

To config the database engine you'll need to edit settings.py. You should change values like ENGINE, NAME, USER, PASSWORD, HOST… By default an sqlite engine is defined, however you can choose: 'django.db.backends.postgresql_psycopg2', 'django.db.backends.mysql', 'django.db.backends.sqlite3' or 'django.db.backends.oracle'.

In an app the models.py file contains all the classes that represents the model, they should be defined like…

from django.db import models
 
class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
 
class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

Field types are described in https://docs.djangoproject.com/en/1.6/ref/models/fields.

Administration

Views

View code

In views.py file (application folder) you can add your code for your views.
This is the simplest view possible:

from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello, world. You're at the poll index.")

Url Mapping

To map views and url's it's needed to create a file url.py in the application folder. Its content will be something like that:

from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
    url(r'^$', views.index, name='index')
)

Notes

  • To see the sql from an application you'll need to run: $ python manage.py sql appname
fw/django.1389718751.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)