In django 2, path is used instead of url.

Essence of the question. How to use the path to make a request on the form domain.test / topic / 1

With the help of url this is done like this: url(r'^topic/(?P<pk>\d+)/$', topic, name='topic')

In views, along with the request, we accept pk and everything works.

How to do this with path? And what you need to pass in the views.

PS Just starting to learn Python and django.

    1 answer 1

    In urls.py

     from django.urls import path from .views import TopicPage urlpatterns = [ path('topic/<slug:title>/<int:id>/', TopicPage.as_view(), name='topic-page'), ] 

    <slug:title> - expected string in slug format
    <int:id> - expected positive number
    You can also specify str , uuid , etc.

    In views.py, you can get the values ​​of title and id from the kwargs dictionary.

     from django.shortcuts import render from django.views.generic import View class TopicPage(View): TEMPLATES = 'someApp/topic-page.html' def get(self, *args, **kwargs): data = {} data['id'] = kwargs.get('id') data['title'] = kwargs.get('title') return render(self.request, self.TEMPLATES, context=data) 

    In general, you can use re_path instead of path , it is similar to the url in Django ver. <2.0

    • Sorry for my ignorance, but how can I render a page from a received URL? What to send to render(request, 'blog/topic.html') ... because, according to the received URL, which is correct, the error "The current path, topic / 1 / is issued, did not match any of these." Alexander Cheremikin September
    • one
      Corrected the answer. TEMPLATES - variable in which the template is specified which will be rendered. The context=data transferred to it (i.e. you can get the data in the template like {{title}} {{id}} ). - AlTheOne