簡體   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