Django 2.0 CreateView AttributeError

一月

为什么会出现AttributeError:'function'对象没有属性'day'吗?

我想为编辑博客文章创建简单的通用视图。

views.py:

class CreatePostView(LoginRequiredMixin, CreateView):
   login_url = '/login/'
   redirect_field_name = 'blog/post_detail.html'
   model = Post
   form_class = PostForm

在models.py中发布模型如下所示:

class Post(models.Model):
    title = models.CharField(max_length=200)
    short_description = models.CharField(max_length=350)
    slug = models.SlugField(unique_for_date='publish')
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    text = models.TextField()
    created_date = models.DateTimeField(auto_now_add=True)
    published_date = models.DateTimeField(default=None, blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def get_absolute_url(self):
        ret_url_context = {'pk':self.pk}
        if self.published_date:
           ret_url_context['slug'] = self.slug
           ret_url_context['year'] = self.published_date.year
           ret_url_context['month'] = self.published_date.month
           ret_url_context['day'] = self.published_date.day
        return reverse("post_detail",kwargs=ret_url_context)

    def __str__(self):
        return self.title

urls.py:

urlpatterns = [
    path('', views.PostListView.as_view(), name='post_list'),
    path('about/', views.AboutView.as_view(), name='about'),
    path('drafts/', views.DraftListView.as_view(), name='post_draft_list'),
    path('post/new/', views.CreatePostView.as_view(), name='post_new'),
    path('post/<int:pk>/edit/', views.PostUpdateView.as_view(), name='post_edit'),
    path('post/<int:pk>/remove/', views.PostDeleteView.as_view(), name='post_remove'),
    path('post/<int:pk>/publish/', views.post_publish, name='post_publish'),
    path('<int:year>/<int:month>/<int:day>/<slug:slug>/', views.PostDetailView.as_view(), name="post_detail_date"),
    path('post/<str:slug>/', views.PostDetailView.as_view(), name='post_detail'),
    ]

最后,我的html表单是简单的post_form.html:

{% extends 'blog/base.html' %}
{% block content %}
    <h1>New post</h1>

    <form method="POST" class="post-form">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="save btn btn-default">Save</button>
    </form>

{% endblock %}

我在forms.py中的表格:

class PostForm(forms.ModelForm):

    class Meta:
        model = Post
        fields = 'title', 'slug', 'short_description', 'author', 'text', 'published_date'
        #fields = '__all__'

        widgets = {
            'title': forms.TextInput(attrs={'class': 'textinputclass'}),
            'text': forms.Textarea(attrs={'class': 'editable medium-editor-textarea postcontent'}),
            'published_date': forms.DateTimeInput(attrs={},format="%Y-%m-%d %H:%M"),
        }

我不知道应该从哪里开始寻找解决方案。但是我认为问题出在插入日期和时间。我要在编辑器中插入以下文本:12.12.2018 13:452018-03-13 13:45-两者都不起作用。

加内什·内吉

unique_for_date模型字段上的属性应设置为DateField或的名称DateTimeField它确保使用unique_for_date并将DateFieldDateTimeField设置为,将为字段组合提供唯一记录unique_for_date

例如,如果您有两个字段:

published_date = models.DateTimeField()
slug = models.SlugField(unique_for_date="published_date")

然后,Django的将不允许的两个记录具有相同的入口published_dateslug

您可以在django docs上阅读有关此内容的更多信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章