Latest web development tutorials

장고는 첫 번째 프로젝트를 생성

이 장에서 우리는 관리 도구와 방법을 프로젝트, 우리하여 HelloWorld 프로젝트를 명령하는 첫 번째 프로젝트를 만들 장고 장고를 사용하는 방법을 소개합니다.


장고 관리 도구

당신이 장고를 설치 한 후 사용할 수 관리 도구 django-admin.py이 있어야합니다. 우리는 프로젝트를 만드는 데 사용할 수있는 Django-admin.py :

우리는 django-admin.py 명령 설명 볼 수 있습니다 :

[root@solar ~]# 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           Raise 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]
    check
    cleanup
    compilemessages
    createcachetable
……省略部分……

첫 번째 프로젝트 만들기

하여 HelloWorld 프로젝트를 만들려면 Django-admin.py 사용 :

django-admin.py startproject HelloWorld

생성되면, 우리는 프로젝트의 디렉토리 구조를 볼 수 있습니다

[root@solar ~]# cd HelloWorld/
[root@solar HelloWorld]# tree
.
|-- HelloWorld
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
`-- manage.py

디렉토리 설명 :

  • 하여 HelloWorld : 프로젝트의 컨테이너입니다.
  • manage.py : 당신이 장고 프로젝트와 상호 작용하는 다양한 방법을 허용하는 유용한 명령 줄 도구입니다.
  • 하여 HelloWorld / __ init__.py : 디렉토리가 파이썬 패키지인지 파이썬을 알려줍니다 빈 파일입니다.
  • 하여 HelloWorld / settings.py : 장고 프로젝트 / 구성의 설정.
  • 하여 HelloWorld / urls.py : URL 장고 프로젝트 선언, 장고 웹 사이트에서 드라이브 "디렉토리를."
  • 하여 HelloWorld / wsgi.py : 프로젝트를 실행하기 WSGI 호환 입구 웹 서버.

다음으로 우리는을 HelloWorld 디렉토리를 입력 서버를 시작하려면 다음 명령을 입력합니다 :

python manage.py runserver 0.0.0.0:8000

0.0.0.0 다른 컴퓨터가 개발 서버, 포트 번호 8000에 연결할 수 있습니다. 설명하지 않고, 기본 포트 번호 8000.

다음과 같이 브라우저와 서버의 IP와 포트 번호를 입력, 일반 시작하면, 출력 결과는 다음과 같습니다

파이썬

보기 및 구성 URL

디렉토리하여 HelloWorld하여 HelloWorld 디렉토리는 이전에 새로운 view.py 파일에 작성하고, 코드를 입력 :

from django.http import HttpResponse

def hello(request):
	return HttpResponse("Hello world ! ")

다음으로, 결합 URL 및보기 기능을합니다. 열기 urls.py 파일, urls.py 파일에 다음 코드를 복사, 원래의 코드를 삭제합니다 :

from django.conf.urls import *
from HelloWorld.view import hello

urlpatterns = patterns("",
	('^hello/$', hello),
)

다음과 같이 전체 디렉토리 구조는 다음과 같습니다

[root@solar HelloWorld]# tree
.
|-- HelloWorld
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- settings.py
|   |-- settings.pyc
|   |-- urls.py              # url 配置
|   |-- urls.pyc
|   |-- view.py              # 添加的视图文件
|   |-- view.pyc             # 编译后的视图文件
|   |-- wsgi.py
|   `-- wsgi.pyc
`-- manage.py

완료되면, 장고 개발 서버를 시작 브라우저를 열고 브라우저 액세스로 이동 :

파이썬-helloworld를

변경 사항이 프로젝트의 코드는, 서버가 자동으로 코드를 변경을 모니터링 할 경우 자동으로 사용자가 시작한 그래서 만약 서버가 수동으로 다시 시작할 필요가 없습니다, 다시로드 :합니다.