如何从Django视图中调用特定的URL?

时间:2016-12-07 18:00:55

标签: python django

我想从Django Views调用特定的url模式。 调用应该在第一个IF条件内,在内部if之外。是否提供重定向的任何功能?或者重定向(map_vertical_crawl)之类的东西?代码部分 Django观点:

def add_vertical(request):

    if request.method == 'POST':

        form = VerticalForm(request.POST)

        if form.is_valid():
            form.save()

        #I WANT TO CALL URL EXACTLY AT THIS POINT, INSIDE OUTER IF.

    verticals = Verticals.objects.all()

    return  render(request, 'cfman/add_vertical.html',
                   {'form': VerticalForm(),
                    'verticals': verticals}
                  )

Django URLS:

from django.conf.urls import url
from . import views, apps

urlpatterns = [

    #Generates Login Screen
    url(r'^$', views.LoginView.as_view(), name="login"),

    #Directs to manager view level 1
    url(r'^manager/$', views.verticals_view, name="manager"),

    #Directs to manager view level 2
    url(r'^manager/(?P<verticalid>\d+)/$', views.source_list, name='source_list'),

    #Add Vertical
    url(r'^verticals/add_vertical/$', views.add_vertical, name="add_vertical"),

    #Add Source
    url(r'^sources/add_source/$', views.add_source, name="add_source"),

    #Add Crawl Type
    url(r'^crawls/add_crawl/$', views.add_crawl, name="add_crawl"),

    #Map verticals and crawl types
    url(r'^vertical_crawl/$', views.map_vertical_crawl, name="map_vertical_crawl"),


]

我想从视图中调用最后一个url模式'map_vertical_crawl'。

1 个答案:

答案 0 :(得分:1)

Django的内置redirect()接受视图名称作为参数,所以只需:

redirect('cfman:map_vertical_crawl')