如何在 Django 中使用基于函数的视图查看帖子?
How to see posts using functions based view in django?
在我当前的 django 项目中,我必须制作一个函数来呈现主页,以便我可以在其中制作一个表单,该表单位于 base.html 中,以便可以在每个页面中呈现 post的网站,目前的代码是这样的:
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})
有效,但每个 post 的视图都是基于 class 的视图,可以完美显示表单:
class PostDetail(generic.DetailView):
model = Post
extra_context = {'form': ContactForm()}
template_name = 'post_detail.html'
唯一的问题是主页中的表格可以工作,因为有代码,同样的代码我不知道如何调用 class(否则我会使用 class 甚至对于家庭)
更详细的 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 表单中,有一个 slug 被添加到新呈现页面的路径中,如果我单击 fourth post
,我将被重定向到 HTTP://127.0。 0.1/第四-post.
我的问题是,每个 post 中的表单都不起作用,但仅在主页中起作用,这显然是因为没有代码。所以我可以做两件不同的事情:
- 重新制作 post class 使其基本上成为主页功能的副本
- 创建一个方法,其中包含使代码有效的相同代码
我想做第二个,但作为 django 的新手我不知道该怎么做,任何帮助或见解都会很棒
您可以在 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})
对于 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})
在我当前的 django 项目中,我必须制作一个函数来呈现主页,以便我可以在其中制作一个表单,该表单位于 base.html 中,以便可以在每个页面中呈现 post的网站,目前的代码是这样的:
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})
有效,但每个 post 的视图都是基于 class 的视图,可以完美显示表单:
class PostDetail(generic.DetailView):
model = Post
extra_context = {'form': ContactForm()}
template_name = 'post_detail.html'
唯一的问题是主页中的表格可以工作,因为有代码,同样的代码我不知道如何调用 class(否则我会使用 class 甚至对于家庭)
更详细的 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 表单中,有一个 slug 被添加到新呈现页面的路径中,如果我单击 fourth post
,我将被重定向到 HTTP://127.0。 0.1/第四-post.
我的问题是,每个 post 中的表单都不起作用,但仅在主页中起作用,这显然是因为没有代码。所以我可以做两件不同的事情:
- 重新制作 post class 使其基本上成为主页功能的副本
- 创建一个方法,其中包含使代码有效的相同代码
我想做第二个,但作为 django 的新手我不知道该怎么做,任何帮助或见解都会很棒
您可以在 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})
对于 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})