How to get url from django to javascript with pk

Viewed 248

I'm having trouble to get the url from django to JavaScript. How can I get the url with pk? the error that I receive Not found: "post/(?Pslug:pk\b[0-9A-Fa-f]{8}\b(-\b[0-9A-Fa-f]{4}\b){3}-\b[0-9A-Fa-f]{12}\b)/$'

urls.py

from django.urls import path
from django.urls import reverse
from . import views
import twitter.views

urlpatterns = [
    path("chatbot/(?P<str:pk>\b[0-9A-Fa-f]{8}\b(-\b[0-9A-Fa-f]{4}\b){3}-\b[0-9A-Fa-f] {12}\b)/$'/", views.chatbot, name='chatbot_chatbot'),
    path("post/(?P<slug:pk>\b[0-9A-Fa-f]{8}\b(-\b[0-9A-Fa-f]{4}\b){3}-\b[0-9A-Fa-f]{12}\b)/$'", views.chatbot_process, name='post'),
    path("", views.landing, name='home')

]

Javascript

$.get("/post/(?P<slug:pk>\b[0-9A-Fa-f]{8}\b(-\b[0-9A-Fa-f]{4}\b){3}-\b[0-9A-Fa-f]{12}\b)/$')", {input_text: rawText, user_text: new_value}).done(function (data) {
    const msgText = data;
    appendMessage(BOT_NAME, BOT_IMG, "left", msgText);
  });
1 Answers

You can use this to get your params in your javascript

let url = new URL(window.location.href);
let pk = url.searchParams.get("pk") // Get value of parameter pk
Related