【问题标题】:How to see posts using functions based view in django?如何在 django 中使用基于函数的视图查看帖子?
【发布时间】:2021-07-12 19:07:46
【问题描述】:

在我当前的 django 项目中,我必须创建一个函数来呈现主页,以便我可以在其中创建一个表单,该表单位于 base.html 中,以便可以在网站的每个页面帖子中呈现,当前代码是这样的:

def contactView(request):
    if request.method == 'GET':
        form = ContactForm()
    else:
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            email = form.cleaned_data['email']
            message = form.cleaned_data['message']
            try:
                send_mail(subject, 
                          message, 
                          email, 
                          ['example@gmail.com'],
                          fail_silently=False)
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('home')
    return render(request, "index.html", {'form': form})

哪个有效,但是每个帖子的视图都是基于类的视图,可以完美地显示表单:

class PostDetail(generic.DetailView):
    model = Post
    extra_context = {'form': ContactForm()}
    template_name = 'post_detail.html'

唯一的问题是主页中的表单可以正常工作,因为有代码,我不知道如何调用类的相同代码(否则我什至会为主页使用一个类)

更详细的 urls.py 是:

from django.urls import path
from . import views

urlpatterns = [ 
    path('', views.contactView, name = 'home'),
    path('<slug:slug>/', views.PostDetail.as_view(), name = 'post_detail'),
]

基本上,在 post 表单中,如果我点击 fourth post,我会在新呈现页面的路径中添加 slug,我会被重定向到 HTTP://127.0.0.1/fourth-post。

我的问题是每个帖子中的表单都不起作用,而只能在主页中使用,这是因为显然没有代码。所以我可以做两件不同的事情:

  • 重新制作 post 类,使其基本上是 home 功能的副本
  • 使用相同的代码创建一个方法,使代码能够正常工作

我想做第二个,但作为 django 的新手,我不知道该怎么做,任何帮助或见解都会很棒

【问题讨论】:

  • 您想在帖子详细信息页面或主页上显示您的联系表格吗?
  • 两者都有问题

标签: python django forms backend


【解决方案1】:

您可以在 contactView 中使用基于函数的视图进行此操作

def contactView(request):
    if request.method == 'GET':
        post = Post.objects.all()
        form = ContactForm()
    else:
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            email = form.cleaned_data['email']
            message = form.cleaned_data['message']
            try:
                send_mail(subject, 
                          message, 
                          email, 
                          ['example@gmail.com'],
                          fail_silently=False)
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('home')
    return render(request, "index.html", {'form': form,'post':post})

对于帖子的详细信息,您可以这样做

def post_detail(request,slug):
    post = Post.objects.filter(slug=slug)
    form = ContactForm()
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            email = form.cleaned_data['email']
            message = form.cleaned_data['message']
            try:
                send_mail(subject, 
                message, 
                email, 
                ['example@gmail.com'],
                fail_silently=False)
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('home')
    return render(request, "post_detail.html", {'form': form,'post':post})

【讨论】:

  • 谢谢,唯一的事情是post = Post.objects.filter(slug=slug) 应该是post = Post.objects.filter(slug=slug).first() 来获取实际的帖子对象,除了它似乎工作并且没有问题。我无法真正测试它,因为我现在没有发送电子邮件的服务,并且电子邮件没有出现在控制台中,如果我有任何问题,我会编辑问题,因为现在你已经很棒了?
  • 我试图发送一封带有服务的电子邮件,虽然它出错了,因为我在 setting.py 中设置了错误的信息,但你的代码实际上是 send 电子邮件,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
  • 2014-02-16
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
相关资源
最近更新 更多