尽管进行了迁移,但没有这样的列出现错误:blog_comment.url

时间:2020-04-17 23:08:21

标签: python django python-3.x

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

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

现在,当我尝试更新帖子时,我收到一条错误消息。

最初,我认为问题是因为我没有在班级中添加名为“注释”的子字段。然后,我添加了url= models.SlugField(max_length=500, unique=True, blank=True)。现在是否需要重置链接到此类的数据库?如果是这样,我该如何实现?还是我正切线不正确?

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


# Generated by Django 2.2.6 on 2020-04-20 13:34

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

    dependencies = [
        ('blog', '0013_auto_20200410_0107'),
    ]

    operations = [
        migrations.AddField(
            model_name='comment',
            name='url',
            field=models.SlugField(blank=True, max_length=500, unique=True),
        ),
        migrations.AlterField(
            model_name='post',
            name='date_posted',
            field=models.DateTimeField(default=datetime.datetime(2020, 4, 20, 13, 34, 51, 980840, tzinfo=utc)),
        ),
    ]

0 个答案:

没有答案