コメントシステムを作りたい。、、、をとることはできますusername
がdoc_id
、をcomp_name
取得することはできませんcomment_others
。フォームの保存に問題があると思います。どうすれば修正できますか?
views.py
def ocr(request, id):
pdf = get_object_or_404(Pdf, id=id)
approval = ApprovalProcess(user_id=request.user, highest_rank=1)
current_user = request.user
userP = UserProfile.objects.get_or_create(username=current_user)
if request.method == 'POST':
form_2 = CommentForm(request.POST or None, instance=pdf)
if form_2.is_valid():
form_2.instance.username=request.user
form_2.instance.comp_name = userP[0].company
form_2.instance.doc_id = pdf
form_2.save()
else:
form_2 = CommentForm()
comment_obj = CommentFromOthers.objects.filter(doc_id=pdf).order_by("-created_date")
.....
models.py
class CommentFromOthers(models.Model):
comp_name = models.ForeignKey(CompanyProfile, on_delete=models.CASCADE, null=True)
doc_id = models.ForeignKey(Pdf, on_delete=models.DO_NOTHING, null=True)
comment_others = RichTextField(blank=True)
created_date = models.DateTimeField(default=datetime.now())
username = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
forms.py
class CommentForm(forms.ModelForm):
comments = RichTextField
class Meta:
model = CommentFromOthers
fields = ('comment_others',)
template.html
<div class="card">
<div class="card-header">
<div class="card-title">Risk Rating & Credit Limit</div>
</div>
<div class="card-body">
{% for comment in comment_obj %}
<li>
<h5>{{ comment.username }} - {{ comment.created_date }}</h5>
<h4>{{ comment.comment_others|safe}}</h4>
</li>
{% endfor %}
</div>
</div>
また、同じページに2つのフォームがあります。このフォームを保存すると、他のフォームが消えます。なぜそれが起こっているのでしょうか?
if request.method == 'POST':
form = PdfRiskForm(request.POST, request.FILES, instance=pdf)
if form.is_valid():
form.save()
...
approval.save()
else:
form = PdfRiskForm()
注:RichTextField
はdjango-ckeditorのテキストフィールドです。しかし、charfieldとして変更してもcomment_others
フィールドが保存されないため、この問題はそれに関係しません。
forms.py
comments = forms.CharField(widget=forms.Textarea)
views.py
if request.method == 'POST':
form_2 = CommentForm(request.POST or None, instance=id) #or user.id
if form_2.is_valid():
form_2.save()
views.pyでこれらの行を削除すると、フィールドがデータベースに自動的に保存されます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加