2. Write the first Django app

Source: Internet
Author: User

1. Create Project

Django-admin Startproject MySite

After entering the above command, generate the MySite directory and the corresponding file in the current directory:

[[email protected] Django]#Tree.. └──mysite Project container, Django does not care, can be arbitrarily named ├──manage.py a command-line tool that allows you to interact with the Django project in a variety of ways, using python    manage.py Help View how to use    The Python package for the └──mysite project is actually stored in a location that needs to be used for import, such as an ├──__init__.py empty file (import mysite.urls ) .  Tell python this directory is a Python package ├──settings.py the configuration ├──urls.py Project URL declaration for this Django project  , which is the Django routing setting └──wsgi.py An entry point for a WSGI compatible server 2 directories, 5 files

2. Open the Web Server service

[[email protected] mysite] # python manage.py  runserver  0.0.0.0:8000performing system checks ... System Check identified no issues (0, 2016-14:16:551.8.2, using Settings ' mysite.settings ' Starting development Server at http://0.0.0.0:8000/Quit the server with CONTROL-C.

Browser access:

3. Create an App

Now that you've created a project, you're ready to start writing apps (apps). Each app in Django is a package (that is, a directory) that can be generated by commands

Python manage.py Startapp Polls

[Email protected] mysite]# tree/polls/
./polls/
├──admin.py
├──__init__.py
├──migrations
│└──__init__.py
├──models.py
├──tests.py
└──views.py


Writing views polls/views.py

# Cat./polls/views.py From django.http Import httpresponsedef Index (Request):     return HttpResponse ("Hello, World.") You ' re at the polls index. ")

To write the URL configuration polls/urls.py:

# Cat./polls/urls.py  = [    url (r ' ^$ ', Views.index, name=' index '),]


Set the forwarding in the URL configuration file under MySite:

#Cat mysite/urls.py"""mysite URL configurationthe ' urlpatterns ' list routes URLs to views. For more information see:https://docs.djangoproject.com/en/1.8/topics/http/urls/examples:function views 1. ADD an import:from My_app import views 2. Add a URL to Urlpatterns:url (R ' ^$ ', views.home, Name= ' home ") class-based views 1. ADD an import:from other_app.views import Home 2. Add a URL to Urlpatterns:url (R ' ^$ ', Home.as_view (), name= ' Home ') including another URLconf 1. ADD an import:from blog import URLs as Blog_urls 2. Add a URL to Urlpatterns:url (R ' ^blog/', include (Blog_urls))"""From django.conf.urls import include, Urlfrom django.contrib import adminurlpatterns=[url (r ' ^admin/', include (Admin.site.urls)), URL (r ' ^polls / ', include (' Polls.urls ')),]

To open the Web Server service

# python manage.py  runserver  0.0.0.0:8000performing system checks ... System Check identified no issues (0, 2016-14:28:091.8.2, using Settings ' Mysite.sett Ings ' starting development Server at http://0.0.0.0:8000/Quit the server with CONTROL-C.

Above access http://0.0.0.0:8000/polls, will jump to polls.urls parsing route, i.e. URL (r ' ^$ ', Views.index, name=' index ' )

Page Access Testing

2. Write the first Django app

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.