我一直在试用Django 1.8.1。到目前为止,在我测试它之前它一直很好。当我去Django服务文件的地址(localhost:8000
)时,我收到错误:
AttributeError at /:
'str' object has no attribute 'copy'
我在Django中有一个名为fb_auth
的应用程序。这是目录树
/path/to/project/directory
|
---.gitignore
|
---project_foo
|
---project_foo
---fb_auth
---templates
---<stuff>
在追溯中,我可以看到这个突出显示的行,这是我的观点。
return render(request, 'login.html')
这是我的views.py
:
from django.shortcuts import render_to_response, redirect, render
from django.contrib.auth import logout as auth_logout
from django.contrib.auth.decorators import login_required
def login(request):
return render(request, 'login.html')
@login_required(login_url='/')
def vote(request):
return render_to_response('vote.html')
def logout(request):
auth_logout(request)
return redirect('/')
这是我的urls.py
(如果有帮助的话):
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url('', include('social.apps.django_app.urls', namespace='social')),
url(r'^$', 'fb_auth.views.login'),
url(r'^vote/$', 'fb_auth.views.vote'),
url(r'^logout/$', 'fb_auth.views.logout'),
]
这是我的login.html
:
{% if user and not user.is_anonymous %}
<a>Hello, {{ user.get_full_name }}!</a>
<br>
<a href="/logout">Logout</a>
{% else %}
<a href="{% url 'social:begin' 'facebook' %}?next={{ request.path }}">Login with Facebook</a>
{% endif %}
这是我vote.html
(暂时):
<h1>Hello vote test</h1>
<p>
<a href="/logout">Logout</a>
</p>
这里有追溯:
File "/home/seanballais/Code/Projects/SAElections/venv/local/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/seanballais/Code/Projects/SAElections/SAElections/fb_auth/views.py" in login
6. return render(request, 'login.html')
File "/home/seanballais/Code/Projects/SAElections/venv/local/lib/python3.4/site-packages/django/shortcuts.py" in render
67. template_name, context, request=request, using=using)
File "/home/seanballais/Code/Projects/SAElections/venv/local/lib/python3.4/site-packages/django/template/loader.py" in render_to_string
98. template = get_template(template_name, using=using)
File "/home/seanballais/Code/Projects/SAElections/venv/local/lib/python3.4/site-packages/django/template/loader.py" in get_template
29. engines = _engine_list(using)
File "/home/seanballais/Code/Projects/SAElections/venv/local/lib/python3.4/site-packages/django/template/loader.py" in _engine_list
144. return engines.all() if using is None else [engines[using]]
File "/home/seanballais/Code/Projects/SAElections/venv/local/lib/python3.4/site-packages/django/template/utils.py" in all
108. return [self[alias] for alias in self]
File "/home/seanballais/Code/Projects/SAElections/venv/local/lib/python3.4/site-packages/django/template/utils.py" in __iter__
105. return iter(self.templates)
File "/home/seanballais/Code/Projects/SAElections/venv/local/lib/python3.4/site-packages/django/utils/functional.py" in __get__
60. res = instance.__dict__[self.name] = self.func(instance)
File "/home/seanballais/Code/Projects/SAElections/venv/local/lib/python3.4/site-packages/django/template/utils.py" in templates
54. tpl = tpl.copy()
Exception Type: AttributeError at /
Exception Value: 'str' object has no attribute 'copy'
编辑:在我的urls.py
和追溯中添加。
我希望这会有所帮助。任何帮助将不胜感激。谢谢! :)
答案 0 :(得分:7)
您应该确保设置上的TEMPLATES是一个列表。
引用django文档:
TEMPLATES
Django 1.8中的新功能。 默认:: [](空列表)
包含与Django一起使用的所有模板引擎的设置的列表。列表中的每个项目都是包含单个引擎选项的字典。
以下面的代码为例或指南。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '<enter here your custom templates directory')], # For those who wants to have a custom place for templates in their Django apps/projects.
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
编辑:添加了一个示例,以防万一。