简体   繁体   English

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

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

i need crucial help, i need to link comments to single post in django , i have no idea what to do我需要重要的帮助,我需要将评论链接到 django 中的单个帖子,我不知道该怎么做

my models i think this is the only part that i know what is goin on我的模型我认为这是我知道发生了什么的唯一部分

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

my views i honestly had no idea what was going on there我的观点老实说,我不知道那里发生了什么

# 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)

my home.html i also have no idea what is goin on here我的 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 %}

pls if you can also give me any other tips i would highly appreciate it.如果您还可以给我任何其他提示,我将不胜感激。 thanks谢谢

Here you have set relation between them:在这里,您已经设置了它们之间的关系:

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

So while you create new Comment object, you need to pass Post id or whole object to field request (I strongly recommend naming it post instead of request ):因此,当您创建新的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.

相关问题 我正在阅读自学程序员 ch17 这本书,但我不明白为什么这不起作用 - I'm reading the book the self taught programmer ch17 and I can't figure out why this dosn't work 当我需要一本自参考字典时我该怎么办? - What do I do when I need a self referential dictionary? 我正在尝试链接 Django 中特定用户帐户创建的帖子 - I'm trying to link the post created by the specific user account in Django 我无法在 django 中链接页面我的代码是正确的。我还需要做什么? - i am not able to link pages in django my code is correct .What else i need to do? 我是 Jupyter 的新手,不知道我在做什么有什么问题 - I'm brand new to Jupyter and have no idea what is wrong with what I am doing 大学问题,我不知道该怎么办(质数) - College problem and I have no idea what to do (Prime numbers) 我还需要做什么才能让 Django 的 @login_required 装饰器工作? - What more do I need to do to have Django's @login_required decorator work? 为了在django中为管理站点提供自定义字符串表示形式,我需要在模型或字段中重写什么? - What do i need to override in a Model or Field in order to have a custom string representation for the admin site in django? 我需要为自己开发的自定义字段编写哪些测试? - What tests do I need to write for the customfield that I have developed? 我需要迁移以将数据库链接到Django吗 - Do I Need to Migrate to Link my Database to Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM