ubuntu中的django安装配置与操作

1 安装django,在安装前确保python已经安装,ubuntu中默认有安装的。我们只需要安装django即可,解压django压缩文件,进入解压后的文件夹,运行,python setup.py install即可安装django到python中。

2 创建django项目,首先在任意位置创建文件夹,这里是在python用户下创建work文件夹,接着进入work运行一下命令即可创建django工程。

root@ubuntu118:/home/python/work# django-admin.py startproject mysite  

3 启动django服务器

3.1 使用默认ip和端口启动django服务器,命令如下

root@ubuntu118:/home/python/work/mysite# python manage.py runserver

然后到ubuntu中浏览器输入http://localhost:8080即可查看相应页面。

3.2 设置自己的ip和端口启动django服务器,命令如下

root@ubuntu118:/home/python/work/mysite# python manage.py runserver 0.0.0.0:8000

接着在任意电脑上运行http://192.168.0.118:8000,这里的ip为运行django服务器的ubuntu电脑ip。

 

完成后,就可以创建自己的app了,

首先创建一个app,创建方式为在mysite/mysite下使用 python manage.py startapp blog

这样就创建完了,然后要把该app加到配置文件setting.py中,在INSTALLED_APPS 中增加'mysite.blog'

然后就可以编辑自己的模板了,编辑models.py文件如下:

from django.db import models

# Create your models here.

class BlogPost(models.Model):
     title = models.CharField(max_length= 150)
     body = models.TextField()
     timestamp = models.DateTimeField()

 

编辑完成后就可以设置数据库了,编辑配置文件中关于database的内容如下:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'data.db',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}
然后执行  :     ./manage.py syncdb

root@ubuntu118:/home/python/work/mysite# python manage.py syncdb

Creating tables ...

Creating table auth_permission

Creating table auth_group_permissions

Creating table auth_group

Creating table auth_user_user_permissions

Creating table auth_user_groups

Creating table auth_user

Creating table django_content_type

Creating table django_session

Creating table django_site

Creating table blog_blogpost

 

You just installed Django's auth system, which means you don't have any superusers defined.

Would you like to create one now? (yes/no): y

Please enter either "yes" or "no": yes

Username (leave blank to use 'root'): root

E-mail address: root^H^H^H^H^H^H^H^H^H^H^H

Error: That e-mail address is invalid.

E-mail address: 1261720989@qq.com

Password: 

Password (again): 

Superuser created successfully.

Installing custom SQL ...

Installing indexes ...

Installed 0 object(s) from 0 fixture(s)

root@ubuntu118:/home/python/work/mysite# ll

total 56

 

查看文件夹下多了一个data.db文件:

drwxr-xr-x 4 root   root    4096 Sep 27 11:25 ./

drwxrwxr-x 3 python python  4096 Sep 27 10:39 ../

drwxr-xr-x 2 root   root    4096 Sep 27 11:24 blog/

-rw-r--r-- 1 root   root   32768 Sep 27 11:25 data.db

-rwxr-xr-x 1 root   root     249 Sep 27 10:39 manage.py*

drwxr-xr-x 2 root   root    4096 Sep 27 11:18 mysite/

转载于:https://my.oschina.net/mkh/blog/80653

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值