我如何使用 Django comment_set.all
how can i use django comment_set.all
我的问题
为 Django 制作评论模型和表单
在 post.comment_set.all % 中使用 {% for i}
无变化html
评论表中保存的信息没有出现
我该如何解决?
我的top_detail.html
<form method="POST" action="{% url 'create_comment' post.id %}">
{% csrf_token %}
{{ comment_form }}
<input type="submit">
{% for i in post.comment_set.all %}
<p>comment: {{ i.subject }}</p>
<p>time: {{ i.created_at }}</p>
<hr>
{% endfor %}
</form>
我的models.py
from django.db import models
class Top(models.Model):
product_name = models.CharField(blank=True, null=True, max_length=30)
product_price = models.CharField(blank=True, null=True, max_length=30)
product_image = models.ImageField(blank=True, null=True, upload_to="images")
product_explain = models.TextField(blank=True, null=True, )
class Question(models.Model):
post = models.ForeignKey(Top, null=True, on_delete=models.CASCADE)
subject = models.CharField(null=True, max_length=150)
created_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.subject
我的urls.py
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
from .views import PostList
urlpatterns = [
path('home/', views.home, name='home'),
path('home/top', PostList.as_view(), name='top'),
path('home/top/<int:pk>/', views.top_detail, name='top_detail'),
path('search/', views.search, name='search'),
path('signup/', views.signup, name='signup'),
path('login/', auth_views.LoginView.as_view(template_name='main/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('create_comment/<int:post_id>', views.create_comment, name='create_comment'),
]
我的views.py
def top_detail(request,pk):
post = get_object_or_404(Top, pk=pk)
post_list = Top.objects.get(pk=pk)
comment_form = CommentForm()
return render(request, 'main/top_detail.html', {'post':post, 'post_list':post_list,'comment_form':comment_form})
def create_comment(request, post_id):
filled_form = CommentForm(request.POST)
if filled_form.is_valid():
form = filled_form.save(commit=False)
form.post = Top.objects.get(id=post_id)
form.save()
return redirect('top_detail',post_id)
我的forms.py
class CommentForm(forms.ModelForm):
class Meta:
model = Question
fields = ('subject',)
top_detail和create_comment(请求,post_id)在VIEWS.py中的pk部分应该一样吗?
你在混淆名字。如果您将某物命名为 Question
,请不要将其命名为 Comment
。 Post
和 Top
也一样。这对您的代码很重要。
目前,您必须使用:
{{ post.question_set.all }}
但如果我是你,我会更改命名,因为看起来连你都不明白它是如何工作的:)
喜欢:
class Post(models.Model):
...
class Comment(models.Model):
post = models.ForeignKey(Post, null=True, on_delete=models.CASCADE)
...
then in template:
{{ post.comment_set.all }}
我的问题
为 Django 制作评论模型和表单
在 post.comment_set.all % 中使用 {% for i}
无变化html
评论表中保存的信息没有出现
我该如何解决?
我的top_detail.html
<form method="POST" action="{% url 'create_comment' post.id %}">
{% csrf_token %}
{{ comment_form }}
<input type="submit">
{% for i in post.comment_set.all %}
<p>comment: {{ i.subject }}</p>
<p>time: {{ i.created_at }}</p>
<hr>
{% endfor %}
</form>
我的models.py
from django.db import models
class Top(models.Model):
product_name = models.CharField(blank=True, null=True, max_length=30)
product_price = models.CharField(blank=True, null=True, max_length=30)
product_image = models.ImageField(blank=True, null=True, upload_to="images")
product_explain = models.TextField(blank=True, null=True, )
class Question(models.Model):
post = models.ForeignKey(Top, null=True, on_delete=models.CASCADE)
subject = models.CharField(null=True, max_length=150)
created_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.subject
我的urls.py
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
from .views import PostList
urlpatterns = [
path('home/', views.home, name='home'),
path('home/top', PostList.as_view(), name='top'),
path('home/top/<int:pk>/', views.top_detail, name='top_detail'),
path('search/', views.search, name='search'),
path('signup/', views.signup, name='signup'),
path('login/', auth_views.LoginView.as_view(template_name='main/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('create_comment/<int:post_id>', views.create_comment, name='create_comment'),
]
我的views.py
def top_detail(request,pk):
post = get_object_or_404(Top, pk=pk)
post_list = Top.objects.get(pk=pk)
comment_form = CommentForm()
return render(request, 'main/top_detail.html', {'post':post, 'post_list':post_list,'comment_form':comment_form})
def create_comment(request, post_id):
filled_form = CommentForm(request.POST)
if filled_form.is_valid():
form = filled_form.save(commit=False)
form.post = Top.objects.get(id=post_id)
form.save()
return redirect('top_detail',post_id)
我的forms.py
class CommentForm(forms.ModelForm):
class Meta:
model = Question
fields = ('subject',)
top_detail和create_comment(请求,post_id)在VIEWS.py中的pk部分应该一样吗?
你在混淆名字。如果您将某物命名为 Question
,请不要将其命名为 Comment
。 Post
和 Top
也一样。这对您的代码很重要。
目前,您必须使用:
{{ post.question_set.all }}
但如果我是你,我会更改命名,因为看起来连你都不明白它是如何工作的:)
喜欢:
class Post(models.Model):
...
class Comment(models.Model):
post = models.ForeignKey(Post, null=True, on_delete=models.CASCADE)
...
then in template:
{{ post.comment_set.all }}