簡體   English   中英

Django重定向

[英]Django redirect

當一些服務器端進程正在進行時,我在Django中有一個加載頁面。

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' )

我的問題是重定向到;

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

而不是

http://127.0.0.1:8000/single_design/single_output

正確的方法是什么?

編輯:問題已解決,謝謝大家。

根據要求提供

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)

正確的方法是不使用硬編碼鏈接。 使用urlresolvers

return redirect("some_view_name")

如果沒有前導斜杠,則將當前值single_output/視為相對網址,該網址會附加到當前網址/single_design/loading_page/以提供/single_design/loading_page/single_output/

您可以使用相對網址../single_output ,但我不建議這樣做。 最好返回要重定向到的URL,包括一個斜杠。

return redirect('/single_design/single_output/' )

理想情況下,您應該使用url模式的名稱,然后Django會為您反向。 既然有

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

您可以使用名稱代替網址,

return redirect('single_output')   

使用名稱single_output的優點是,您現在可以在url模式中更改URL,而不必更新視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM