如何将我的表单应用于django中的多个模板
我正在创建一个博客网站,用户可以在主页上发表文章,然后也可以使用表单对其进行评论。这些帖子也可以在网站的其他地方查看,比如搜索结果和用户档案。我想做的是允许用户在任何地方发表评论。为此,我在我的评论表单中使用了一个包含标签,如下所示:如何将我的表单应用于django中的多个模板,django,forms,inclusion,Django,Forms,Inclusion,我正在创建一个博客网站,用户可以在主页上发表文章,然后也可以使用表单对其进行评论。这些帖子也可以在网站的其他地方查看,比如搜索结果和用户档案。我想做的是允许用户在任何地方发表评论。为此,我在我的评论表单中使用了一个包含标签,如下所示: @register.inclusion_tag('posts/comment.html') def comment_create_and_list_view(request): profile = Profile.objects.get(user=requ
@register.inclusion_tag('posts/comment.html')
def comment_create_and_list_view(request):
profile = Profile.objects.get(user=request.user)
c_form = CommentModelForm()
if 'submit_c_form' in request.POST:
c_form = CommentModelForm(request.POST)
if c_form.is_valid():
instance = c_form.save(commit=False)
instance.user = profile
instance.post = Post.objects.get(id=request.POST.get('post_id'))
instance.save()
c_form = CommentModelForm()
context = {
'profile': profile,
'c_form': c_form,
}
return context
from django.urls import path
from .templatetags.custom_tags import comment_create_and_list_view
from .views import *
app_name = 'posts'
urlpatterns = [
path('comment/<int:pk>/', comment_create_and_list_view, name='comment'),
]
class CommentModelForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('body',)
<form action="{% url 'posts:comment' post.id %}" method="post">
{% csrf_token %}
<input type="hidden" name="post_id" value={{ post.id }}>
{{ c_form }}
<button type="submit" name="submit_c_form">Send</button>
</form>
并注册到我的URL,如下所示:
@register.inclusion_tag('posts/comment.html')
def comment_create_and_list_view(request):
profile = Profile.objects.get(user=request.user)
c_form = CommentModelForm()
if 'submit_c_form' in request.POST:
c_form = CommentModelForm(request.POST)
if c_form.is_valid():
instance = c_form.save(commit=False)
instance.user = profile
instance.post = Post.objects.get(id=request.POST.get('post_id'))
instance.save()
c_form = CommentModelForm()
context = {
'profile': profile,
'c_form': c_form,
}
return context
from django.urls import path
from .templatetags.custom_tags import comment_create_and_list_view
from .views import *
app_name = 'posts'
urlpatterns = [
path('comment/<int:pk>/', comment_create_and_list_view, name='comment'),
]
class CommentModelForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('body',)
<form action="{% url 'posts:comment' post.id %}" method="post">
{% csrf_token %}
<input type="hidden" name="post_id" value={{ post.id }}>
{{ c_form }}
<button type="submit" name="submit_c_form">Send</button>
</form>
comment.html如下所示:
@register.inclusion_tag('posts/comment.html')
def comment_create_and_list_view(request):
profile = Profile.objects.get(user=request.user)
c_form = CommentModelForm()
if 'submit_c_form' in request.POST:
c_form = CommentModelForm(request.POST)
if c_form.is_valid():
instance = c_form.save(commit=False)
instance.user = profile
instance.post = Post.objects.get(id=request.POST.get('post_id'))
instance.save()
c_form = CommentModelForm()
context = {
'profile': profile,
'c_form': c_form,
}
return context
from django.urls import path
from .templatetags.custom_tags import comment_create_and_list_view
from .views import *
app_name = 'posts'
urlpatterns = [
path('comment/<int:pk>/', comment_create_and_list_view, name='comment'),
]
class CommentModelForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('body',)
<form action="{% url 'posts:comment' post.id %}" method="post">
{% csrf_token %}
<input type="hidden" name="post_id" value={{ post.id }}>
{{ c_form }}
<button type="submit" name="submit_c_form">Send</button>
</form>
{%csrf_令牌%}
{{c_form}}
发送
我正在使用{%comment\u create\u And\u list\u view request%}
尝试加载页面时,我收到NoReverseMatch at/error:
未找到参数为“(“”,)”的“comment”的反向。尝试了1个模式:[“注释/(?P[0-9]+)/$”]
在谷歌上搜索了几个小时,不知道我哪里出了问题…如果有一天这对其他人有帮助,我通过重构包含标签来修复我的问题,只呈现表单:
@register.inclusion_tag('posts/comment.html')
def comment_tag():
c_form = CommentModelForm()
return {'c_form': c_form}
并添加了处理提交的视图:
def comment_view(request):
profile = Profile.objects.get(user=request.user)
if 'submit_c_form' in request.POST:
c_form = CommentModelForm(request.POST)
if c_form.is_valid():
instance = c_form.save(commit=False)
instance.user = profile
instance.post = Post.objects.get(id=request.POST.get('post_id'))
instance.save()
return redirect('posts:index')
然后,我调整了comment.html以简单地应用带有{{c_form}}
的表单,并在我的索引中用form元素包装了我的模板标记:
<form action="{% url 'posts:comment' %}" method="post">
{% csrf_token %}
<input typ="hidden" name="post_id" value={{ post.id }}>
{% comment_tag %}
<button type="submit" name="submit_c_form">Send</button>
</form>
{%csrf_令牌%}
{%comment_tag%}
发送
您可以在表单中使用操作谢谢,您是对的,我忘了这么做。我已经编辑了我的问题。但是我的表格仍然没有显示您可以使用templatetagsCan,请评估?如何使用templatetags进行此操作?