Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

django - The solution to the problem with the uniqueness of the slug or how to find the slug that I need

Colleagues, good afternoon! There is a model book and a model chapter. Each book has many chapters. Each chapter is tied to a specific book, and if the slug of the chapter is made unique, then when book1 - chapter1, I cannot create book2 - chapter 2, an error is generated. If you make the slug non-unique, then an error is issued that one argument was expected, but 2 was passed.

How can I solve this problem? I want the slug to be a number and django understands that along the path / book1 / 1 / you need to take a slug with number 1, which is tied to book1 specifically, and not to pay attention to the slug with number 1, but tied to book2.

if the slug is unique, then I calmly end up in the right book and the right chapter, but everything collapses when I need to get there as intended.

The path is built like this: / book1 / 1 (chapter) / etc. book2 / 1 / etc

class Book(models.Model):
    some code

class Chapter(models.Model):
   book= models.ForeignKey(Book, verbose_name="title", on_delete=models.CASCADE)
   number = models.PositiveIntegerField(verbose_name="num chapter")
   slug = models.SlugField(unique=True, verbose_name="slug_to", null=True, blank=True)

   def save(self, *args, **kwargs):
        self.slug = self.number
        super().save(*args, **kwargs)

Views.py

class Base(View):
    def get(self, request, *args, **kwargs):
        book = Book.objects.all()
        return render(request, "base.html", context={"book": book})


class BookDetail(DetailView):
    model = Book
    context_object_name = "book"
    template_name = "book_detail.html"
    slug_url_kwarg = "slug"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["chapter"] = Chapter.objects.filter(title=self.object)
        return context


class ChapterRead(DetailView):
    model = Chapter
    context_object_name = "chapter"
    template_name = "chapter_read.html"
    slug_url_kwarg = "int"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["imgs"] = ImgChapter.objects.filter(chapter=self.object)
        return context


urls.py 

from django.contrib import admin
from django.urls import path
from .views import *


urlpatterns = [
    path("", Base.as_view(),name="book_list"),
    path("<str:slug>/", BookDetail.as_view(), name="book_detail"),
    path("<str:slug>/<str:int>/", ChapterRead.as_view(), name="chapter_detail")
]


html

base.html 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>tf</title>
</head>
<body>

{% for i in book%}
    <a href="{{ i.slug }}"> {{i.name}}</a>
{% endfor %}


</body>
</html>

book_detail.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{{ book.name }}
{% for i in chapter %}
<a href="{{ i.slug }}">{{ i.number }}</a>
{% endfor %}
</body>
</html>

chapter_read.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

{{ chapter.number }}

{% for i in imgs %}

    <img src="{{ i.img.url }}">

{% endfor %}

</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...