15.Django steps to add a function module (and SPRINGMVC analogy)

Source: Internet
Author: User
Tags java web

Here's how to create a new module in Django, which is the simplest example.

Access http://localhost:8000/hello/from the browser and return to a welcome page

I am a Java web-born, here with the Python Django to do, the habitual will and SPRINGMVC to compare

Actually, in my opinion, there's nothing magical about using Django here.

When using SPRINGMVC to make a Hello page, when a request is sent to Tomcat, the first is to request the mapper to process the URL, after the processor adapter, to find the controller to process the request, after processing the view parser rendered view, In fact, aside from the configuration in the framework XML, you need to provide three parts of the code

1. An HTML file for rendering views

2. A controller is used to process the request, and the controller tells the framework that its own results are rendered to that page

3. A @requestmapping used to configure the relationship between URL and controller

In fact, with Django is the three parts.

The HTML file corresponds to the template file in Django (Static and templates directories)

The controller corresponds to the function defined in the views.py file in Django, and this function needs to tell Django to go to that template with render, and the third parameter in render is actually analogous to the data carried by the model in Modelandview.

@RequestMapping corresponding to the configuration in the urls.py file in Django (this might be more like configuring mappings in XML in Sturts, but I'm more familiar with SPRINGMVC, so I'll use SPRINGMVC to compare it). To tell the Django request to come in urls.py, go to that function in the views.

Here's a look at the code:

First step: Create a template file

1. Create a new static and templates directory under the app directory (both directory names and paths are fixed unless configuration parameters are modified)

Static to store CSS and JS code

Templates for storing template code (HTML format)

  

2. Writing template files (according to personal development habits, someone likes to write the logic in the views.py file first)

  

Note: This file <link href= "{% static ' css/bootstrap.min.css '%}" rel= "stylesheet" > Static setting.py static_url = '/ static/' value, so, if you want to change the name of the static folder to change this parameter, if you need to change the path of the static file needs to set the value of the Staticfiles_dirs parameter, in the following configuration, is to put the static file in the project root directory configuration mode:

              

If you want to put the Templates folder in the project root directory, configure the value of the Templates property, such as:

          

3. Writing views.py (\hello_django\hello\views.py) files

  

4. Configuring the urls.py (\hello_django\hello_django\urls.py) file

  

5. Visit: http://localhost:8000/hello/

  

15.Django steps to add a function module (and SPRINGMVC analogy)

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.