Django starts from scratch and django starts from scratch

Source: Internet
Author: User
Tags django website install django pip install django

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.

  • _ Init _. py: Let Python treat this directory as a file required by an Development Kit (that is, a group of modules. This is an empty file. You do not need to modify it.

  • Manage. py: A command line tool that allows you to interact with this Django project in multiple ways. TypePython manage. py helpTo see what it can do. You should not need to edit this file; generate it in this directory for convenience.

    5

  • Settings. py: Set or configure the Django project. View and understand the available setting types and their default values in this file.

  • Urls. py: Configure the Django project URL. It can be viewed as the directory of your django website. Currently, it is empty.

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


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.