Django书籍详细信息,使用不同的表单和HTML模板。

huangapple go评论64阅读模式
英文:

Django book details with different form and html template

问题

对于Django框架的书籍博客详细信息示例,您有一些问题。您的问题是在Django模板中使用类似于{% url 'book_details' value.slug_title %}的方式列出书籍清单。问题是对于不同的书籍详细信息,您有不同的Django表单和不同的HTML模板。您建立了一段代码片段,它可以正常工作,但您不喜欢这种代码方式,您想提出一种不同的代码方式以获得正确的结果。

以下是您提供的代码:

def blog_details(request, slug):
    data = get_object_or_404(app_descript, slug_title=slug)
    if str(data.title) == 'name 1':
        if request.method == 'POST':
            # 处理name 1 的逻辑
            return render(request, 'details/name1.html', {'data': data})
    elif str(data.title) == 'name 2':
        if request.method == 'POST':
            # 处理name 2 的逻辑
            return render(request, 'details/name2.html', {'data': data})
    elif str(data.title) == 'name 3':
        if request.method == 'POST':
            # 处理name 3 的逻辑
            return render(request, 'details/name3.html', {'data': data})

如果您希望提出一种不同的代码方式以获得正确的结果,您可以进一步说明您期望的代码结构或逻辑。

英文:

i have some question about a DJANGO framework book blog details example .My problem is i have a book list in Django template using something like this {% url 'book_details' value.slug_title %}.My problem is for different book details i have a different Django form and different HTML template .i build a snippet code and work fine ,but i dont like this code way ,i would like to propose a different code way to take correct results.

here the code :

def blog_details(request,slug):

    data = get_object_or_404(app_descript, slug_title=slug)
    if str(data.title) == 'name 1':
        if request.method=='POST':

             .......................

        return render(request, 'details/name1.html', {'data': data})


    elif str(data.title) == 'name 2':
        if request.method=='POST':
			...............................


        return render(request, 'details/name2.html', {'data': data})
		
    elif str(data.title) == 'name 3':
        if request.method=='POST':
			.................


        return render(request, 'details/name3.html', {'data': data})

答案1

得分: 0

这个代码片段怎么样?
类 BookBaseView(View):
    模板名称 = 'details/base.html'
    表单类 = None
    
    def get(self, request, slug):
        book = get_object_or_404(Book, slug_title=slug)
        form = self.表单类(instance=book)
        返回渲染(request, self.模板名称, {'book': book, 'form': form})
        
    def post(self, request, slug):
        book = get_object_or_404(Book, slug_title=slug)
        form = self.表单类(request.POST, instance=book)
        如果 form.is_valid():
            form.save()
            # 处理成功的表单提交
        返回渲染(request, self.模板名称, {'book': book, 'form': form})

类 Book1View(BookBaseView):
    模板名称 = 'details/book1.html'
    表单类 = Book1Form

类 Book2View(BookBaseView):
    模板名称 = 'details/book2.html'
    表单类 = Book2Form

类 Book3View(BookBaseView):
    模板名称 = 'details/book3.html'
    表单类 = Book3Form

URL

urlpatterns = [
    path('book1/<slug:slug>/', Book1View.as_view(), name='book1_details'),
    path('book2/<slug:slug>/', Book2View.as_view(), name='book2_details'),
    path('book3/<slug:slug>/', Book3View.as_view(), name='book3_details'),
    # 应用程序的其他URL模式
]
英文:

How about this snippet?

class BookBaseView(View):
    template_name = &#39;details/base.html&#39;
    form_class = None
    
    def get(self, request, slug):
        book = get_object_or_404(Book, slug_title=slug)
        form = self.form_class(instance=book)
        return render(request, self.template_name, {&#39;book&#39;: book, &#39;form&#39;: form})
        
    def post(self, request, slug):
        book = get_object_or_404(Book, slug_title=slug)
        form = self.form_class(request.POST, instance=book)
        if form.is_valid():
            form.save()
            # handle successful form submission
        return render(request, self.template_name, {&#39;book&#39;: book, &#39;form&#39;: form})

class Book1View(BookBaseView):
    template_name = &#39;details/book1.html&#39;
    form_class = Book1Form

class Book2View(BookBaseView):
    template_name = &#39;details/book2.html&#39;
    form_class = Book2Form

class Book3View(BookBaseView):
    template_name = &#39;details/book3.html&#39;
    form_class = Book3Form

URL

urlpatterns = [
    path(&#39;book1/&lt;slug:slug&gt;/&#39;, Book1View.as_view(), name=&#39;book1_details&#39;),
    path(&#39;book2/&lt;slug:slug&gt;/&#39;, Book2View.as_view(), name=&#39;book2_details&#39;),
    path(&#39;book3/&lt;slug:slug&gt;/&#39;, Book3View.as_view(), name=&#39;book3_details&#39;),
    # other URL patterns for your application
]

huangapple
  • 本文由 发表于 2023年3月7日 16:42:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定