简体   繁体   中英

Django redirecting from one view to another

I'm trying to redirect to a view function from another using djando redirect().

def view1(request):
    message="hello! redirection was done!!"
    return render(request,'test.html',locals())
def view2(request):
    return redirect(view1)

but I'm getting NoReverseMatch exception. My urls.py is as shown below

from django.conf.urls import url
from django.contrib import admin
from .views import signup,auth_test,redirected,view2
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^signup/$' ,signup),
    url(r'^login/$', auth_test),
    url(r'^simple',view2)
    #url(r'^locallibrary.views.redirected/$',redirected)
]

any help will be appreciated thank you

error page

NoReverseMatch at /simple
Reverse for 'locallibrary.views.view1' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL:    http://127.0.0.1:8000/simple
Django Version: 1.10.4
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'locallibrary.views.view1' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Exception Location: C:\Users\Hp\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 392
Python Executable:  C:\Users\Hp\AppData\Local\Programs\Python\Python35-32\python.exe
Python Version: 3.5.2
Python Path:    
['C:\\Users\\Hp\\PycharmProjects\\locallibrary',
 'C:\\Program Files (x86)\\JetBrains\\PyCharm 145.597.11\\helpers\\pycharm',
 'C:\\Users\\Hp\\PycharmProjects\\locallibrary',
 'C:\\Users\\Hp\\AppData\\Local\\Programs\\Python\\Python35-32\\python35.zip',
 'C:\\Users\\Hp\\AppData\\Local\\Programs\\Python\\Python35-32\\DLLs',
 'C:\\Users\\Hp\\AppData\\Local\\Programs\\Python\\Python35-32\\lib',
 'C:\\Users\\Hp\\AppData\\Local\\Programs\\Python\\Python35-32',
 'C:\\Users\\Hp\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages']
Server time:    Sun, 5 Feb 2017 12:31:15 +0530

You have to add view1 to url patterns also:

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^signup/$' ,signup),
url(r'^login/$', auth_test),
url(r'^simple',view2),
url(r'^simple2',view1)
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM