Your Community is getting an upgrade!
Read about our partnership with Higher Logic and how we will build the next generation of the Instructure Community.
Found this content helpful? Log in or sign up to leave a like!
I'm using the Canvas API to create pages and link these created pages dynamically between other pages. If the page has been created before in the course, regardless of whether the page has been deleted, the page slug (ex: the "week-1" in "https://xxx.instructure.com/courses/799908/pages/week-1") adds a hyphen and number to the end of the slug where the number increments depending on how many times the same page name has been created before.
This behavior is not ideal when trying to "predict" the page slug for automatically generated links. Could someone help navigate this issue? Below is similar to the code I'm using to upload pages.
headers = {"Authorization": f"Bearer {access_token}"}
slug = re.sub(r"[^a-z0-9]+", "-", title.lower()).strip("-")
data = {
"wiki_page[title]": title,
"wiki_page[body]": html_content,
"wiki_page[published]": True,
"on_duplicate": "overwrite",
}
data["wiki_page[page_url]"] = slug
resp = requests.post(
f"https://xxx.instructure.com/api/v1/courses/{course_id}/pages",
headers=headers,
data=data,
resp.raise_for_status()
return resp.json()
Solved! Go to Solution.
Hi @vihdutta,
Honestly I wouldn't try to "predict" page URLs at all, as you have seen it's almost impossible to do. What I'd perhaps do instead is create all of your items first so you can get the actual URLs Canvas generated saved somewhere, then edit those items and use the URLs you saved. This may be additional code, but it'll probably make a more stable end product.
-Chris
Hi @vihdutta,
Honestly I wouldn't try to "predict" page URLs at all, as you have seen it's almost impossible to do. What I'd perhaps do instead is create all of your items first so you can get the actual URLs Canvas generated saved somewhere, then edit those items and use the URLs you saved. This may be additional code, but it'll probably make a more stable end product.
-Chris
You don't have to predict the URL, as creating the page returns the page object, which has the URL including the slug.
If you lost track of the URLs and just want to edit the page, you can list all pages, which returns their URLs. In a script, you can look for the largest numbered slug if you want.
To interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign InTo interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign In