Django path: the next day, the second day of django path

Source: Internet
Author: User

Django path: the next day, the second day of django path

A project has multiple apps. If you want to implement one of the APP functions, what should you do? Next, let's take a look:
Step 1: You have created a project
Step 2: You have created an APP in the project.
Step 3: You have added the APP to the middleware (image 1)


Step 4: Change your urls to this format (Image 2)


Step 5: Go to your APP, find the. py file "views", and write it in it (image 3)


Step 6: start the service
Step 7: Change the address to http: // 127.0.0.1: 8000/index/
Step 8: view the effect (Image 4)


------------------------------------------------------------------------
Question ①: why is there an "index" before a route in urls "?
A: He tells you where the access is located. If the default root directory is not written, that is, http: // 127.0.0.1: 8000,
If it is written, it becomes http: // 127.0.0.1: 8000/index /.
Question 2: I still don't understand it!
A: According to PEP8, the url is followed by the module name of your APP. If your APP name is "fuck, write your urls in this way.
"Path ('fuck/', views. index) ", which becomes http: // 127.0.0.1: 8000/fuck/, so that others can know that this is the function under your" fuck "APP.
Question 3: What if I want to add a path in the middle? For example: http: // 127.0.0.1: 8000/fuck/you?
A: Look down! Forget it. Let's talk about it first. You can use the "include" ingress for Route distribution. Next we will talk about
----------------------------------------------------------------------------
What is the name in the URL?
In a project, we usually see a method in a route:
Path ("XXX/", views. XXX, name = "XXX "),
What is this name? Why write it? Actually:
This is very important, because in projects, you can often find view functions based on them and access them even if the website is changed.
Don't you understand? It's right if you don't understand it. It's right if you don't understand it for new people. Read the code!
Step 1: Go back to the initial project (you can also create a new project and APP if you have enough time)
Step 2: Change the url (image 5)


Step 3: Create an html file (image 7)


Step 4: change the view function (image 6)


Step 5: Write something casually in an html file (picture 8)


Step 6: see Step 7.

Step 7: run the system
My first system is up. The url is http: // 127.0.0.1: 8000/batman/, but the boss says he doesn't like batman or Superman, so I have to change this, what can I do?
Change it, and then I will change it, but you will find that if you write "Dead URL" like this ",
After the URL is changed, the template, view (for example, URL jump), model (for obtaining the access address of the record), and the URL must be changed accordingly, the modification cost is very high. If you do not change it, you will not be able to use it.
What should I do? Fortunately, I have a foresight. I leave a "name" behind each url! Well, you are right. I started to change the version. After the change, I will see the effect: 66666666666666.
How to change it? Next let's look at the example:
Step 1: Add a url (image 9)


Step 2: Modify the view (image 10)


Step 3: Check the effect !!
Then you will find that when you input "batman" again, it will become "supman", but the function has not changed, if you want to change it ............ fuck! There are many things! You cannot add a view function with the corresponding url directly in the view function!
________________________________________________________________________________________
Principle:
After you enter "batman" in the request, it will execute the "index2" function, because this function is specially designed for redirect, then, it will search for your "name = batman" url based on "reverse". After finding the url, it will execute the url code and perform corresponding processing.
Next, you will see the function corresponding to "supman", but your original function code has not changed, and it becomes just your url
Bytes --------------------------------------------------------------------------------------------
Answer question ③:
There is a parameter in urls called "include", which is specially used for Route distribution. The source code will not be mentioned and will be dizzy ............
What should I do? Look down
Step 1: add the url. py file to the APP (image 11)


Step 2: Modify the root url (image 12)


Step 3: Modify the URL. py file under the APP (picture 13)


Step 4: Modify the view function (Figure 14)


Step 5: Check the effect !!! (Http: // 127.0.0.1: 8000/wondwom/look /)

------------------------------------------------------------------

Code Section:

Root urls: from django. contrib import adminfrom django. urls import path, includefrom batman1 import viewsurlpatterns = [path ('admin/', admin. site. urls), path ('Batman/', views. index2), path ('supman/', views. index, name = "batman"),] view function of the APP: from django. shortcuts import render, HttpResponse, HttpResponseRedirectfrom django. urls import reverse # Call module def index (request): return render (request, "index.html") def index2 (request): return HttpResponseRedirect (reverse ("batman "))

  

Root urls: from django. contrib import adminfrom django. urls import path, includeurlpatterns = [path ('admin/', admin. site. urls), path ('wondwom/', include ("batman1.urls"),] urls in the APP: from django. urls import pathfrom. import views # use ". "urlpatterns = [path ('look/', views. look),] view function in APP: from django. shortcuts import render, HttpResponse, HttpResponseRedirectdef look (request): return HttpResponse ("-------------> OK ")

  

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.