"CentOS django 설치"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-<source lang='cli'> +<source lang='console'>))
잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 2명의 중간 판 5개는 보이지 않습니다)
4번째 줄: 4번째 줄:


==사전확인==
==사전확인==
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost ~]# rpm -qa | grep ^python
[root@localhost ~]# rpm -qa | grep ^python
python-libs-2.6.6-36.el6.x86_64
python-libs-2.6.6-36.el6.x86_64
11번째 줄: 11번째 줄:
python-2.6.6-36.el6.x86_64
python-2.6.6-36.el6.x86_64
python-urlgrabber-3.9.1-8.el6.noarch
python-urlgrabber-3.9.1-8.el6.noarch
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost ~]# python -V
[root@localhost ~]# python -V
Python 2.6.6
Python 2.6.6
</source>
</syntaxhighlight>


==EPEL 저장소 추가==
==EPEL 저장소 추가==
21번째 줄: 21번째 줄:


==python-pip 설치==
==python-pip 설치==
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost ~]# yum install python-pip
[root@localhost ~]# yum install python-pip
... (생략)
... (생략)
39번째 줄: 39번째 줄:
Installed size: 2.5 M
Installed size: 2.5 M
Is this ok [y/N]: y
Is this ok [y/N]: y
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
... (생략)
... (생략)
Installed:
Installed:
49번째 줄: 49번째 줄:


Complete!
Complete!
</source>
</syntaxhighlight>


==django 설치==
==django 설치==
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost ~]# pip install django
[root@localhost ~]# pip install django
Downloading/unpacking django
Downloading/unpacking django
67번째 줄: 67번째 줄:
Successfully installed django
Successfully installed django
Cleaning up...
Cleaning up...
</source>
</syntaxhighlight>


==webapp 폴더 초기화==
==webapp 폴더 초기화==
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost ~]# mkdir /opt/webapp
[root@localhost ~]# mkdir /opt/webapp
[root@localhost ~]# cd /opt/webapp
[root@localhost ~]# cd /opt/webapp
80번째 줄: 80번째 줄:
./mysite/mysite/settings.py
./mysite/mysite/settings.py
./mysite/mysite/__init__.py
./mysite/mysite/__init__.py
</source>
</syntaxhighlight>


==서버 실행==
==서버 실행==
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost webapp]# cd mysite/
[root@localhost webapp]# cd mysite/
[root@localhost mysite]# python manage.py runserver &
[root@localhost mysite]# python manage.py runserver &
91번째 줄: 91번째 줄:
Quit the server with CONTROL-C.
Quit the server with CONTROL-C.
^M
^M
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost mysite]# ps -ef | grep runserver | grep -v grep
[root@localhost mysite]# ps -ef | grep runserver | grep -v grep
root    20014 19953  1 15:34 pts/0    00:00:00 python manage.py runserver
root    20014 19953  1 15:34 pts/0    00:00:00 python manage.py runserver
root    20015 20014  2 15:34 pts/0    00:00:00 /usr/bin/python manage.py runserver
root    20015 20014  2 15:34 pts/0    00:00:00 /usr/bin/python manage.py runserver
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost mysite]# netstat -anp | grep python
[root@localhost mysite]# netstat -anp | grep python
tcp        0      0 127.0.0.1:8000              0.0.0.0:*                  LISTEN      20015/python
tcp        0      0 127.0.0.1:8000              0.0.0.0:*                  LISTEN      20015/python
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
[root@zetawiki mysite]# curl "localhost:8000" 2>&1 | grep worked
[root@zetawiki mysite]# curl "localhost:8000" 2>&1 | grep worked
[20/Jun/2014 06:34:43] "GET / HTTP/1.1" 200 1757
[20/Jun/2014 06:34:43] "GET / HTTP/1.1" 200 1757
   <h1>It worked!</h1>
   <h1>It worked!</h1>
</source>
</syntaxhighlight>


