30分钟快速搭建Web CRUD的管理平台--django神奇魔法
加上你的准备的时间,估计30分钟完全够用了,因为最近在做爬虫管理平台,想着快速开发,没想到python web平台下有这么非常方便的框架,简洁而优雅。将自己的一些坑总结出来,方便给大家的使用。
准备环境:
系统:win7 or ubuntu
django版本:1.8.5
python版本:2.7.6
数据库:自带的SQLLITE3
IDE: sublime text 3
===========================Read ? go===================================
一,选择文件夹,用命令行创建文件夹
sudo django-admin startproject mysite
可以看到mysite文件夹,用命令行切换下mysite文件夹里面情况
1 2 3 4 5 6 7 8 9 | . ├── manage.py └── mysite ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py 1 directory, 5 files |
而manage.py就是我们管理mysite文件夹管理命令文件,用sublime text 3打开该文件夹。
二,在site下建立app ,输入命令:
1 | sudo python manage.py startapp spiderinfo |
这个时候文件夹的情况如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 2015-10-18 17:09:24 ☆ BruceUbuntu in ~ /Desktop/djangoprojects/mysite ○ → tree . ├── manage.py ├── mysite │ ├── __init__.py │ ├── __init__.pyc │ ├── settings.py │ ├── settings.pyc │ ├── urls.py │ └── wsgi.py └── spiderinfo ├── admin.py ├── __init__.py ├── migrations │ └── __init__.py ├── models.py ├── tests.py └── views.py |
三,app情况已经创建好了,django自带了ORM,我们只需要关注代码层的情况就可以了。
这个时候打开spiderinfo文件夹下的models.py,我们来简单的设计两张表
spider爬虫表,spiderconfig爬虫配置表
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | from django.db import models # Create your models here. class SpiderConfig(models.Model): """docstring for SpiderConfig""" cid = models.AutoField(primary_key = True ) configname = models.CharField(max_length = 200 ) createtime = models.DateTimeField() class Spider(models.Model): Sid = models.AutoField(primary_key = True ) SpiderName = models.CharField(max_length = 200 ) Config = models.ForeignKey(SpiderConfig,to_field = 'cid' ) Enable = models.BooleanField(default = True ) |
每个Spider有一个SpiderConfig配置方案,这样就有一个主外键的关系,我们先将写好的关系同步到数据库:
python manage.py migrate
这个时候会自动产生脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Operations to perform: Synchronize unmigrated apps: staticfiles, messages Apply all migrations: admin, contenttypes, auth, sessions Synchronizing apps without migrations: Creating tables... Running deferred SQL... Installing custom SQL... Running migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying sessions.0001_initial... OK |
同步到数据库:
1 | python manage.py syncdb |
这个时候会产生同步的过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | ○ → python manage.py syncdb /usr/local/lib/python2 .7 /dist-packages/django/core/management/commands/syncdb .py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9 warnings.warn( "The syncdb command will be removed in Django 1.9" , RemovedInDjango19Warning) Operations to perform: Synchronize unmigrated apps: staticfiles, messages Apply all migrations: admin, contenttypes, auth, sessions Synchronizing apps without migrations: Creating tables... Running deferred SQL... Installing custom SQL... Running migrations: No migrations to apply. You have installed Django 's auth system, and don' t have any superusers defined. Would you like to create one now? ( yes /no ): yes Username (leave blank to use 'bruce' ): Email address: nice_game@163.com Password: Password (again): Superuser created successfully. |
这个时候会让你输入管理界面的用户名密码,正常输入就可以了。
四,运行server,打开从网页中打开。
python manage.py runserver
打开server,输入网址:
http://127.0.0.1:8000/admin/
我们就可以在后台中看到管理界面了:
五,管理的后台看不到里面的内容,这个时候我们要编辑admin.py的内容,在后台管理界面来显示
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | from django.contrib import admin import spiderinfo.models as app # Register your models here. class SpiderConfigAdmin(admin.ModelAdmin): #要显示的字段列表 list_display = [ 'Cid' , 'Configname' , 'Createtime' ] #要搜索的字段列表 search_fields = [ 'Configname' , 'Createtime' ] list_filter = [ 'Createtime' ] #max show count #list_max_show_all = 100 #Config_id class SpiderAdmin(admin.ModelAdmin): list_display = [ 'SpiderName' , 'Config' , 'Enable' ] #这里特别说明,比如我要根据外键的ConfigName来在Spider实体中的 search_fields = [ 'SpiderName' , 'Config__Configname' ] list_filter = [ 'Enable' , 'SpiderName' ] admin.site.register(app.Spider ,SpiderAdmin) admin.site.register(app.SpiderConfig , SpiderConfigAdmin) |
最的一步,在settings.py里面为我们的应用注册
效果如下:
============================end============================
总结:说30分钟,其实只是建立一个快速搭建的界面,django写的这么优雅和简洁,30分钟怎么可能了解全部呢,一个好的东西是需要花时间好好学习的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?