Django starts from scratch and django starts from scratch
1. Download and install django
Method 1: pip install Django = 1.6.5
Test whether the installation is successful:
~ $ Python
Import django
~ $ (1, 6, 5, 'final', 0)
2. Method 2: Most people will consider installing the latest official release from http://www.djangoproject.com/download.
Django uses the Python standard distutils installation method, which may include the following steps on the Linux platform:
Download the tar installation package, whose file name may be a Django-0.96.tar.gz.
Tar xzvf Django-.tar.gz.
Cd Django
Sudo python setup. py install
2. Hello World
Django uses the MVC design pattern. Django comes with a Web Server for testing. This web server can be used before the site is released, but it cannot be used as a Deployment Server because the server can only process reliable requests once.
Create a folder to store django project: mkdir djcode
Enter djcode: cd djcode
Create a django project: django-admin.py startproject mysite
The generated project directory is:
/Mysite
Manage. py
/Mysite
Setrule. py
Urls. py
_ Init _. py
Wsgi. py
The preceding figure shows the directory structure of the new project.
Now you only need to configure the urls. py file and create the iews. py file to implement the hello world view.
The urls. py file is as follows:
from django.conf.urls import patterns, include, urlfrom mysite.views import hello,current_time,hours_aheadfrom django.contrib import adminadmin.autodiscover()urlpatterns = patterns('', # Examples: # url(r'^$', 'mysite.views.home', name='home'), # url(r'^blog/', include('blog.urls')), (r'^hello/$',hello), # url(r'^admin/', include(admin.site.urls)),)
Then touch views. py
The views. py file is as follows:
from django.http import HttpResponseimport datetimedef hello(request): return HttpResponse("Hello World")
Start web server: python manage. py runserver
Access: http: // 127.0.0.1: 8080/hello/in the browser and you can see hello world