The URLs are a way of mapping incoming requests to a view. This mapping is generally done in the urls.py file. The main app(in this case portfolio_blog ) also need to know about this urls.py file. This file is not present by default you need to create this file and add URL configuration.
Let’s see a simple example of what the URL file contains.
from django.urls import path from .views import index_view, AboutPage urlpatterns = [ path('hello/', index_view, name="demo_index_view"), path('about/', AboutPage.as_view(), name="about"), ]
Here you can see two types of view one if function-based(ie. index_view, which was created in the previous chapter) and a class-based view(i.e. AboutPage view). The urls.py file maps URL using the path() variable which is kept in urlpatters named list. The path() take mainly 3 parameters, first is URL to match. This URL path is typed in the browser, the second is the view either class-based or function-based. If it is a class-based view then it should be called using syntax like class_view.as_view(). The third is the name this name variable is used to identify this path in the Django framework. This name variable makes path dynamic, even if the first path changes, if the user has used a name variable to map this URL, the URL will be automatically updated.
Back to portfolio/Blog project.
Let’s set this urls.py file in blog app and contactus app. First create urls.py file in the blog and contactus app in the same path as models.py file.
After adding this file add the following code in each app’s urls.py file
URL settings for blog> urls.py file.
from django.urls import path from .views import index_view, home, AboutPage, BlogList, BlogDetailView urlpatterns = [ path('hello/', index_view, name="demo_index_view"), path('', home, name="home"), path('about/', AboutPage.as_view(), name="about"), path('blog/', BlogList.as_view(), name="blog-list"), path('blog-detail//', BlogDetailView.as_view(), name='blog-detail'), ]
URL settings for contactus> urls.py file.
from django.urls import path from .views import ContactUs urlpatterns = [ path('', ContactUs.as_view(), name='contact-us'), ]
Setting URLs in the main protfolio_blog/urls.py file
You need to know the main app know that there are these apps in each blog add the following code in protfolio_blog >urls.py file.
from django.contrib import admin from django.urls import path, include # add these from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path('admin/', admin.site.urls), path('ckeditor/', include('ckeditor_uploader.urls')), path('', include('blog.urls')), path('contact/', include('contact.urls')), ] urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
After this, you can add templates to each path. If you run the app and open it in the browser it will complain about templates not present.
After adding urs.py file you file structure look like this
In the next chapter, you will see how to set up templates for each view.