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:
JavaScript
x
4
1
<a href="chapters/1">Chapter1</a>
2
<a href="chapters/2">Chapter2</a>
3
<a href="chapters/3">Chapter3</a>
4
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;
JavaScript
1
6
1
class Chapter(models.Model):
2
chapter_id = models.IntegerField(primary_key=True, default=None)
3
chapter_title = models.CharField(max_length=100, default=None)
4
chapter_text = RichTextField(default=None)
5
chapter_footnotes = RichTextField(default=None, blank=True)
6
Advertisement
Answer
You should be able to use a URL dispatcher like you mentioned in your description
JavaScript
1
9
1
// urls.py
2
3
from django.urls import path
4
from . import views
5
6
urlpatterns = [
7
path("chapter/<int:chapter_id>", views.chapter, name="chapter"),
8
]
9
The ID entered there can essentially be used to get the associated chapter in views.py
JavaScript
1
8
1
// views.py
2
3
def chapter(request, chapter_id):
4
5
chapter = Chapter.models.get(id=chapter_id)
6
7
return render(request, "yourtemplate.html", {"chapter": chapter})
8
Then you can render them in your HTML template
JavaScript
1
8
1
// yourtemplate.html
2
3
{{ chapter.chapter_title }}
4
5
{{ chapter.chapter_text }}
6
7
{{ chapter.chapter_footnotes }}
8