I’m currently working on a website which posts a web novel. I need a system to make user send the chapter id and get the chapter which uses that id maybe like:
<a href="chapters/1">Chapter1</a> <a href="chapters/2">Chapter2</a> <a href="chapters/3">Chapter3</a>
I don’t want to create a specific html page for every novel chapter that we posts and use a system to maybe get the links “/chapter/id” part or send the id when clicked to an element and pull data from the database using the id given. I searched up in the net and couldn’t find anything useful.
Note: The database is like this;
class Chapter(models.Model): chapter_id = models.IntegerField(primary_key=True, default=None) chapter_title = models.CharField(max_length=100, default=None) chapter_text = RichTextField(default=None) chapter_footnotes = RichTextField(default=None, blank=True)
Advertisement
Answer
You should be able to use a URL dispatcher like you mentioned in your description
// urls.py from django.urls import path from . import views urlpatterns = [ path("chapter/<int:chapter_id>", views.chapter, name="chapter"), ]
The ID entered there can essentially be used to get the associated chapter in views.py
// views.py def chapter(request, chapter_id): chapter = Chapter.models.get(id=chapter_id) return render(request, "yourtemplate.html", {"chapter": chapter})
Then you can render them in your HTML template
// yourtemplate.html {{ chapter.chapter_title }} {{ chapter.chapter_text }} {{ chapter.chapter_footnotes }}