How can I pass a dict to urlpatterns in Django

Viewed 31

I am trying to send data from my web page to urlpattern in Django and then to the view. I know that I can include a dict in my urlpattern path that will be sent to my view:

path("edit_page", views.edit_article, {'article':'fake_text'}, name="edit_page")

I want to send the dict directly from my webpage. I have been trying:

urls.py

path("edit_page", views.edit_article, name="edit_page")

html

<a href='{% url 'edit_page' {'article': 'article_test'} %}'>Edit</a>

Where am I going wrong?

1 Answers

You typically encode the data in the URL, for example with:

path('edit_page/<str:article>/', views.edit_article, name='edit_page'),

then you define the view as:

def edit_article(request, article):
    # …

then the view will be called with the article part of the URL path.

In the template you can then "calculate" the URL with:

<a href="{% url 'edit_page' article='article_test' %}">Edit</a>
Related