Here is the screenshot of what I'm seeing.我有2个应用帖子和评论。我试图在帖子上看到表格,但似乎无法显示表格。 这是我的代码:
模型-发布
from django.db import models
from django.utils import timezone
from django.conf import settings
from comments.models import Comment
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
class Post(models.Model):
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
@property
def comment_list(self):
instance = self
return Comment.object.filter_by_Post(instance)
def get_absolute_url(self):
return reverse("post_list")
@property
def get_content_type(self):
instance = self
content_type = ContentType.objects.get_for_model(instance.__class__)
return content_type
评论模型
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.utils import timezone
from django.conf import settings
from django.db import models
#from blog.models import Post
class CommentManager(models.Manager):
def filter_by_Post(self, instance):
content_type = ContentType.objects.get_for_model(instance.__class__)
qs = super(CommentManager, self).filter(content_type=content_type, object_id = instance.id)
return qs
class Comment(models.Model):
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
object=CommentManager()
def __str__(self):
return str(self.user.username)
评论表格
from django import forms
from datetime import datetime
from comments.models import Comment
from django.utils import timezone
class CommentForm(forms.Form):
content_type = forms.CharField(widget=forms.HiddenInput)
object_id = forms.IntegerField(widget=forms.HiddenInput)
title = forms.CharField(max_length=200)
text = forms.CharField()
created_date = forms.DateTimeField(initial=timezone.now)
观看次数-帖子
def post_detail(request, pk=None):
instance = get_object_or_404(Post, pk=pk)
comments =instance.comment_list
initial_data = {
"content_type": instance.get_content_type,
"object_id": instance.id
}
comment_form = CommentForm(request.POST or None, initial=initial_data)
if comment_form.is_valid():
c_type = comment_form.cleaned_data.get("content_type")
content_type = ContentType.objects.get(model=c_type)
obj_id = comment_form.cleaned_data.get("object_id")
title = comment_form.cleaned_data.get("title")
text = comment_form.cleaned_data.get("text")
created_date = comment_form.cleaned_data.get("created_date")
new_comment, created = Comment.object.get_or_create(
#user = request.user,
content_type= content_type,
object_id = obj_id,
title = title,
text = text,
created_date = created_date,
)
return HttpResponseRedirect(new_comment.content_object.get_absolute_url())
# if local variable 'context' referenced before assignment... this means that the context indentaion is off
context = {
"instance": instance,
"comments": comments,
"comment_form":comment_form,
}
return render(request, "blog/post_list.html", context)
网址-帖子
from django.conf.urls import url
from . import views
from comments.models import Comment
from comments.forms import CommentForm
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
url(r'^post/new/$', views.post_new, name='post_new'),
url(r'^post/(?P<pk>\d+)/edit/$', views.post_edit, name='post_edit'),
url(r'^drafts/$', views.post_draft_list, name='post_draft_list'),
url(r'^post/(?P<pk>\d+)/publish/$', views.post_publish, name='post_publish'),
url(r'^post/(?P<pk>\d+)/remove/$', views.post_remove, name='post_remove'),
url(r'^search/$', views.post_search, name='post_search'),
post_list.html
{%extends 'blog/post_detail.html'%}
{% block content %}
<h1> Blogs</h1>
{% for post in posts %}
<div class="post">
<div class="date">
<div class="comment">
<h5><a href="">{{ post.title }}</a></h5>
<p>{{ post.text|linebreaksbr }}</p>
{{post.published_date}}
{%endfor%}
</div>
<div>
<hr>
<H1> Comments</h1>
<div>
<p class='lead'>Comments</p>
<form method="POST" action="."> {% csrf_token %}
{{ comment_form.as_p }}
<input type='submit' value='Save' class='btn btn-default'>
</form>
<hr/>
{% endblock %}
如果您看到错误,请告诉我在哪里。在过去的2周中,我一直在尝试解决此问题。
谢谢
答案 0 :(得分:0)
尝试一下。在视图中实例化表单时,请执行以下操作:
CommentForm(data=request.POST if request.POST else None, initial=initial_data)
代替:
CommentForm(request.POST or None, initial=initial_data)
答案 1 :(得分:0)
您无法看到comment_form
,因为它在detail_view
中呈现,并且您正在查看list_view
。您需要处理comment_form
函数中的list_view
,然后才能看到它。我不知道在post_list.html
中渲染detail_view
有什么意义?希望这有用