Viii. Python Django Database Add query

Source: Internet
Author: User
Tags egrep

Python Django Database Add query

Working with Data

First, create a record

# pwd

/root/csvt03

# Ipython manage.py Shell

In [1]: From blog.models import Employee

# (First method)

In [2]: Employee

OUT[2]: blog.models.Employee


In [3]: emp = Employee ()


In [4]: emp.name = ' Alen '


In [5]: emp.save ()


# (second method)

In [6]: emp = Employee (name= ' Tom ')


In [7]: emp.save ()


# (Third method)

Call Manager Create

In [8]: Employee.objects.create (name= ' Max ')


Query database has created a record


Second, the inquiry record

# Ipython manage.py Shell

In []: emps = Employee.objects.all ()


In []: emps

OUT[14]: [<employee:employee object>, <employee:employee object>, <employee:employee Object>, < Employee:employee Object>]


in [+]: emps[0].id

OUT[16]: 1L


in [+]: emps[0].name

OUT[17]: U ' Alen '


In []: emps[1].name

OUT[18]: U ' Tom '


in [+]: emps[2].name

OUT[19]: U ' Max '


# Cat blog/models.py


From django.db import Models


Class Employee (models. Model):

Name = models. Charfield (MAX_LENGTH=20)

def __unicode__ (self): # Displays the queried data as a string by __unicode__

Return Self.name


# Ipython manage.py Shell

In [1]: From blog.models import Employee


In [2]: emp = Employee.objects.all ()


In [3]: EMP

OUT[3]: [<employee:alen>, <employee:tom>, <employee:max>, <employee:sumer>]




Third, the delivery to the Web page display query results

# Add URL, and add index.html template file

# egrep-v "#|^$" urls.py


From django.conf.urls.defaults import patterns, include, url

Urlpatterns = Patterns ("',

URL (r ' ^index/$ ', ' Blog.views.index '),

)


# egrep-v "#|^$" blog/models.py


From django.db import Models

Class Employee (models. Model):

Name = models. Charfield (MAX_LENGTH=20)

def __unicode__ (self):

Return Self.name


# egrep-v "#|^$" blog/templates/index.html


<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>

<title>loyu Django test</title>

<body>

{% for EMP in emps%}

<div>{{forloop.counter}}:{{emp}}</div>

{% ENDFOR%}

<div> Total Records </div>

</body>



Iv. Start-Up project

# nohup python manage.py runserver & (Background boot with nohup support)


This article is from "Meteor Yu" blog, please be sure to keep this source http://8789878.blog.51cto.com/8779878/1850751

Viii. Python Django Database Add query

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.