简体   繁体   English

与/ app_name / page或app_name / page混淆

[英]Confused with /app_name/page or app_name/page

I am confused that whether should I start a path starting with / or not? 我很困惑是否应该以/开头的路径开始? For example, here is one method from my views.py in django: 例如,这是我在django中的views.py中的一种方法:

@login_required
def add_page(request,category_name_slug=None):
    # check for post method
    print category_name_slug
    try:
        cat = Category.objects.get(slug = category_name_slug)
    except Exception, e:
        cat = None


    if request.method == 'POST':
        print "sjhdfkjsdhkfhjs"
        # means you submitted the form.
        form = PageForm(request.POST)
        a = form.is_valid()
        if a:
            if cat:
                page = form.save(commit = False)
                page.category = cat
                page.views = 0
                page.save()
                print "sdjhfjshk"
                return HttpResponseRedirect('/rango/category/' + category_name_slug + '/')
        else:
            print form.errors

    else:
        form = PageForm()

    context_dict = {'form' : form,'category' : cat, 'slug': category_name_slug}

    return render(request,'rango/add_page.html', context_dict)

In the render line 在渲染行中

return render(request,'rango/add_page.html', context_dict) 返回render(request,'rango / add_page.html',context_dict)

the line is not started with / . 该行不是以/开头。 But, in case of following line: 但是,在以下情况下:

return HttpResponseRedirect('/rango/category/' + category_name_slug + '/') 返回HttpResponseRedirect('/ rango / category /'+ category_name_slug +'/')

I have to start with / . 我必须从/开始。 Please explain me when should I use '/' and when not. 请解释我什么时候应该使用“ /”,什么时候不使用。

When using the render function in django, the second argument is the template name. 在django中使用render函数时,第二个参数是模板名称。

The string you specify there is used relative to the TEMPLATES settings you've defined in your settings.py. 您在此处指定的字符串将相对于您在settings.py中定义的TEMPLATES设置使用。 It tells django where the template file is located. 它告诉django模板文件在哪里。 This is why you don't start the template name with '/' in render() 这就是为什么不在render()以'/'开头模板名称的原因

Whereas, HttpResponseRedirect directly asks for the URL to redirect. HttpResponseRedirect直接要求URL进行重定向。 It is upto you whether you should add '/' to it before the string. 由您决定是否应在字符串前面添加“ /”。

When the supplied URL starts with '/', no matter where you redirect from, the user is taken to the exact path relative the the website root. 当提供的URL以“ /”开头时,无论您从何处重定向,都将把用户带到相对于网站根目录的确切路径。

For example, if the user is at http://127.0.0.1:8000/some/other/page/ and you use the following to redirect user: 例如,如果用户位于http://127.0.0.1:8000/some/other/page/,并且您使用以下命令重定向用户:

return HttpResponseRedirect('/dashboard/') 返回HttpResponseRedirect('/ dashboard /')

The user will be redirected to http://127.0.0.1:8000/dashboard/ 用户将被重定向到http://127.0.0.1:8000/dashboard/

If the URL does not start with '/', the url is just appended to the url from which user is visiting. 如果URL并非以“ /”开头,则该URL只是附加到用户正在访问的URL上。 In this case: 在这种情况下:

return HttpResponseRedirect('dashboard/') 返回HttpResponseRedirect('dashboard /')

User will be taken to http://127.0.0.1:8000/some/other/page/dashboard/ 用户将被带到http://127.0.0.1:8000/some/other/page/dashboard/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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