且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

[原]Python Web框架Django初尝试

更新时间:2022-10-14 12:35:58


之前没接触Python Web框架Django,今天下午抽1小时时间尝试了一下,总结一下,供各位和我一样的Django新手们阅读,如果有问题,请高手们指教。

 

1.安装 Django

1.按照以下步骤下载并安装 Django, 多种下载安装方式:

  1).直接下载安装:

    root@localhost svn co http://code.djangoproject.com/svn/django/trunk/ django_src

    root@localhost cd django_src

    root@localhost python setup.py install

  2).或者通过安装包下载安装:

    下载:https://www.djangoproject.com/download/1.5.1/tarball/

    tar xzvf Django-1.5.1.tar.gz

    cd Django-1.5.1

    sudo python setup.py install

  3).也可以通过git安装:

    git clone https://github.com/django/django.git

  4)通过pip安装:

  pip install Django==1.5.1

如果装了之后有问题,建议直接阅读:https://docs.djangoproject.com/en/stable/intro/install/,这里有详细的安装办法。

 

2. 使用 Django 管理工具

Django 管理工具

在安装 Django 之后,您现在应该已经有了可用的管理工具 django-admin.py。下面是给出了这个管理工具中可以使用的一些命令:

 

[原]Python Web框架Django初尝试
[root@localhost mysite]# django-admin.py
Usage: django-admin.py subcommand [options] [args]

Options:
  -v VERBOSITY, --verbosity=VERBOSITY
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output
  --settings=SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath=PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Print traceback on exception
  --version             show program's version number and exit
  -h, --help            show this help message and exit

Type 'django-admin.py help <subcommand>' for help on a specific subcommand.

Available subcommands:

[django]
    cleanup
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    runfcgi
    runserver
    shell
    sql
    sqlall
    sqlclear
    sqlcustom
    sqldropindexes
    sqlflush
    sqlindexes
    sqlinitialdata
    sqlsequencereset
    startapp
    startproject
    syncdb
    test
    testserver
    validate
[原]Python Web框架Django初尝试

3.查看当前Django版本情况:

python -c "import django; print(django.get_version())"

4.创建工程:

django-admin.py startproject mysite

可见结构如下:

[原]Python Web框架Django初尝试
mysite/

    manage.py

    mysite/

        __init__.py

        settings.py

        urls.py

        Wsgi.py
[原]Python Web框架Django初尝试

5.启动服务:

python manage.py runserver

python manage.py runserver 8080

python manage.py runserver 0.0.0.0:8000

可以看到Web如下:

[原]Python Web框架Django初尝试

6.配置数据库:

python manage.py syncdb

[原]Python Web框架Django初尝试

7.创建应用程序:

python manage.py startapp polls

应用程序结构:

polls/
    __init__.py
    models.py
    tests.py
  Views.py

8.编辑 polls/models.py 文件:

 

[原]Python Web框架Django初尝试
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)
[原]Python Web框架Django初尝试

9.再次编辑settings.py 文件, 修改INSTALLED_APPS 设置包含 'polls'. 如下:

[原]Python Web框架Django初尝试
NSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'polls',
)
[原]Python Web框架Django初尝试

 

10.现在Django知道了所包含的应用 polls.我们可以通过如下命令运行它:

python manage.py sql polls

我们可以看到如下信息提示:

[原]Python Web框架Django初尝试

现在再次运行 syncdb来向我们的数据库中创建模块及相关表:

 python manage.py syncdb