Django Learning Journey-Basic commands explained

Source: Internet
Author: User

Long time no blog, always write Python script, has not written Python web aspects of Things, the Web works are written in PHP, recently in the Doom of learning Django, intends to write a Python work, I have been thinking, Can you write an Android app to do my usual work, or not to be ambitious, first of all Django well, this is written before the note, because in order to catch up with the holiday so the video on the mobile phone to learn that there is not too much to collate documents, in the follow-up will slowly add to the document.


OK, this document simply tells you about Django's basic commands, and a simple little example.


Django Basic Commands Explained

1. Create a new Django project

django-admin.py Startproject Project-name

#一个 Project to a project, project-name the project name to your own, to match the Python variable naming convention (the beginning of the underscore or letter)


2. Create a new app

Python manage.py startapp app-name or django-admin.py startapp app-name

3. Synchronizing the database

Python mangge.py makeigrations

Python manage.py Migrate

# This way you can create a table, and when you add a class to the models.py, you can create a table in the database without creating it manually.


4. Development Server Operations

Python manage.py runserver "POST"

#若是想让局域网内的其他机器来访问的话, you need to do this in the following format

Python manage.py runserver 0.0.0.0:8000 #端口根据实际来定

(Development server is used at development time, general code changes will be automatically restarted, due to performance problems, not used in production environment)


5. Clear the Database

Python manage.py Flush

#此命令会询问yes还是no, select Yes to empty the data, leaving only empty tables.


6. Export data, import data

Python manage.py dumpdata app-name > Appname.json #导出

Python manage.py loaddata Appname.json


7. Create a Super Administrator

Python manage.py Createsuperuser

#用户名和密码必须写, the mailbox can be omitted

#修改密码

Python manage.py changepassword username


Practical applications

1. First create a project

django-admin.py Startproject MySite


2. Go to MySite and create an app

Python manage.py Startapp Leran


3. Add our newly defined app to the Install_apps in settings.py, or we won't be able to find the name of the new project.

4. Defining what to see when you visit

Open the views.py below the project to modify the source code, for example

#coding: Utf-8

From django.http import HttpResponse

def index (Request):

return HttpResponse (U "Welcome to Self-Improvement Academy!")

#引入HttpResponse, it is used to return content to the page, just like print in Python, except that HttpResponse is displayed on the page.

#我们定义一个index函数, the first parameter must be request, which is related to requests from the Web page, which contains the contents of Get or post, user's browser, system, etc.


5. Define Access paths

We want to access the function that we just defined, which is displayed in the page, and we need to define the rules in the urls.py in the project, for example:

When we open the urls.py file, we need to add a URL in the connection, the following is the written rules, for reference only:

From Django.conf.urls import URL

From Django.contrib Import admin

From learn import views as Learn_views # New

Urlpatterns = [

URL (r ' ^$ ', Learn_views.index), # New

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

]


Django Learning Journey-Basic commands explained

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.