【发布时间】:2020-03-21 23:22:59
【问题描述】:
我无法添加form-group(我相信它是引导类)。
表单组根本没有做任何事情,或者可能是form.author 和form-body 变量的问题!?
更简单地说,我需要 UI 评论部分(现在只有我可以从 django 管理页面添加和编辑 cmets)。一些代码:
post_details.html
<article class="media content-section">
<form action="/post/{{ post.slug }}/" method="post">
{% csrf_token %}
<div class="form-group">
{{ form.author }}
</div>
<div class="form-group">
{{ form.body }}
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<ul>
{% for comment in post.comments.all %}
<p>
<b>@{{ comment.author }}</b>
<small>{{ comment.created_date }} </small>
</p>
<p> {{ comment.text }}</p>
<hr>
{% if comment.replies.all %}
<ul>
{% for reply in comment.replies.all %}
<p>{{ reply.text }}</p>
<hr>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
<ul>
</article>
forms.py
from django import forms
class CommentForm(forms.Form):
author = forms.CharField(
max_length=60,
widget=forms.TextInput(
attrs={"class": "form-control", "placeholder": "Your Name"}
),
)
body = forms.CharField(
widget=forms.Textarea(
attrs={"class": "form-control", "placeholder": "Leave a comment!"}
)
)
views.py
def comment(request):
form = CommentForm()
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = Comment(
author=form.cleaned_data["author"],
body=form.cleaned_data["body"],
post=post,
)
comment.save()
context = {"post": post, "comments": comments, "form": form}
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = Comment(
author=form.cleaned_data["author"],
body=form.cleaned_data["body"],
post=post
)
comment.save()
comments = Comment.objects.filter(post=post)
context = {
"post": post,
"comments": comments,
"form": form,
}
models.py
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(User, on_delete=models.CASCADE)
text = models.TextField()
created_date = models.DateField(auto_now_add=True)
def __str__(self):
return self.text
编辑:
urls.py
from django.urls import path
from django.conf.urls import include, url
from . import views
from .views import PostListView, PostDetailView, PostCreateView, PostUpdateView, PostDeleteView, UserPostListView
urlpatterns = [
#Blog section
path("", PostListView.as_view(), name='blog-home'),
path("user/<str:username>", UserPostListView.as_view(), name='user-posts'),
path('post/<slug:slug>/', PostDetailView.as_view(), name='post-detail'),
path("posts/new/", PostCreateView.as_view(), name='post-create'),
path("post/<slug:slug>/update/", PostUpdateView.as_view(), name='post-update'),
path("post/<slug:slug>/delete/", PostDeleteView.as_view(), name='post-delete'),
path("about/", views.about, name="blog-about"),
path("<category>/", views.blog_category, name="blog_category"),
]
我真的需要这样的东西(尝试关注this tutorial,但没有任何效果:
我的评论区:
【问题讨论】:
-
你能提供你的urls.py吗?
-
添加了 urls.py。见编辑
-
嗨,您是否添加了 Stéphane 指出的 return 语句?
-
@EliakinCosta 我认为问题不在于回报,问题在于我没有允许用户输入评论信息的表格(作者和正文),但是,正如我所提到的,我可以在 django 管理页面中添加 cmets。请看教程,也许我丢失了一些东西:realpython.com/get-started-with-django-1/…
-
当然,这也是一个问题。您的观点不正确,因为您没有指向 view.comment。 @C O D E
标签: django django-models bootstrap-4 django-forms django-templates