Django development and o & M backend (2): create models and update data

Source: Internet
Author: User

In the previous article, I have created a new saadmin app, and now I start to code under this app


1. Modify the database connection of setting. py:

DATABASES = {    ‘default‘: {        ‘ENGINE‘:‘django.db.backends.mysql‘,        ‘NAME‘: ‘QjshAdmin‘,        ‘USER‘: ‘root‘,        ‘PASSWORD‘: ‘lihuipeng‘,        ‘HOST‘: ‘localhost‘,        ‘PORT‘: ‘3306‘,    }}

Create a database:

CREATE DATABASE QjshAdmin DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;


2. Create a table structure model. py:

from django.db import models# Create your models here.class ServerList(models.Model):    kaifu_id = models.IntegerField()    plat = models.CharField(max_length=30)    server_id = models.IntegerField()    server_name = models.CharField(max_length=30)    open_time = models.DateTimeField()    domain = models.CharField(max_length=50)    dx_ip = models.IPAddressField()    lt_ip = models.IPAddressField()    version = models.CharField(max_length=10)    dfid = models.IntegerField()    hefu_range = models.TextField()


3. synchronize data to the database:

python manage.py syncdb

In the meantime, you will be prompted to enter some information about the super administrator. After completion, the database will automatically generate multiple tables:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3A/6F/wKioL1O7shCSqfUrAACyHVnz14Y515.jpg "Title =" 4.png" alt = "wkiol1o7shcsqfuraacyhvnz14y515.jpg"/> auth starts with the verification system and is useless for the time being. Django starts with the Django function table, I only use the saadmin_serverlist table for OK.


4. Update Data

Okay, so I don't need to add two backend servers. I'm a technical eldest brother who generates a URL list for me, and then I can directly read the list and insert the information into the database. After all, I am doing this, the URL content is as follows:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3A/70/wKiom1O7tFeiiniTAAEdIKHLZlg038.jpg "Title =" 5.png" alt = "wkiom1o7tfeiinitaaedikhlzl41038.jpg"/> then I write a view dedicated to updating data:

Views. py:

from django.shortcuts import renderfrom django.http.response import HttpResponsefrom django.views.generic import  View#modelfrom models import *#pythonimport urllib2# Create your views here.class Update_ServerList(View):    url = ‘http://xxx.xxx.xxx.com/qjsh/api/saadmin/‘    def get(self, request):        ServerList.objects.all().delete()        response = urllib2.urlopen(self.url)        for line in response:            serverinfo = line.split(‘,‘)            SL = ServerList(kaifu_id=serverinfo[0],                       plat=serverinfo[1],                       server_id=serverinfo[2],                       server_name=serverinfo[3],                       open_time=serverinfo[4],                       domain=serverinfo[5],                       dx_ip=serverinfo[6],                       lt_ip=serverinfo[7],                       version=serverinfo[8],                       dfid=serverinfo[9],                       hefu_range=serverinfo[10])            SL.save()        return HttpResponse("OK!")

After insertion, a simple OK is returned. I didn't write any front-end HTML ..

Configure URLs. py:

from django.conf.urls import patterns, include, urlfrom django.views.generic import TemplateViewfrom views import *urlpatterns = patterns(‘‘,    url(r‘^test/$‘, TemplateView.as_view(template_name=‘base2.jinja.html‘)),    url(r‘update/$‘, Update_ServerList.as_view(), name="update_serverlist"),)

All right, start the server and access http: // 127.0.0.1: 8000/saadmin/update/. Check the effect first. If there is no accident, it should be OK.


Now the basic data is available. What should we do next ..


This article is from the "O & M notes" blog, please be sure to keep this source http://lihuipeng.blog.51cto.com/3064864/1435983

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.