设为首页 收藏本站
查看: 726|回复: 0

[经验分享] How to Install Django on Ubuntu 16.04 LTS-Arnold

[复制链接]

尚未签到

发表于 2019-2-19 09:00:10 | 显示全部楼层 |阅读模式
  In this tutorial, we will install Django 1.10 on an Ubuntu 16.04 LTS server. Django can be installed on a server in many ways, in this tutorial, I will show you 3 different ways to install Django:

  •   Django installation with pip.
  •   Install Django with virtualenv.
  •   Install Django fron it's github repository.
  When the Django installation is done, I will show you the first steps to start a new project with the Django web framework.

  Django is a web application framework written in python that follows the MVC (Model-View-Controller) architecture, it is available for free and>Prerequisites

  •   Ubuntu 16.04 - 64bit.
  •   Root privileges.
Step 1 - Setup python 3 as Default Python version
  We will configure python 3 before we start with the Django installation.
  On my Ubuntu machine, there are two versions of python available, python2.7 as default python version and python3. In this step, we will change the default python version to python 3.
  Check the python version:
  python
  Result:
  python
  Python 2.7.12 (default, Jul  1 2016, 15:12:24)
  [GCC 5.4.0 20160609] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>>
  So the default python is 2.7 at the moment.
  Next, remove default python 2 and change the default to python 3 with the 'update-alternatives' command:
  update-alternatives --remove python /usr/bin/python2
  update-alternatives --install /usr/bin/python python /usr/bin/python3 1
  Now check again the python version:
  python
  Result:
  python
  Python 3.5.2 (default, Jul  5 2016, 12:43:10)
  [GCC 5.4.0 20160609] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  >>>
DSC0000.png

Step 2 - Install Django
  In this step, I will show you 3 ways to install Django. Please follow either chapter 2.1, 2.2 or 2.3 to install Django but not all 3 options at the same time :)
2.1. Install Django with Pip
  Pip is a package management system for python. Python has a central package repository from which we can download the python package. It's called Python Package Index (PyPI).
  In this tutorial, we will use python 3 for Django as recommended by the Django website. Next, we will install pip for python 3 from the Ubuntu repository with this apt command:
  apt-get install python3-pip
  The installation will add a new binary file called 'pip3'. To make it easier to use pip, I will create a symlink for pip3 to pip:
  which pip3
  ln -s /usr/bin/pip3 /usr/bin/pip
  Now check the version :
  pip -V
DSC0001.png

  The pip installation is done. Now we can use the pip command to install python packages. Let's install Django on our server with pip command below:
  pip install django==1.10
  Note:
  We set django==1.10 to get a specific version. If you want a different version, just change the number e.g. to django==1.9 etc.
  If you have an error about the locale settings, run the command below to reconfigure the locale settings:
  export LANGUAGE=en_US.UTF-8
  export.UTF-8
  export LC_ALL=en_US.UTF-8
  locale-gen en_US.UTF-8
  dpkg-reconfigure locales
  When the installation is done, check the Django version with the command below:
  django-admin --version
  Alternatively, we can use the command below:
  python
  import django
  print(django.get_version())
DSC0002.png

  Django 1.10 has been installed on the system with pip. Proceed with chapter 3.
2.2. Install Django with Virtualenv
  Virtualenv is a python environment builder, it is used to create isolated python environments. We can choose the version of python that will be installed in the virtualenv environment. This is very useful for developers, they can run and develop an application with different python versions and different environments on one OS.
  Virtualenv is available on PyPI, we can install it with the pip command:
  pip install virtualenv
  Now we can use the virtualenv command to create a new environment with python3 as default python version. So let's create a new environment "mynewenv" with python3 as the python version and pip3 for the django installation.
  virtualenv --python=python3 mynewenv
  Note :
  --python=python3 is a binary file for python 3.
  mynewenv is the name of the environment.
  The command will create a new directory called 'mynewenv' which contains the directories bin, include and lib.
  The virtualenv has been created, now let's log into the new environment with the command below:
  source mynewenv/bin/activate
  If you do not have the source command, you can run this command instead:
  . mynewenv/bin/activate
  Note: If you want to get out from the virtual environment, use the command 'deactivate'.
  Now check the pip version:
  pip -V
  Pip will be automatically installed in the virtual environment.
  Next, install Django in the thevirtual environment that we've created:
  pip install django==1.10
  When the installation finished, check the Django installation:
  django-admin --version
