前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ubuntu中的django安装配置与操作

ubuntu中的django安装配置与操作

作者头像
闵开慧
发布2018-03-30 13:02:05
9660
发布2018-03-30 13:02:05
举报
文章被收录于专栏:闵开慧闵开慧

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

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

代码语言:javascript
复制
root@ubuntu118:/home/python/work# django-admin.py startproject mysite  

3 启动django服务器

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

代码语言:javascript
复制
root@ubuntu118:/home/python/work/mysite# python manage.py runserver

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

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

代码语言:javascript
复制
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文件如下:

代码语言:javascript
复制
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的内容如下:

代码语言:javascript
复制
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.
     }
 }

然后执行  :    

代码语言:javascript
复制
./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文件:

代码语言:javascript
复制
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/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档