2016-11-24 32 views
1

我知道有很多关于django重定向的文章。但是,我没有发现像我的问题。我不知道发生了什么,但我已经阅读了很多,但还没有发现任何关于它的事情。

所以我发布我的代码。

我的项目urls.py:用django重定向错误。似乎没有任何工作

from django.conf.urls import url, include 
urlpatterns = [ 
    url(r'^backbone', include('backbone.urls', namespace='backbone')), 
] 

我的应用程序urls.py

from django.conf.urls import url 
from . import views 
urlpatterns = [ 
    url(r'^/shell$', views.shell, name='shell'), 
    url(r'^/login$', views.login, name='login'), 
] 

我的应用程序views.py

def shell(request, app='cashier'): 
    if 'user' not in request.session: 
     response = redirect('backbone:login', app=app) 
    else: 
     response = render(request, 'template.html') 
    return response 

def login(request, app): 
    context = { 
     'app': app, 
    } 
    response = render(request, 'backbone/login.html', context) 
    return response 

出现在控制台上的错误:

django.urls.exceptions.NoReverseMatch: Reverse for 'login' with arguments '()' and keyword arguments '{'app': 'app'}' not found. 1 pattern(s) tried: ['backbone/login$'] 

浏览器的错误:

NoReverseMatch at /backbone/shell 
Reverse for 'login' with arguments '()' and keyword arguments '{'app': 'app'}' not found. 1 pattern(s) tried: ['backbone/login$'] 
Request Method: GET 
Request URL: http://192.168.1.79/backbone/shell 
Django Version: 1.10.3 
Exception Type: NoReverseMatch 
Exception Value:  
Reverse for 'login' with arguments '()' and keyword arguments '{'app': 'app'}' not found. 1 pattern(s) tried: ['backbone/login$'] 
Exception Location: /usr/local/lib/python3.5/dist-packages/django/urls/resolvers.py in _reverse_with_prefix, line 392 
Python Executable: /usr/bin/python3 
Python Version: 3.5.2 
Python Path:  
['/home/ipi/workspace/RamoSP', 
'/usr/lib/python35.zip', 
'/usr/lib/python3.5', 
'/usr/lib/python3.5/plat-x86_64-linux-gnu', 
'/usr/lib/python3.5/lib-dynload', 
'/usr/local/lib/python3.5/dist-packages', 
'/usr/lib/python3/dist-packages'] 
Server time: Thu, 24 Nov 2016 19:56:47 +0000 

我不明白。我发送它需要的参数,对吧?我被困了一段时间了。

回答

4

不,您试图访问此网址:

url(r'^/login$', views.login, name='login') 

带参数,它并没有想到,因为它不希望任何参数。

看一看this post,具体是发送参数到视图的主题。它将帮助你理解为什么你不能将参数发送到这个URL中的视图。

顺便说一下,您可能需要的是将请求中的查询参数发送到此URL。在这种情况下,代替:

redirect('backbone:login', app=app) 

你必须做的:

url = '{}?app={}'.format(reverse('backbone:login'), '?item=4') 
redirect(url) 
+0

我要检查后你g大街,但它不是我想的帖子。 –

+0

哎呦,我的坏。修复了链接。 – lucasnadalutti

3

你想干什么?

/backbone/login?app=cashier 

解决方案:https://stackoverflow.com/a/3766503/6622256

/backbone/login/cashier 

解决方案:

添加参数 '应用' 在你的路线声明:

url(r'^login/(?P<app>)$', views.login, name='login'),