[Django]-Django โ€“ button url

6๐Ÿ‘

โœ…

Note: path(..) is available from Django-2.0.

The problem here is that friend is not in the URL:

url(r'^add_friend/$', views.friend_add, name="add_friend"),  # no parameter

You can for instance use the primary key (pk) for the friend by specifying a path(..):

path(r'add_friend/<int:friend>/', views.friend_add, name="add_friend"),

In case you work with , you can use url(..) with a regex:

url(r'^add_friend/(?P<friend>[0-9]+)/$', views.friend_add, name="add_friend"),

And now we can use the primary key of the friend in the reverse url:

<input
    type="button"
    class="btn btn-info"
    value="Add Friend"
    onclick="location.href='{% url 'user:add_friend' friend=post.poster.pk %}';"
>

(multi-line to make it easier to read).

2๐Ÿ‘

Your urlpattern shoul also have friend argument like this:

url(r'^add_friend/(?P<friend>[0-9]+)/$', views.friend_add, name="add_friend"),

This will allow you to pass integer ids of friend object to the url like this add_friend/12.

๐Ÿ‘คneverwalkaloner

1๐Ÿ‘

data-ajax-target call particular URL of Django in HTML button

<button type="Submit" value="Submit" class="btn btn-outline-light text-dark"  data-ajax-target="{% url 'collections' %}?collection={{ collection.collectionID }}" }">
<span>{{ collection.choice_label }}</span>
</button>
๐Ÿ‘คJhanvi Patel

Leave a comment