The first Django project development

Source: Internet
Author: User

This article is a practical version of the official website https://docs.djangoproject.com/en/1.7/intro/tutorial01/. Because there are more explanatory elements in the original text and writing in English is not easy to quickly enter the development of Django. That's why this article.

Part 1. Environment Building Test

If you want to reprint please specify the source: *************************** Thank you.

1. Environment

Ubuntu 13.10 # cat/etc/issue for viewing

Python 3.3.2+ # python-v for viewing

Django 1.7.3. # python-c "Import Django; Print (Django.get_version ()) " to view

http://blog.csdn.net/michael_kong_nju/article/details/42878651 documented in this article, how to install and upgrade

If the Python version is different, such as 2.x.x, then there is a grammatical difference.

2. Create a project

We put the project under the/opt/django_programming/.

# cd/opt/django_programming         # django-admin.py Startproject mysite percent is   consistent with the official tutorial, our new project named MySite.

We then completed the creation of a MySite project. We use the tree command to view the structure we just created.

[Email protected]:/opt/django_programming# tree.└──mysite    %%%% The outside of this mysite is the name of our project just now. Here is a container to store some of the content of project.    ├──manage.py    └──mysite        ├──__init__.py        ├──settings.py    %%%% The configuration file for Django project.        ├──urls.py        └──wsgi.py

3. Create a database.

Note that this is a quick start for Django, and we're using the Sqlite3 database that comes with Python. If the actual development of commercial projects is not advocated so open. You can view the following post: *************

# vim  mysite/settings.py           %%%%%% Open the configuration file, find the database array contents, do not modify the database.  DATABASES = {default     ': {                  ' ENGINE ': ' Django.db.backends.sqlite3 ',                 %%%% the Sqlite3 database used by default. If you later need to correlate something like MySQL only needs to be handled here.           ' name ': Os.path.join (Base_dir, ' db.sqlite3 '), percent of the          name         }      }   change Time_zone to:   time_zone = ' Asia /shanghai '. Save after exiting execution: #  python manage.py Migrate         

This completes the database setup.

4. Turn on the server

Here we use Django's own server, only need to use the following command to open: (because this command can not be interrupted, so it is recommended to open a new terminal to run this command)

#  python manage.py runserver    %%%%%%%%% can also be useful for this command:  python manage.py runserver 0.0.0.0:8000    Specify IP and port , this uses the default

Then open the http://127.0.0.1:8000/in the server to see a welcome screen.

Now that a project has been completed, we have completed a project architecture, and we are starting to build a specific application (APP)

5. Create an App

Create polls this voting app here. Execute the following command under the same directory as manage.py:

  #  python manage.py startapp polls
Then we found a new polls directory under the current directory, using the tree command to view the file structure. We then modified the models.py file under polls. In short, models stores our definition of the format of the data schema.

# Vim polls/models.py
Then add it under # Create your models here:

class Question(Models.Model):    Question_text = Models.Charfield(Max_length= $)    pub_date = Models.Datetimefield(' Date published ')class Choice(Models.Model):    question = Models.ForeignKey(Question)    Choice_text = Models.Charfield(Max_length= $)    votes = Models.Integerfield(default=0)

Save exit. The two classes that define Python here are Question and Choice6. Activating the model of the polls

Modifying the Install_apps array in mysite/setting.py

# Vim mysite/setting.py
<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > Add ' Polls ' after the Install_apps array, so that the next time the app is created, the polls will be created together. </span>

Installed_apps = (    ' django.contrib.admin ',    ' Django.contrib.auth ',    ' Django.contrib.contenttypes ' ,    ' Django.contrib.sessions ' ,    ' Django.contrib.messages ' ,    ' Django.contrib.staticfiles ' ,    ' polls ' , )
Save Exit, execute:

# python manage.py makemigrations polls

The following execution results appear:

Migrations for ' polls ':  0001_initial.py:    -Create Model Question    -Create model Choice    -Add field Questi On to Choice
Run the following statement to view the returned SQL statement.

#python manage.py sqlmigrate Polls 0001
Here is the information for my terminal output:

BEGIN;  CREATE TABLE "Polls_choice" ("id" integer NOT null PRIMARY KEY autoincrement, "choice_text" varchar ($) NOT null, "votes" Integer not NULL); CREATE TABLE "Polls_question" ("id" integer NOT null PRIMARY KEY autoincrement, "question_text" varchar ($) NOT NULL, "Pu B_date "datetime not NULL); CREATE TABLE "Polls_choice__new" ("id" integer NOT null PRIMARY KEY autoincrement, "choice_text" varchar ($) NOT NULL, "V  OTEs ' integer NOT NULL, ' question_id ' integer NOT null REFERENCES ' Polls_question ' ("id"); INSERT into "polls_choice__new" ("Choice_text", "question_id", "id", "votes") Select "Choice_text", NULL, "id", "votes" from "Polls_choice";D rop TABLE "Polls_choice"; ALTER TABLE "Polls_choice__new" RENAME to "Polls_choice"; CREATE INDEX "Polls_choice_7aa0f6ee" On "Polls_choice" ("question_id");

Run the following statement to make the data table

Python manage.py Migrate
Output the following information, the data table was created successfully.

[Email protected]:/opt/django_programming/mysite# python manage.py migrateoperations to perform:  Apply all Migrations:sessions, admin, ContentTypes, polls, authrunning migrations:  applying polls.0001_initial ... Ok
This completes the creation of the polls model.

7. Below we try to use SQLite's API in Django.

# python manage.py Shell to  enter interactive interface

Click

>>>from polls.models import Question, Choice percent    imports we just bed home your model class >>>question.objects.all ()  

>>> from django.utils import timezone>>> q = Question (question_text= "What ' s new?", pub_date= Timezone.now ()) percent Add data >>> q.save ()

If there is no error indicating OK, there are a lot of APIs to use, here do not elaborate, you can do reference to the official website of the tutorial:

https://docs.djangoproject.com/en/1.7/intro/tutorial01/#writing-your-first-django-app-part-1

At this point we have completed the construction of the environment, and created the project and the app, below we specifically do the development.

Part2. Instance Development

The first Django project development

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.