英文:
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 = 'details/base.html'
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, {'book': book, 'form': 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, {'book': book, 'form': form})
class Book1View(BookBaseView):
template_name = 'details/book1.html'
form_class = Book1Form
class Book2View(BookBaseView):
template_name = 'details/book2.html'
form_class = Book2Form
class Book3View(BookBaseView):
template_name = 'details/book3.html'
form_class = 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'),
# other URL patterns for your application
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论