2

ubuntu中的django安装配置与操作

 1 year ago
source link: https://blog.p2hp.com/archives/9672
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

ubuntu中的django安装配置与操作 | Lenix Blog

本文章向大家介绍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: [email protected]
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/

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK