Getting Started with the Django web framework

Source: Internet
Author: User
Tags install django

I. Django INTRODUCTION

Django is an open-source Web application framework, written in Python, using the MVC framework pattern. The main purpose of Django is to develop a database-driven Web site quickly and easily. It emphasizes code reuse, multiple components can be conveniently in the form of "plug-in" to serve the entire framework, Django has many powerful third-party plug-ins, it is easy to develop their own toolkit.

Two. Create a Django Project

Environment: Django (1.11.6)

Install Django using PIP

[email protected]:~# python3-m pip install Django

New Project (project named Django)

[email protected]:~/root/web# django-admin.py startproject Django

Run the project

[email protected]:~/root/web/django# python3 manage.py runserver

Access Address http://127.0.0.1:8000

Three. Create a Django App

New app (app under Project, app named user)

[email protected]:~/root/web/django# python3 manage.py Startapp user

Modify the Django project configuration django/settings.py, add the user app to the Django project, modify the Installed_apps item to increase the user

Installed_apps = [    'Django.contrib.admin',    'Django.contrib.auth',    'Django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'Django.contrib.staticfiles',    'User',#Add User]

Modifying User/models.py,model is equivalent to encapsulating a database table, the class name user represents the database table name, username and password are field names

 from Import Models class User (models. Model):    #  Django will automatically add an ID as the primary key    username = models. Charfield (max_length=64)    = models. Charfield (max_length=64)

Notifies the Django project that the user application model has changed

[email protected]:~/root/web/django# python3 manage.py makemigrations user

Create a database table structure

[email protected]:~/root/web/django# python3 manage.py Migrate

Modify the Django project configuration django/settings.py, set the static file access directory, add the file last

Static_root = Os.path.join (Base_dir,'Static') Staticfiles_dirs= (    ("CSS", Os.path.join (Static_root,'CSS')),    ("JS", Os.path.join (Static_root,"JS")),    ("Images", Os.path.join (Static_root,"Images")),)

Add views for adding data and presentation data, modifying user/views.py

 fromDjango.shortcutsImportRender fromUser.modelsImportUser fromDjango.httpImportHttpResponsedefAdd (Request): U= Request. get["username"] P= Request. get["Password"] User= User (Username=u, password=p) user.save ()returnHttpResponse ("Success")deflist (Request): List=User.objects.all () context=dict () context["Users"] =ListreturnRender (Request,'list.html', context)

Create a front-end page of data presentation, create a templates/list.html

{% load static%}<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>Title</title>    <Linkhref= "{% static ' css/bootstrap.css '%}"rel= "stylesheet"></Head><Body><Divclass= "Container">    <Divclass= "Row">        <Tableclass= "Table">            <thead>                <TR>                    <th>Id</th>                    <th>User name</th>                    <th>Password</th>                </TR>            </thead>            <tbody>{% for user in users%}<TR>                    <TD>{{User.ID}}</TD>                    <TD>{{User.username}}</TD>                    <TD>{{User.password}}</TD>                </TR>{% endfor%}</tbody>        </Table>    </Div></Div><Scriptsrc= "{% static ' js/jquery-3.2.1.js '%}"></Script><Scriptsrc= "{% static ' js/bootstrap.js '%}"></Script></Body></HTML>

New static directory, add list.html dependent CSS and JS files, directory structure

[Email protected]:~/root/web/django# tree static/static/|-- css|   '-- bootstrap.min.css '-- js    |-- bootstrap.min.js    '--jquery-3.2.  1. min.js

Modify the Django Project routing configuration django/urls.py to increase the routing of the View list and the Add method

 fromDjango.conf.urlsImportURL fromDjango.contribImportAdmin fromUserImportviews as User_viewurlpatterns=[url (r'^admin/', admin.site.urls), url (r'Add', user_view.add), url (r'List', User_view.list),]
Four. Verification Project

Run the deployment

[Email protected]:~/root/web/django# python3 manage.py runserver

Call the Add interface to add a record

Access Address http://127.0.0.1:8000/add?username=Google&password=123456

Access Address Http://127.0.0.1:8000/list

Getting Started with the Django web framework

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.