17th Day of Python-----The first Django experience

Source: Internet
Author: User

Django is an MTV framework

M:models (Database)

T:templates (placing HTML templates)

V:views (processing user requests)

So what is the legendary MVC framework?

M:models (Database)

V:views (placing HTML templates)

C:controllers (processing user requests)

All the time if someone asks you what MVC or MTV is.

1. Create a Django Project

Django-admin Startproject Mysite_django (your project name), generate a directory containing the following content

Among the Mysite_django, there will be:

settings.py #配置文件

urls.py #路由系统

wsgi.py #WSGI (can be ignored)

2. Create an App

CD Mysite_django

Python manage.py Startapp CMDB (your app name)

3.url.py

The overall Routing system table, need to carefully match the regular and well distinguish the guidance of the URL Oh

1  fromDjango.conf.urlsImportURL2  fromDjango.contribImportAdmin3  fromCmdbImportViews#Be sure to import the processing function of the app you created!4 5Urlpatterns = [6     #URL (r ' ^admin/', admin.site.urls),7URL (r'^login/$', Views.login),#The front is the match URL, followed by the function that handles the URL8URL (r'^login/register/$', Views.register),9URL (r'^admin/$', views.admin),TenURL (r'^index/$', Views.index), One]

4.views.py

It's in every app.

1  fromDjango.shortcutsImportRender#All three of these are written.2  fromDjango.shortcutsImportHttpResponse#All three of these are written.3  fromDjango.shortcutsImportredirect#All three of these are written.4  fromCmdbImportModels#write this if you call the database.5 6 7 defIndex (Request):8     """9 processing a function with the URL indexTen :p Aram Request: This must be written, from the user in the form on the HTML page to get content with One : Return: The simplest is to go directly to a static page A     """ -     returnRender (Request,'index.html')

5. Database related, the default is to use their own db.sqlite3 this library

The models.py within each app is to define the individual tables in your own library

1  fromDjango.dbImportModels2 3 #Create your models here.4 5 6 classUserInfo (models. Model):7     """8 Create a userinfo table9     """TenUsername = models. Charfield (max_length=32) OnePassword = models. Charfield (MAX_LENGTH=32)

Python manage.py makemigrations

Python manage.py Migrate

6.templates

Place where all HTML pages are stored

7.statics

Storage of static files, such as js,css, pictures, etc.

Need to configure the path in setting.py

1 ' /static/ ' 2 staticfiles_dirs = (3     'statics'),4  )

17th Day of Python-----The first Django experience

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.