Python Learning Notes

Source: Internet
Author: User

Http://python.usyiyi.cn/translate/django_182/intro/tutorial01.html

Create Project: django-admin.py startproject MySite

Create model: Python manage.py startapp MyApp, the model should be created with manage.py in the same directory

Edit myapp/models.py File

From django.db import Models

Class question (models. Model): #继承djagno. Db.models.Model

Question_text = models. Charfield (max_length=200) #设置字段question_text为字符串类型, maximum 200

Pub_date = models. Datetimefield (' date published ') #设置字段pub_date为时间类型

Class Choice (models. Model): #继承djagno. Db.models.Model

Question = models. ForeignKey (question) #设置字段question为外键

Choice_text = models. Charfield (max_length=200) #设置字段choice_text为字符串类型, maximum 200

Votes = models. Integerfield (default=0) #设置字段votes为数值型, default 0

Django applications can be ' hot-swappable ' and can be used in multiple projects, or distributed across applications

Activate model: Edit mysite/setting.py, add ' MyApp ' to Installed_apps

Run Python manage.py makemigrations MyApp

Three steps for model change: 1, modify mysite/models.py

2. Python manage.py makemigrations MyApp #为这些修改创建迁移文件

3. Python manage.py migrate #将修改同步到你的数据库

Model operations: Run the Python manage.py shell

From Myapp.models import Question,choice

Question.objects.all () #获取全部数据

From django.uutils import TimeZone #加载时间模块

Q = question (question_text= ' text ', Pub_date=timezone.now ())

Q.save () #保存数据

Q.id #获取保存的id

Question.objects.all () #获取的是一个 <question:question object>

Modify the acquired model, myapp/models.py each model in Riga

def __str__ (self):

Return Self.question_text

Create Admin User: Python manage.py createsuperuser

Localhost:8000/admin go in to manage the login page of the site

Let MyApp apply at the administration site: Edit myapp/admin.py

From Django.contrib Import admin

From. Models Import question

Admin.site.register (question)

Create a view: Edit myapp/view.py

From Django. Http Import HttpResponse

def index (Request):

Return HttpResponse (' This is my first view of Python! ‘)

Edit myapp/urls.py

From Django.conf.urls import URL

From. Import View

Urlpatterns = [

URL (r ' ^$ ', view.index,name= ' index ')

]

Edit mysite/urls.py

From Django.conf.urls import Inclide,url

From Django.contrib Import Amin

Urlpatterns = [

URL (r ' ^myapp/', include.urls)

URL (r ' ^admin/', include (Admin.site.urls))

]
Browser access to Localhost/myapp, complete

     

     

  

  

Python Learning Notes

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.