简体   繁体   中英

Django redirect

I have a loading page in Django while some server side processes are ongoing the view is;

def loading_page( request ):

    testname = request.session['testname']

    done_file = filepath_to_design_dir( testname + ".done" )


    if os.path.exists( done_file ):

        request.session["job_stat"] = "job_done"        
        return redirect( "single_output/")


    else:

        return render( request, 'single_design/loading.html' )

My problem is that the redirect goes to;

http://127.0.0.1:8000/single_design/loading_page/single_output/

Rather than

http://127.0.0.1:8000/single_design/single_output

What is the correct way to do this???

EDIT : issue resolved, thanks guys.

Urls as requested

from django.conf.urls import url , include
from . import views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [

    url(r'^$', views.get_single_input, name='single_design_input'),
    url(r'^single_output/$', views.single_output, name='single_output'),
    url(r'^loading_page/$', views.loading_page, name='loading_page'), 
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

The correct way is not to use hardcoded link. Use urlresolvers .

return redirect("some_view_name")

Without the leading slash, the current value single_output/ is treated as a relative url, which is appended to the current url /single_design/loading_page/ to give /single_design/loading_page/single_output/ .

You could use the relative url ../single_output , but I wouldn't recommend it. It would be better to return the url you want to redirect to, including a leading slash.

return redirect('/single_design/single_output/' )

Ideally, you should use the name of the url pattern, then Django will reverse it for you. Since you have,

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

you can use the name instead of the url,

return redirect('single_output')   

The advantage of using the name single_output , is that you can now change the URL in your url patterns, without having to update the view.

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