我正在写一个Django博客。我在Django admin(Category
,Post
)中注册了两个模型。我想添加一个带有某个类别的新帖子,但我收到了以下错误:
Internal Server Error: /admin/blog/post/add/
Traceback (most recent call last):
File "/home/pecan/env/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/pecan/env/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/pecan/env/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/pecan/env/lib/python3.4/site-packages/django/contrib/admin/options.py", line 551, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/pecan/env/lib/python3.4/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/pecan/env/lib/python3.4/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/pecan/env/lib/python3.4/site-packages/django/contrib/admin/sites.py", line 224, in inner
return view(request, *args, **kwargs)
File "/home/pecan/env/lib/python3.4/site-packages/django/contrib/admin/options.py", line 1508, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "/home/pecan/env/lib/python3.4/site-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/home/pecan/env/lib/python3.4/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/pecan/env/lib/python3.4/site-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/pecan/env/lib/python3.4/site-packages/django/contrib/admin/options.py", line 1408, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "/home/pecan/env/lib/python3.4/site-packages/django/contrib/admin/options.py", line 1440, in _changeform_view
if form.is_valid():
File "/home/pecan/env/lib/python3.4/site-packages/django/forms/forms.py", line 183, in is_valid
return self.is_bound and not self.errors
File "/home/pecan/env/lib/python3.4/site-packages/django/forms/forms.py", line 175, in errors
self.full_clean()
File "/home/pecan/env/lib/python3.4/site-packages/django/forms/forms.py", line 386, in full_clean
self._post_clean()
File "/home/pecan/env/lib/python3.4/site-packages/django/forms/models.py", line 414, in _post_clean
self.validate_unique()
File "/home/pecan/env/lib/python3.4/site-packages/django/forms/models.py", line 423, in validate_unique
self.instance.validate_unique(exclude=exclude)
File "/home/pecan/env/lib/python3.4/site-packages/django/db/models/base.py", line 1034, in validate_unique
date_errors = self._perform_date_checks(date_checks)
File "/home/pecan/env/lib/python3.4/site-packages/django/db/models/base.py", line 1145, in _perform_date_checks
date = getattr(self, unique_for)
TypeError: getattr(): attribute name must be string
[21/Sep/2017 22:16:01] "POST /admin/blog/post/add/ HTTP/1.1" 500 143101
[21/Sep/2017 22:16:16] "GET /admin/blog/post/add/ HTTP/1.1" 200 7865
[21/Sep/2017 22:16:48] "GET /static/admin/img/calendar-icons.svg HTTP/1.1" 200 1094
我做错了什么?
这是文件models.py
:
from django.contrib.auth.admin import User
from django.db import models
from django.utils import timezone
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=50)
description = models.TextField(blank=True)
class Post(models.Model):
title = models.CharField(max_length=250)
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="post_author")
slug = models.SlugField(unique_for_date=True)
content = models.TextField(blank=True)
created = models.DateField(default=timezone.now)
modified = models.DateField(auto_now_add=True)
published = models.DateField(auto_now=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="post_category")
和admin.py
:
from django.contrib import admin
from . import models
# Register your models here.
admin.site.register(models.Post)
admin.site.register(models.Category)
希望得到帮助!
答案 0 :(得分:1)