Python-Django-part 1, pythondjango-part

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

Python-Django-part 1, pythondjango-part
Python manage. py syncdb has been replaced in django1.7; Use python manage. py migrate is used to move the library; Delete and uninstall django in cd/usr/local/lib/python2.7/dist-packages/delete and install the directory related to django: pip install Django = 1.9 specific version info: https://www.djangoproject.com/download/ view django version info:

python -c "import django; print(django.get_version())"
Create a project
django-admin.py startproject mysiteaiapple@Aiapple:~/mysite$ tree.mysite├── manage.py└── mysite    ├── __init__.py    ├── settings.py    ├── urls.py    └── wsgi.py1 directory, 5 files
  • The root directory of the. mysite Project is a container of the project. The name can be changed at will.
  • Manage. py provides multiple ways to interact with projects. Manage. py details
  • The mysite directory under the root directory is where the actual python package of the project is located. His name is the prefix in declaring the python package, for example, mysite. urls;
  • Mysite/_ init _. py is an empty file used to tell python that the directory should be considered as the directory of the python package
  • Mysite/settings. py: configure this django project; Django setting will show you how to set
  • Mysite/urls. py: The django project url declaration. It is a directory table that powers the django site. For details, refer to URL dispatcher.
  • Mysite/wsgi. py is an entry point of the compatibility web server to serve our project, How to deploy
Run the program: Under the root directory mysite
$ python manage.py runserver
Note that port 8000 is enabled by default. At this time, open the browser: http: // 127.0.0.1: 8000/at this time, django is working. Of course, you can also open other ports and other iP addresses. For example:
$ python manage.py runserver 8080$ python manage.py runserver 0.0.0.0:8080

  Create a Polls Application
  $ python manage.py startapp polls
The polls application will automatically generate the basic directory and other application structures, so that we can focus on writing code rather than creating various directory structures;
aiapple@ubuntu:~/mysite/polls$ tree.├── admin.py├── apps.py├── __init__.py├── migrations│   └── __init__.py├── models.py├── tests.py└── views.py1 directory, 7 files 
Write the first view
Polls/views. py # Use the django library to write a viewfrom django. http import HttpResponse def index (request): return HttpResponse ("Hello, world. you're at the polls index. ")

This may be the simplest django view. To call this view, you need to map it to a URL. Therefore, you need to configure the URL. Therefore, you need to create a URL under the polls directory. py file, used to configure the URL;

Polls/urls. py # Call the django urls configuration module from django. conf. urls import url # declare the newly written views module from. import views # establish the relationship between the two urlpatterns = [url (R' ^ $ ', views. index, name = 'index'),]
Next, configure the url of the polls. urls application and inform the project of the url configuration: mysite/urls. py;
mysite/urls.pyfrom django.conf.urls import include, urlfrom django.contrib import adminurlpatterns = [    url(r'^polls/', include('polls.urls')),    url(r'^admin/', admin.site.urls),]
To put it simply, include the urlconf of the app to the urlconf of the Project. Here, only the admin app is used, except the rest. Check whether the newly written view works:
$ python manage.py runserver
The http: // 127.0.0.1: 8000/polls url () function has passed four parameters in the previous process: two required: regex, view; two optional: kwargs, name; url () argrument: regex is commonly known as a regular expression. Django downloads the regular expression list from the first regular expression; compare request url and regular expression until a match is found; Note: regular expression does not look for GET or post parameters, or domain name; example: Request: http://www.example.com/myapp/; URLconf will look for myapp /; or request a http://www.example.com/myapp? Page = 3; URLconf will still find myapp/url () argrument: view. When Django finds the regular expression, it calls a special view function. The HttpRequest object is used as the first parameter, otherwise, the value of the regular expression is used as another parameter.

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.