==서버 중지/시작==
==서버 중지/시작==
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost mysite]# kill 20014 20015
[root@localhost mysite]# kill 20014 20015
[root@localhost mysite]# ps -ef | grep runserver | grep -v grep
[root@localhost mysite]# ps -ef | grep runserver | grep -v grep
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost mysite]# python manage.py runserver 0.0.0.0:8000 &
[root@localhost mysite]# python manage.py runserver 0.0.0.0:8000 &
... (생략)
... (생략)
119번째 줄: 119번째 줄:
Quit the server with CONTROL-C.
Quit the server with CONTROL-C.
^M
^M
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost mysite]# ps -ef | grep runserver | grep -v grep
[root@localhost mysite]# ps -ef | grep runserver | grep -v grep
root    20031 19953  1 15:35 pts/0    00:00:00 python manage.py runserver 0.0.0.0:8000
root    20031 19953  1 15:35 pts/0    00:00:00 python manage.py runserver 0.0.0.0:8000
root    20032 20031  1 15:35 pts/0    00:00:00 /usr/bin/python manage.py runserver 0.0.0.0:8000
root    20032 20031  1 15:35 pts/0    00:00:00 /usr/bin/python manage.py runserver 0.0.0.0:8000
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost mysite]# netstat -anp | grep python
[root@localhost mysite]# netstat -anp | grep python
tcp        0      0 0.0.0.0:8000                0.0.0.0:*                  LISTEN      20032/python
tcp        0      0 0.0.0.0:8000                0.0.0.0:*                  LISTEN      20032/python
</source>
</syntaxhighlight>
*다른 컴퓨터 웹브라우저에서 http://서버주소:8000 으로 접속해보자.
*다른 컴퓨터 웹브라우저에서 http://서버주소:8000 으로 접속해보자.
<source lang='text'>
<syntaxhighlight lang='text'>
It worked!
It worked!
Congratulations on your first Django-powered page.
Congratulations on your first Django-powered page.
136번째 줄: 136번째 줄:
Of course, you haven't actually done any work yet. Next, start your first app by running python manage.py startapp [appname].
Of course, you haven't actually done any work yet. Next, start your first app by running python manage.py startapp [appname].
You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!
You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!
</source>
</syntaxhighlight>


==Hello, world!==
==Hello, world!==
*views.py 작성
*views.py 작성
<source lang='bash'>
<syntaxhighlight lang='bash'>
vi views.py
vi views.py
</source>
</syntaxhighlight>
<source lang='python'>
<syntaxhighlight lang='python'>
from django.http import HttpResponse
from django.http import HttpResponse
def hello(request):
def hello(request):
         return HttpResponse("Hello, world!")
         return HttpResponse("Hello, world!")
</source>
</syntaxhighlight>


*[[urls.py]] 수정
*[[urls.py]] 수정
<source lang='bash'>
<syntaxhighlight lang='bash'>
vi urls.py
vi urls.py
</source>
</syntaxhighlight>
<source lang='python'>
<syntaxhighlight lang='python'>
from django.conf.urls import patterns, include, url
from django.conf.urls import patterns, include, url


166번째 줄: 166번째 줄:
     url(r'^hello/', 'mysite.views.hello', name='home')
     url(r'^hello/', 'mysite.views.hello', name='home')
)
)
</source>
</syntaxhighlight>


*웹브라우저에서 http://서버주소:8000/hello/ 접속 확인  
*웹브라우저에서 http://서버주소:8000/hello/ 접속 확인  
<source lang='text'>
<syntaxhighlight lang='text'>
Hello, world!
Hello, world!
</source>
</syntaxhighlight>


==안녕, 친구들!==
==안녕, 친구들!==
{{참고|Python 한글 사용}}
{{참고|Python 한글 사용}}
*views.py 수정
*views.py 수정
<source lang='python'>
<syntaxhighlight lang='python'>
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from django.http import HttpResponse
from django.http import HttpResponse
def hello(request):
def hello(request):
         return HttpResponse("안녕, 친구들!")
         return HttpResponse("안녕, 친구들!")
</source>
</syntaxhighlight>


*웹브라우저에서 http://서버주소:8000/hello/ 접속 확인  
*웹브라우저에서 http://서버주소:8000/hello/ 접속 확인  
<source lang='text'>
<syntaxhighlight lang='text'>
안녕, 친구들!
안녕, 친구들!
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[django]]
* [[우분투 Apache+django 설치]]
*[[Python 한글 사용]]
* [[django]]
*[[/opt/webapp/mysite/mysite/urls.py]]
* [[Python 한글 사용]]
*[[리눅스 Flask 설치]]
* [[/opt/webapp/mysite/mysite/urls.py]]
* [[리눅스 Flask 설치]]


==참고 자료==
==참고==
*https://docs.djangoproject.com/en/dev/intro/tutorial01/
*https://docs.djangoproject.com/en/dev/intro/tutorial01/
*http://www.joinc.co.kr/modules/moniwiki/wiki.php/Site/Python/DJango/Tutorial01
*http://www.joinc.co.kr/modules/moniwiki/wiki.php/Site/Python/DJango/Tutorial01

2020년 11월 2일 (월) 00:55 기준 최신판

리눅스 django 설치
django 튜토리얼

1 사전확인[ | ]

