我在这里谈论Django 1.7.3:
在我的urls.py中,我有这条规则:url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}, name='logout_url'),
在我的模板中,我尝试这样做:{% url 'logout_url' %}
计算机告诉我:type object argument after ** must be a mapping, not str
并突出显示网址标记。
怎么了? :(我尝试将next_page ='/'作为参数添加到标记中,或者从url定义中删除kwargs,制作我自己的调用d.c.a.v.logout的视图,我有点迷失。
完全追溯,位匿名
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.7.3
Python Version: 2.7.8
Installed Applications:
('admin_tools',
'admin_tools.theming',
'admin_tools.dashboard',
'django.contrib.auth',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'FreqScanner')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Template error:
In template D:\company\OpenShift-App\FreqScanner\templates\FreqScanner\main.html, error at line 45
type object argument after ** must be a mapping, not str
35 : aria-expanded="false" aria-controls="navbar">
36 : <span class="sr-only">Toggle navigation</span>
37 : <span class="icon-bar"></span>
38 : <span class="icon-bar"></span>
39 : <span class="icon-bar"></span>
40 : </button>
41 : <a class="navbar-brand" href="#">App</a>
42 : </div>
43 : <div id="navbar" class="navbar-collapse collapse">
44 : {% if user.is_authenticated %}
45 : <p class="navbar-text navbar-right">Welcome, {{ user.username }} | <a href=" {% url 'logout_url' %} " class="navbar-link">Logout</a></p>
46 : {% else %}
47 : <form class="navbar-form navbar-right" role="form" method="post" action="{% url 'login_url' %}">
48 : {% csrf_token %}
49 : {% if next %}
50 : <input type="hidden" name="next" value="{{ next }}" />
51 : {% endif %}
52 : <div class="form-group">
53 : <input type="text" id="username" name="username" class="form-control" value="{{username}}" placeholder="Username">
54 : </div>
55 : <div class="form-group">
Traceback:
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:/company/OpenShift-App\FreqScanner\views.py" in dashboard
20. return render(request, 'FreqScanner/dashboard.html', {'results': results, 'last_crawl': last_crawl, 'upload_form': form})
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\shortcuts.py" in render
50. return HttpResponse(loader.render_to_string(*args, **kwargs),
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\template\loader.py" in render_to_string
178. return t.render(context_instance)
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\template\base.py" in render
148. return self._render(context)
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\template\base.py" in _render
142. return self.nodelist.render(context)
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\template\base.py" in render
844. bit = self.render_node(node, context)
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\template\debug.py" in render_node
80. return node.render(context)
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\template\loader_tags.py" in render
126. return compiled_parent._render(context)
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\template\base.py" in _render
142. return self.nodelist.render(context)
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\template\base.py" in render
844. bit = self.render_node(node, context)
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\template\debug.py" in render_node
80. return node.render(context)
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\template\defaulttags.py" in render
312. return nodelist.render(context)
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\template\base.py" in render
844. bit = self.render_node(node, context)
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\template\debug.py" in render_node
80. return node.render(context)
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\template\defaulttags.py" in render
444. url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\core\urlresolvers.py" in reverse
551. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
415. self._populate()
File "D:\Python27\VirtualEnvs\company\lib\site-packages\django\core\urlresolvers.py" in _populate
295. lookups.appendlist(name, (new_matches, p_pattern + pat, dict(defaults, **pattern.default_kwargs)))
Exception Type: TypeError at /
Exception Value: type object argument after ** must be a mapping, not str
完整的urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from FreqScanner import views
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'Company.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^$', views.dashboard, name='index'),
url(r'^crawl/$', views.crawl_and_wait, name='crawl'),
url(r'^alert/$', views.alert_and_wait, name='alert'),
url(r'^upload/$', views.upload_action, name='upload'),
url(r'^login/$', views.login_user, name='login_url'),
url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}, name='logout_url'),
url(r'^admin/', include(admin.site.urls), name='admin_page'),
url(r'^admin_tools/', include('admin_tools.urls'), 'admin_tools'),
)