Django framework----Django Model URL configuration view function

Source: Internet
Author: User

Download Django Create a project

On the command line, use the CD command to enter the directory where you want to store the code, and then run the following command:

django-admin.py startproject mysite

 

Let's take a look at what Startproject has created:

mysite/    manage.py #一个实用的命令行工具 allows you to interact with the Django project in a variety of ways.    mysite/        __init__.py        settings.py The     Settings/configuration of the project #Django        urls.py The URL declaration of the project #Django; a Django-driven Web site Directory "        wsgi.py  #一个 The portal of a WSGI-compatible WEB server to run your project
Run a Django Project

Start command:

1 python manage.py runserver

This has started the Django development server, a purely lightweight Web server written by Python. We've included this server in Django so you can develop it quickly, without having to configure a server in a production environment before the product is put into use-for example, Apache,nginx.

Now is a good time to prompt: do not use this server in any similar production environment. It applies only to the development environment. (We're providing a web-framework business, not a Web server.) )

Now that the server is running, please visit http://127.0.0.1:8000/in your Web browser. You will see a delightful, soft light blue "Welcome to Django" page. It works fine!

By default, the development server started by the Runserver command listens only to port 8000 on the local IP. If you want to change the port of the server, pass it as a command line parameter. For example, the following command initiates a server that listens on port 8080:

Python manage.py runserver 8080
Create an App

Each app you write through Djaong is made up of Python packages that are stored in your Python path and follow a certain naming convention. Django provides a utility to automatically generate a basic directory schema for an application, so you can focus on writing code rather than creating a directory.

To create an app command:

Python manage.py Startapp Polls

This creates a polls directory that expands to look like this:

polls/    __init__.py    models.py           tests.py             views.py
URL configuration

To design a URL for an app, you need to create a Python module, often called a urlconf (URL configuration). This module is purely Python code and contains a simple map of the URL pattern (simple regular expression) to the Python function (your view).

Simple URLconf.
From Django.conf.urls import Urlfrom. Import viewsurlpatterns = [    url (R ' ^articles/2003/$ ', views.special_case_2003),    url (r ' ^articles/([0-9]{4})/ $ ', views.year_archive),                             #相当于调用 year_archive (request,year)    url (r ' ^articles/([0-9]{4})/([0-9]{2})/$ ', views.month_archive),    url (r ' ^articles/([0-9]{4})/([0-9]{2})/([0-9]+]/$ ', views.article_detail),           ]
Named groups

The example above uses a simple, unnamed set of regular expressions (through parentheses) to capture the values in the URL and pass it to the view with positional parameters. In a more advanced usage, you can use named regular expression groups to capture the values in the URL and pass it to the view as a keyword argument.

In a python regular expression, the syntax for naming a regular expression group is the name of the (?P<name>pattern) group, which is the name pattern pattern to match.

Here are the overrides for the above urlconf using named groups:

From Django.conf.urls import Urlfrom. Import viewsurlpatterns = [    url (R ' ^articles/2003/$ ', views.special_case_2003),    url (r ' ^articles/(? P<YEAR>[0-9]{4})/$ ', views.year_archive),    url (r ' ^articles/(? P<YEAR>[0-9]{4})/(? P<MONTH>[0-9]{2})/$ ', views.month_archive),    url (r ' ^articles/(? P<YEAR>[0-9]{4})/(? P<MONTH>[0-9]{2})/(? P<DAY>[0-9]{2})/$ ', Views.article_detail),]

This implementation is exactly the same as the previous example, with only one subtle difference: The captured value is passed to the view function as a keyword parameter rather than as a positional parameter. For example:

    the/articles/2005/03/request will call Views.month_archive (Request, Year= ' 2005 ', month= ' 03 ') function,
Instead of views.month_archive (Request, ' 2005 ', ' 03 '). The/ARTICLES/2003/03/03/request will call the function views.article_detail (request, year= ' 2003 ', Month= ' ", day= ' 03 ').

In practical applications, this means that your urlconf will be clearer and less prone to errors in parameter order problems-you can rearrange the order of parameters in your view function definition. Of course, these benefits are at the cost of brevity, and some developers believe that named group syntax is ugly and cumbersome.

View functions

A view function, or briefly called a view, is a simple Python function that accepts a Web request and returns a Web response. The response can be the HTML content of a Web page, a redirect, a 404 error, an XML document, or a picture ... Is anything can. Returns a response regardless of the logic contained in the view itself.

Render method

Combines a given template and a given context dictionary, and returns a rendered HttpResponse object.

1 render(request, template_name,[context])
Redirect Method
1 redirect(to, [permanent=False, ]*args, **kwargs)

Returns httpresponseredirect to the correct URL for the passed in parameter.

Django framework----Django Model URL configuration view function

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.