DSC0003.png

  Django 1.10 has been successfully installed in our virtual environment. Proceed with chapter 3.
2.3. Install Django from Git Repository
  In this chapter, we will install the Django web framework inside the system, not in a virtual environment. I will show you how to install it manually from the Django Git repository. Make sure you have git installed on your server. If you don't have git, install it with the command below:
  apt-get install git -y
  Next, create a new python virtual environment and activate it:
  virtualenv --python=python3 django-git
  source django-git/bin/activate
  Then clone the Django git repository with the command below:
  cd django-git
  git clone git://github.com/django/django django-dev
  Install django with this pip command:
  pip install -e django-dev/
  -e =  Install a package in editable mode or a local package. In this chapter, we install Django from the local code that we've cloned.
  When the installation process is done, let's check the Django version on the server:
  django-admin --version
  1.11.dev20160831163130
  We see the Django 1.11 dev version.
DSC0004.png

  The manual Django installation is finished.
Step 3 - Create you First Project with Django
  In this step, we will install Django inside a virtual environment and then start our first project with Django.
  Install virtualenv on the server and create a new environment named 'firstdjango' :
  pip install virtualenv
  virtualenv --python=python3 firstdjango
  Now go to the 'firstdjango' directory and activate the virtual environment, then install Django with the pip command:
  cd firstdjango/
  source bin/activate
  pip install django==1.10
  Next, create a new project called 'myblog' with the django-admin command:
  django-admin startproject myblog
  It will create a new directory 'myblog' that contains the Django files:
  ll myblog
  -rwxr-xr-x 1 root root  249 Sep 06 09:01 manage.py*
  drwxr-xr-x 2 root root 4096 Sep 06 09:01 myblog/
  Go to the 'myblog' directory and run the 'manage.py' file :
  cd myblog/
  python manage.py runserver
  The runserver option will create an HTTP connection with python on localhost IP and port 8000. If your development environment is on a separate server, as in my example here I'm using an Ubuntu server with I : 192.168.1.9, you can use the server IP so you can access Django from outside of the server.
  python manage.py runserver 192.168.1.9:8000
  Now check from your browser: 192.168.1.9:8000
DSC0005.png

  The Django default page is working and inside the server, you can look at the access log:
[31/Aug/2016 17:04:40] "GET / HTTP/1.1" 200 1767  Next, we will configure the Django admin. Django will automatically generate the database for a superuser. Before we create the superuser, run the command below:
  python manage.py migrate
  migrate: make adds the models (adding fields, deleting etc.) into the database scheme, the default database is sqlite3.
  Now create the admin/superuser:
  python manage.py createsuperuser
  Username (leave blank to use 'root'): admin
  Email address: admin@mydjango.co
  Password:
  Password (again):
  Superuser created successfully.
DSC0006.png

  The Django super user has been added, now you can execute the runserver command, then go to the browser and visit the django admin page:
  python manage.py runserver 192.168.1.9:8000
  Visit Django admin page: 192.168.1.9:8000/admin/. Login with username admin and your password, you will see the admin page:
  Django admin login page.
DSC0007.png

  The Django admin dashboard.
DSC0008.png

  Django has been successfully installed inside a virtual environment and we've created a sample Django project named 'firstdjango'.
Conclusion

  Django is a web framework based on the Python programming language, it is>


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-674166-1-1.html 上篇帖子: Install Chevereto Image Hosting on Ubuntu 16.04-Arnold 下篇帖子: How to Install AbanteCart on Ubuntu 16.04 LTS-Arnold
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表