我在浏览器中收到此异常,我已经看到多达20个与此错误相关的帖子,但我找不到任何解决方案。我正在使用Postgre数据库(pgAdmin 4)。我是Django的新手,请帮助我,提前致谢。
我从djangogirls教程http://tutorial.djangogirls.org/en/#django-girls-tutorial做了一个项目。然后我将我的数据库更改为postgre,并且prject正在按预期运行。然后我制作了这个项目的副本并存储在我的电脑里。然后我使用相同的数据库设置对原始项目进行了一些更改。
当我运行我之前项目的副本时,我发现了这个错误: -
项目名称/ urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('blog.urls')),
]
我的views.py
def blog_list(request):
posts = Blog.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
return render(request, 'blog/blog_list.html', {'posts': posts})
blog_list.html
{% for post in posts %}
<div class="post">
<div class="date">
{{ post.published_date }}
</div>
<h1><a href="{% url 'blog_detail' pk=post.pk %}">{{ post.title }}</a></h1>
<p>{{ post.short_description|linebreaksbr }}</p>
</div>
{% endfor %}
my models.py
class Blog(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
sub_title = models.CharField(max_length=100, null = True)
short_description = models.CharField(max_length=100, null = True)
long_description = models.TextField(null = True)
#text = models.TextField()
published_date = models.DateTimeField(blank=True, null=True)
created_date = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
class DeletedPost(models.Model):
post_title = models.CharField(max_length=200, null=True)
deleted = models.DateTimeField(default = timezone.now)
我的appname / urls.py
urlpatterns = [
url(r'^$', views.blog_list, name='blog_list'),
url(r'^blog/(?P<pk>\d+)/$', views.blog_detail, name='blog_detail'),
url(r'^blog/new/$', views.blog_new, name='blog_new'),
url(r'^blog/(?P<pk>\d+)/edit/$', views.blog_edit, name='blog_edit'),
url(r'^blog/(?P<pk>\d+)/delete/$', views.post_delete, name='post_delete'),
]
浏览器出错:
回溯:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.10
Python Version: 3.4.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template D:\mysite\blog\templates\blog\blog_list.html, error at line 4
relation "blog_blog" does not exist
LINE 1: ..."."created_date", "blog_blog"."last_updated" FROM "blog_blog...
^
1 : {% extends 'blog/base.html' %}
2 :
3 : {% block content %}
4 : {% for post in posts %}
5 : <div class="post">
6 : <div class="date">
7 : {{ post.published_date }}
8 : </div>
9 : <h1><a href="{% url 'blog_detail' pk=post.pk %}">{{ post.title }}</a></h1>
10 : <p>{{ post.short_description|linebreaksbr }}</p>
11 : </div>
12 : {% endfor %}
13 : {% endblock %}
Traceback:
File "C:\Python34\lib\site-packages\django\db\backends\utils.py" in execute
64. return self.cursor.execute(sql, params)
The above exception (relation "blog_blog" does not exist
LINE 1: ..."."created_date", "blog_blog"."last_updated" FROM "blog_blog...
^
) was the direct cause of the following exception:
File "C:\Python34\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\mysite\blog\views.py" in blog_list
10. return render(request, 'blog/blog_list.html', {'posts': posts})
File "C:\Python34\lib\site-packages\django\shortcuts.py" in render
30. content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Python34\lib\site-packages\django\template\loader.py" in render_to_string
68. return template.render(context, request)
File "C:\Python34\lib\site-packages\django\template\backends\django.py" in render
66. return self.template.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
208. return self._render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in _render
199. return self.nodelist.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
994. bit = node.render_annotated(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render_annotated
961. return self.render(context)
File "C:\Python34\lib\site-packages\django\template\loader_tags.py" in render
174. return compiled_parent._render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in _render
199. return self.nodelist.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
994. bit = node.render_annotated(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render_annotated
961. return self.render(context)
File "C:\Python34\lib\site-packages\django\template\loader_tags.py" in render
70. result = block.nodelist.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
994. bit = node.render_annotated(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render_annotated
961. return self.render(context)
File "C:\Python34\lib\site-packages\django\template\defaulttags.py" in render
166. len_values = len(values)
File "C:\Python34\lib\site-packages\django\db\models\query.py" in __len__
238. self._fetch_all()
File "C:\Python34\lib\site-packages\django\db\models\query.py" in _fetch_all
1085. self._result_cache = list(self.iterator())
File "C:\Python34\lib\site-packages\django\db\models\query.py" in __iter__
54. results = compiler.execute_sql()
File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
835. cursor.execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\backends\utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\backends\utils.py" in execute
64. return self.cursor.execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\utils.py" in __exit__
94. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python34\lib\site-packages\django\utils\six.py" in reraise
685. raise value.with_traceback(tb)
File "C:\Python34\lib\site-packages\django\db\backends\utils.py" in execute
64. return self.cursor.execute(sql, params)
Exception Type: ProgrammingError at /
Exception Value: relation "blog_blog" does not exist
LINE 1: ..."."created_date", "blog_blog"."last_updated" FROM "blog_blog...
^
答案 0 :(得分:3)
尝试manage.py makemigrations blog
和manage.py migrate
。有时候manage.py makemigrations
不会反映所有变化。
错误告诉您数据库中没有用于提取所需数据的表blog_blog
。你分享的代码看起来很好。
答案 1 :(得分:1)
您应该通过以下方式检查您的型号是否是最新的:
python manage.py makemigrations
python manage.py migrate
答案 2 :(得分:1)
迁移模型以反映更改。
python manage.py makemigrations
python manage.py migrate
答案 3 :(得分:0)
迁移后,在settings.py中查看已安装的应用程序,您在应用程序名称后输入了逗号 已安装的应用程序: [ 博客, ] 由于该逗号,它无法检测到您的应用名称。