尝试解决以下问题:blog_comment.url

时间:2020-04-20 14:23:00

标签: python django python-3.x

用户登录到我的网站后,他可以写一个帖子并对其进行更新。

然后,我在添加允许人们发表评论的功能方面取得了进展。我当时处于可以从后端添加评论的位置,并且可以将它们准确地显示在前端。

现在,当我尝试更新帖子时,我收到一条错误消息。最初,我认为问题是因为我没有包括一个子弹场,并且没有进行适当的迁移。然后,我删除了错误消息中突出显示的代码的特定部分,但这只是意味着标记了下一行代码。

enter image description here enter image description here

models.py

from django.db import models
from django.utils.text import slugify
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse


# Create your models here.
class Post(models.Model):
    title = models.CharField(max_length=100)
    content =  models.TextField()
    date_posted = models.DateTimeField(default=timezone.now())
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    url= models.SlugField(max_length=500, unique=True, blank=True)

    def save(self, *args, **kwargs):
        self.url= slugify(self.title)
        super().save(*args, **kwargs)

    def __str__(self):
        return self.title 

    def get_absolute_url(self):
        return reverse('article_detail', kwargs={'slug': self.slug})


class Comment(models.Model):
    post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name='comments')
    name = models.CharField(max_length=80)
    email = models.EmailField()
    body = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=False)
    url= models.SlugField(max_length=500, unique=True, blank=True)

    class Meta:
        ordering = ['created_on']

    def __str__(self):
        return 'Comment {} by {}'.format(self.body, self.name)

    def save(self, *args, **kwargs):
        self.url= slugify(self.post)
        super().save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse('article_detail', kwargs={'slug': self.slug})

views.py

def post_detail(request, pk):
template_name = 'post_detail.html'

comments = Comment.objects.filter(post=pk ,active=True)
post = Post.objects.get(pk=pk)
new_comment = None
# Comment posted
if request.method == 'POST':
    comment_form = CommentForm(data=request.POST)
    if comment_form.is_valid():

        # Create Comment object but don't save to database yet
        new_comment = comment_form.save(commit=False)
        # Assign the current post to the comment
        new_comment.post = post
        # Save the comment to the database
        new_comment.save()
else:
    comment_form = CommentForm()

return render(request, template_name, {'post': post,
                                       'comments': comments,
                                       'new_comment': new_comment,
                                       'comment_form': comment_form})

更新-迁移

我现在已经(或试图进行)必要的迁移。

WARNINGS:
?: (2_0.W001) Your URL pattern '^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$' [name='activate'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().
blog.Post.date_posted: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
Migrations for 'blog':
  blog\migrations\0014_auto_20200420_2034.py
    - Add field url to comment
    - Alter field date_posted on post

更新-修改了created_on

class Comment(models.Model):
    post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name='comments')
    name = models.CharField(max_length=80)
    email = models.EmailField()
    body = models.TextField()
    created_on= models.DateTimeField(default = timezone.now())
    active = models.BooleanField(default=False)
    url= models.SlugField(max_length=500, unique=True, blank=True)

1 个答案:

答案 0 :(得分:0)

将字段更改为以下内容:

function App(children) {
  let [width, height] = WindowSize(); // to watch the window Resize
  console.log(width, height)
  return  (
    <div className="table-container">
      <div className="table-container-content">
        <table>
          <thead>
            <tr>
              <th data-type="text-short">
                From <span class="resize-handle"></span>
              </th>
              <th data-type="text-short">
                To<span class="resize-handle"></span>
              </th>
              <th data-type="text-short"></th>
              <th data-type="text-short">
                Subject <span class="resize-handle"></span>
              </th>
              <th></th>
              <th data-type="text-short">
                Date <span class="resize-handle"></span>
              </th>
            </tr>
          </thead>
          <tbody>
            {userData.map((row, index) => (
              <tr key={index}>
                <td>{row.from}</td>
                <td>{row.to}</td>
                <td>
                  <div className="bro-fg">+1</div>
                </td>
                <td>{row.subject}</td>
                <td>
                  <img className="icon_clip" src={Icon_clip} fill="#024873" />
                </td>
                <td>{rightDateFormat(row.date)}</td> //formating my dates
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </div>
  );

}

ReactDOM.render(<App />, document.getElementById('root'));

请研究文档:

1。https://docs.djangoproject.com/en/3.0/ref/models/fields/