Python study note 08: Install django and pythondjango

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

Python study note 08: Install django and pythondjango

Install django in linux:

Sudo pip install django

Install django in windows:

Pip install django

Verify that django is installed:

Python-m django -- version

Switch the directory to E: \ SourceCode and create a new project named mysite:

Django-admin startproject mysite

Switch to the mysite directory and run the mysite project:

Python manage. py runserver

In the mysite project, create a polls application:

Python manage. py startapp polls

Start writing the first page and open polls/views. py:

from django.shortcuts import renderfrom django.http import HttpResponse# Create your views here.def index(request):    return HttpResponse("Hello, world. You're at the polls index.")

At this time, django cannot access the view of the polls application. Because the routing information is missing, configure the routing information as follows:
Add and enable polls/urls. py:

from django.conf.urls import urlfrom . import viewsurlpatterns = [    url(r'^$', views.index, name='index'),]

Open mysite/urls. py:

from django.conf.urls import include, urlfrom django.contrib import adminurlpatterns = [    url(r'^polls/', include('polls.urls')),    url(r'^admin/', admin.site.urls),]

There are several ways to add a route, which can be used according to different situations.

Function views ):

1. First import the view module, from. import views

2. Insert a new route in urlpatterns, url (R' ^ $ ', views. index, name = 'index ')

Class-based views ):

1. First import the view module, from other_app.views import Home

2. Insert a new route in urlpatterns, url (R' ^ $ ', Home. as_view (), name = 'home ')

Third (Including another URLconf ):

1. First import the include () function, from django. conf. urls import include, url

2. Insert a new route in urlpatterns, url (r '^ polls/', include ('polls. urls '))

Initialize the database of the mysite project first:

Python manage. py migrate

After the configuration is complete, run the mysite project:

Python manage. py runserver

 

Related Article

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.