[Answered ]-Django, how to create a path: <model_id>/<model_id>/template

1👍

1.The first mistake is passing projects to the ‘project’:projects dictionary. Should be project(‘project’:project).

def function(request, user_id, project_id):
    user = User.objects.get(pk=user_id)
    project = Project.objects.get(pk=project_id)

    return render(request, 'main/cart_page.html',{'project':project, 'users':user})
  1. No trailing slash in path. Instead of a function, you pass a template to views, but you need a view function. It should be like this:

urls.py

urlpatterns = [
    path('<user_id>/<project_id>/test_url/', views.function, name="test-url"),
]
  1. In the template, instead of user.id, there should be users.id (the call goes by the keys of the dictionary that you passed and you have it users).

templates

<p>{{ project }}</p>
<p>{{ users }}</p>

<a href="{% url 'test-url' users.id project.id %}">Refresh the page</a>

address: http://localhost:8000/1/1/test_url/

Leave a comment