In this Django tutorial, we are going to build some URL patterns. This may be a stupid question but was is an URL? A URL is an uniformed resource locator. Think about that for a moment. URL is used to find resources so how does it do that? Well when a client accesses a url the server will take that url and return something in response. So you can think of the url as the clients request. AHHH in the previous tutorial we built a view that took a request which is an URL and then returned a respone which was HTML document that may or maynot include context(dynamic data in most cases). So now we are going to build some request for the user to access our content. We can do this one of two ways. We could just include urls in one file in the CMS directory on the urls.py file but I like to try to keep my apps plugable so we will create a new urls.py file inside our blog app then connect that urls.py file the urls.py in our main project directory. 1. Step one create a new file in your blog app and call it urls.py. 2. Open your new urls.py file. 3. Now we need to import a Django module 'from django.conf.urls import url'. What does this module actually do? Well first this module will look in the containing file for a variable called 'ulrpatterns' this variable should represent a Python list(square brackets[]). Once the variable is located the module will loop through the list variables until it finds the matching url. Once it has located a matching view then Django will follow return the corsponding view function or class based view. This module is actually pretty cool. 4. As I said before we need a variable called 'urlpatterns' so let's go ahead and add that variable and set it to an empty list. Now we have met the requirements for a urls.py file but this does nothing as it stands for us. 5. First we are going to need access to our views so we can map the url to the proper view. We now import the proper view. 'from . import views' 6. Now that we have access to our views we can now map a url request to the proper view which will handle the response. 7. We can now structure the url to provide a request. Inside you empty list enter the url() function. 8. Now the url() function takes two arguments. First one we will be our URL or address. We create the url using regex or otherwise known as regular expressions. I suggest that you visit Python.org and read up on regular expressions if you are not fimilar. The second argument view that we would like to map the url to or connect too. 9. Let's write the regex porition. First we need to inform Pyhton that url string will be in raw format so we add 'r' inside the prethesise. 10. Now we will create our string url. So add two quotes. 11. Now inside the quotes we are going to add a carat ^. In reqular expressions this indicates if the start of the string matches continue. 12. Now that we can decide if the beggin of the string matches we need to decide if the end of url matches our string. To do this we use the regular expression '$' The dollar sign indicates if the end of the string matches. Basically what we are saying here is there is nothing after the backslash. 13. Now we need to map our view. Enter a comma after the string. Now say in views find the view called list_of_post. Our code should look like this. views.list_of_post Now we have officially met the requirements of url function. 14. One last argument I would like to include here is the name='list_of_post' This allows us to name the url so we can use it in our templates. You will see this in action shortly. 15. Guess what we are not done yet. We still need to map the cms urls.py file to our newly created urls.py file. The cms urls.py is the parent so all urls run through the cms urls.py file. 16. Open your cms urls.py file. You will see a very long comment at the top explaining how to use urls.py. There is some great info here if you would like to read it. Let's get started on adding our new urls.py file to the existing one so they can work together. 17. Under url that maps to admin. Add an new url() and inside this we will indicate the raw string again and then use the ^ to indicate if the beggining part of the url up to this point matches continue. Then we will put blog/ this will now make our url http://example.com/blog/. Notice we did not put a $ that is becuase we need to include our other urlconf. 18. Now we need to import the view. Using the include function. We need to first import the include function 'from django.conf.urls import include, url' the we have to include() function as the second argument in url(). 19. Now we will include the new urlconf file. First argument in the include function is 'blog.urls' basically this says in the blog directory find the urls file. 20. Now we are going to include a argument called namespace='blog' this helps with creating links in our templates we will see this in use shortly do not be to concerned at moment about this.
Building URL Patterns For Our Django Project - YouTube | |
58 Likes | 58 Dislikes |
6,222 views views | 14,790 followers |
Education | Upload TimePublished on 12 Oct 2016 |
Không có nhận xét nào:
Đăng nhận xét