[root@localhost ~]# rpm -qa | grep ^python
python-libs-2.6.6-36.el6.x86_64
python-iniparse-0.3.1-2.1.el6.noarch
python-pycurl-7.19.0-8.el6.x86_64
python-2.6.6-36.el6.x86_64
python-urlgrabber-3.9.1-8.el6.noarch
[root@localhost ~]# python -V
Python 2.6.6

2 EPEL 저장소 추가[ | ]

3 python-pip 설치[ | ]

[root@localhost ~]# yum install python-pip
... (생략)
======================================================================================================
 Package                        Arch                Version                   Repository         Size
======================================================================================================
Installing:
 python-pip                     noarch              1.3.1-4.el6               epel              330 k
Installing for dependencies:
 python-setuptools              noarch              0.6.10-3.el6              base              336 k

Transaction Summary
======================================================================================================
Install       2 Package(s)

Total download size: 666 k
Installed size: 2.5 M
Is this ok [y/N]: y
... (생략)
Installed:
  python-pip.noarch 0:1.3.1-4.el6                                                                     

Dependency Installed:
  python-setuptools.noarch 0:0.6.10-3.el6                                                             

Complete!

4 django 설치[ | ]

[root@localhost ~]# pip install django
Downloading/unpacking django
  Downloading Django-1.6.5.tar.gz (6.6MB): 6.6MB downloaded
  Running setup.py egg_info for package django
    warning: no previously-included files matching '__pycache__' found under directory '*'
    warning: no previously-included files matching '*.py[co]' found under directory '*'
Installing collected packages: django
  Running setup.py install for django
    changing mode of build/scripts-2.6/django-admin.py from 644 to 755
    warning: no previously-included files matching '__pycache__' found under directory '*'
    warning: no previously-included files matching '*.py[co]' found under directory '*'
    changing mode of /usr/bin/django-admin.py to 755
Successfully installed django
Cleaning up...

5 webapp 폴더 초기화[ | ]

[root@localhost ~]# mkdir /opt/webapp
[root@localhost ~]# cd /opt/webapp
[root@localhost webapp]# django-admin.py startproject mysite
[root@localhost webapp]# find . -type f
./mysite/manage.py
./mysite/mysite/wsgi.py
./mysite/mysite/urls.py
./mysite/mysite/settings.py
./mysite/mysite/__init__.py

6 서버 실행[ | ]

[root@localhost webapp]# cd mysite/
[root@localhost mysite]# python manage.py runserver &
... (생략)
Django version 1.6.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
^M
[root@localhost mysite]# ps -ef | grep runserver | grep -v grep
root     20014 19953  1 15:34 pts/0    00:00:00 python manage.py runserver
root     20015 20014  2 15:34 pts/0    00:00:00 /usr/bin/python manage.py runserver
[root@localhost mysite]# netstat -anp | grep python
tcp        0      0 127.0.0.1:8000              0.0.0.0:*                   LISTEN      20015/python
[root@zetawiki mysite]# curl "localhost:8000" 2>&1 | grep worked
[20/Jun/2014 06:34:43] "GET / HTTP/1.1" 200 1757
  <h1>It worked!</h1>

7 서버 중지/시작[ | ]

[root@localhost mysite]# kill 20014 20015
[root@localhost mysite]# ps -ef | grep runserver | grep -v grep
[root@localhost mysite]# python manage.py runserver 0.0.0.0:8000 &
... (생략)
Django version 1.6.5, using settings 'mysite.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
^M
[root@localhost mysite]# ps -ef | grep runserver | grep -v grep
root     20031 19953  1 15:35 pts/0    00:00:00 python manage.py runserver 0.0.0.0:8000
root     20032 20031  1 15:35 pts/0    00:00:00 /usr/bin/python manage.py runserver 0.0.0.0:8000
[root@localhost mysite]# netstat -anp | grep python
tcp        0      0 0.0.0.0:8000                0.0.0.0:*                   LISTEN      20032/python
It worked!
Congratulations on your first Django-powered page.

Of course, you haven't actually done any work yet. Next, start your first app by running python manage.py startapp [appname].
You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!

8 Hello, world![ | ]

  • views.py 작성
vi views.py
from django.http import HttpResponse
def hello(request):
        return HttpResponse("Hello, world!")
vi urls.py
from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^hello/', 'mysite.views.hello', name='home')
)
Hello, world!

9 안녕, 친구들![ | ]

  • views.py 수정
# -*- coding: utf-8 -*-
from django.http import HttpResponse
def hello(request):
        return HttpResponse("안녕, 친구들!")
안녕, 친구들!

10 같이 보기[ | ]

11 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}