繁体   English   中英

我需要将评论链接到 django 中的单个帖子,我不知道该怎么做,我是自学的

[英]I need to link comments to single post in django , i have no idea what to do , i'm self taught

我需要重要的帮助,我需要将评论链接到 django 中的单个帖子,我不知道该怎么做

我的模型我认为这是我知道发生了什么的唯一部分

from tkinter import CASCADE, Widget
from django.db import models
from django.contrib.auth.models import User

class Post (models.Model):
    id = models.AutoField(primary_key=True)
    name = models.ForeignKey(User, on_delete=models.CASCADE, default='User')
    text = models.TextField (max_length=200)
    posted = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        return self.text
    

class Comment(models.Model):#comment for the requests
    request = models.ForeignKey('sunu_app.Post', on_delete =models.CASCADE , related_name='comments', null=True , blank=True)
    # name = models.ForeignKey(User, on_delete=models.CASCADE , null=True , blank=True)
    text = models.TextField ()
    posted = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        return self.text

我的观点老实说,我不知道那里发生了什么

# from typing_extensions import Self
from urllib.request import Request
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from django.shortcuts import render,redirect
from .forms import *
from .models import *

def home(request):
    requests = Post.objects.all().order_by('-posted')
    count = Post.objects.all().count()
    #diplay the comments here and let it save in commentform.view
    comments = Post.comment_set.all()
    
    context = {'requests':requests , 'count':count , 'comments':comments}
    return render(request,'sunu_app/home.html', context)
    # context = {'cases':rooms} the second value is the data
    # you want to pass the first value is what it should be called in the template

def requestform (request):
    form = RequestForm(request.POST)
    if form.is_valid():
        form.save()
        return redirect('home')
    context = {'requestform':form}
    return render(request,'sunu_app/RequestForm.html',context)  

def deleteall(request):
    requests = Post.objects.all()
    requests.delete()
    return render(request,'sunu_app/home.html')

def commentform(request ,post_id):
    form = CommentForm(request.POST)
    requests = Post.objects.get(id = post_id)
    #save the comments here and let it display in home.view
    if request.method == 'POST':
        comment = Comment.objects.create{
            request = requests,
        }
        #pls i have no single idea what is goin on here
    
    context = {"commentform":form}
    return render(request,'sunu_app/CommentForm.html',context)

我的 home.html 我也不知道这里发生了什么

 <!DOCTYPE html>

    {%extends 'main.html'%}
    {% block content%}   
    <!-- <style>
        .home-container{
            display: grid;
            grid-template-columns: 1fr 2fr 1fr;
        }
    </style> -->
    <!-- put everything in a freaking div -->
       
        <div class="addrequest">
            <a href="{% url 'requestform'  %}">
                <h3>Add request</h3>
            </a>
            <a href="{% url 'deleteall' %}">
                delete all Requests
            </a>
        </div>

        <div class="intronote">
            <p>
                Lorem ipsum dolor sit amet consectetur adipisicing elit. Commodi hic maiores
                quisquam sed ipsa incidunt iste, minima aperiam est sunt
                nobis, soluta adipisci laborum esse. Placeat praesentium quibusdam corrupti quis?
                Natus doloremque illum commodi unde nostrum expedita
                tempora sapiente magnam asperiores, placeat dolorum vitae repellendus possimus ut,
            </p>
            <div>
                <h4>{{count}} Requests in total</h4>
            </div>
            <hr>
        </div>

        <div class="allrequests">

            {% for post in requests %}
                <div>
                    <!-- request -->
                    <h4>{{post.name}}  the request id is : {{post.id}} posted <strong>{{post.posted}}</strong></h4>
                    <p>--{{post.text}}</p> 


                    
                    <!--comment -->
                    <h5>Comments</h5>
                    <p>{{post.comments.all}}</p>
                    

                    <!-- comment input -->
                    <a href="{% url 'commentform' post.id  %}">
                        <h3>Add Comment</h3>
                    </a> 
                   
                </div>
            
                <hr>    
            {% endfor %}

        </div> 
    
    {% endblock content %}

如果您还可以给我任何其他提示,我将不胜感激。 谢谢

在这里,您已经设置了它们之间的关系:

class Comment(models.Model):
    request = models.ForeignKey('sunu_app.Post', ...)
    ...

因此,当您创建新的Comment对象时,您需要将Post id 或整个对象传递给字段request (我强烈建议将其命名为post而不是request ):

post = Post.objects.get(id=1)
comment = Comment.objects.create(    # use round parenthesis
    request = post,
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM