30-minute quick web crud management Platform--django Magic

Source: Internet
Author: User
Tags sublime text

Add your preparation time, estimated 30 minutes fully enough, because recently in the crawler management platform, thinking of rapid development, did not expect the Python web platform has such a very convenient framework, concise and elegant. Some of their own pits summed up, convenient for everyone's use.

Preparation environment:

System: Win7 or Ubuntu

Django Version: 1.8.5

Python version: 2.7.6

Database: Bring your own SQLLITE3

Ide:sublime Text 3

===========================read? go===================================

One, select folder, create folder with command line

sudo django-admin startproject mysite

You can see the MySite folder and use the command line to switch the contents of the MySite folder.

. ├──manage.py└──mysite    ├──__init__.py    ├──settings.py    ├──urls.py    └──wsgi.py1 directory, 5 files

And manage.py is our Management MySite folder Management command file, open the folder with sublime text 3.

Second, create the app under Site, enter the command:

sudo python manage.py startapp spiderinfo

This time the folder is as follows:

2015-10-18 17:09:24☆  Bruceubuntu in ~/DESKTOP/DJANGOPROJECTS/MYSITE0→TREE.├──MANAGE.PY├──MYSITE│├──__INIT__.P Y│├──__init__.pyc│├──settings.py│├──settings.pyc│├──urls.py│└──wsgi.py└──spiderinfo    ├──admin.py< c2/>├──__init__.py    ├──migrations    │└──__init__.py    ├──models.py    ├──tests.py    └──views.py

Third, the app has been created, and Django comes with an ORM, and we just have to focus on the code layer.

This time open the Spiderinfo folder under the models.py, we can simply design two tables

Spider reptile table, spiderconfig crawler Configuration table

The code is as follows:

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 = $)    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)

  

Each spider has a spiderconfig configuration scheme, so that there is a primary foreign key relationship, we first synchronize the written relationship to the database:

Python manage.py Migrate

This time the script is automatically generated:

Operations to perform:  Synchronize unmigrated apps:staticfiles, Messages  Apply all migrations:admin, ContentTypes, auth, sessionssynchronizing 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

Synchronize to database:

Python manage.py syncdb

This time there will be a synchronization process

0→python manage.py syncdb/usr/local/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py:24: Removedindjango19warning:the syncdb command would be removed in Django 1.9  Warnings.warn ("The SYNCDB command would be R Emoved in Django 1.9 ", removedindjango19warning) Operations to perform:  Synchronize unmigrated Apps:staticfiles, Messages  Apply All migrations:admin, ContentTypes, auth, sessionssynchronizing 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 has any superusers defined. Would to create one now? (yes/no): Yesusername (leave blank to use ' Bruce '): Email address: [email protected]  Password:password (again): Super User created successfully.

This time will let you enter the management interface user name password, normal input can be.

Four, run server, open open from Web page.

Python manage.py runserver

Open the server and enter the URL:

http://127.0.0.1:8000/admin/

We can see the admin interface in the background:

Five, the management of the background can not see the content, this time we want to edit admin.py content, in the background management interface to display

Code:

From Django.contrib import Adminimport 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_idclass spideradmin (admin. Modeladmin):    list_display =[' spidername ', ' Config ', ' Enable ']       #这里特别说明, for example, I want to base the configname of the foreign key in the spider entity    search_fields = [' Spidername ', ' config__configname ']    list_filter = [' Enable ', ' spidername '] Admin.site.register (app. Spider, Spideradmin) admin.site.register (app. Spiderconfig, Spiderconfigadmin)

  

The most step, register for our app in settings.py

The effect is as follows:

============================end============================

Summary: Say 30 minutes, actually just build a fast build interface, Django write so elegant and concise, 30 minutes how can understand all, a good thing is to take time to study.

30-minute quick web crud management Platform--django Magic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.