Mohammad AlMarzouq
Improved URLs
/blog/posts
/blog/post/10
/blog/post/234
/profile/khaled
/profile/ahmed
urlpatterns = [
path('test/', test_view),
path('blog/post', blog_posts),
]
Remember to import the view functions!
path('profile/<str:name>/', user_profile),
path('blog/post/<slug:slug>/', view_post_by_slug),
path('blog/post/<int:id>/', view_post_by_id),
path('blog/post/<int:year>/<int:month>/', view_posts_in_month),
The syntax:
<data_type:variable_name>
Just place it in the part that you would like to vary in the URL path
path('profile/<str:name>/', user_profile),
Must be matched with the following view function:
def user_profile(request, name):
The following path:
path('blog/post/<int:year>/<int:month>/', view_posts_in_month),
Must be matched with the following view function:
def view_posts_in_month(request, year, month):
path('admin/', admin.site.urls),
admin.site.urls
?from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
]
This is telling Django that our paths can be found in the blog app in the file named urls.py
path('blog/', include('blog.urls')),
from . import views
from django.urls import path
urlpatterns = [
path('', views.post_list),
path('<slug:slug>/', views.display_post),
]
Notice we do not execute the function using ()
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
]
from . import views
from django.urls import path
urlpatterns = [
path('greet/<str:name>', views.test_view),
]
def test_view(request, name):
data = {}
data["name"] = name
return render(request, 'test.html', context=data)
<html>
<body>
<h1>Hello {{ name }}!</h1>
</html>
The correct path is:
/blog/greet/your-name-here
As you change the name part in the path, so will the message in the page