简体   繁体   English

不安全的重定向到URL

[英]Unsafe redirect to URL

i'm create a class views on django, but get this error: 我在Django上创建一个类视图,但出现此错误:

Unsafe redirect to URL with protocol 'products'

this is my code on the views: 这是我对视图的代码:

class CreateProduct(CreateView):

    model = Product
    form_class = ProductForm
    template_name = "administrador/create_product.html"
    success_url = "products:admin_productos"

this is my urls: 这是我的网址:

url(r'^create_product$', CreateProduct.as_view(), name="create_product"),

i dont know why get this error..please some one idea..!! 我不知道为什么会收到此错误..请一些想法.. !!

thanks..!! 谢谢..!!

Success_url runs get_success_url method inside of CreateView and this method should return url via reverse lookup. Success_urlCreateView内运行get_success_url方法,该方法应通过反向查找返回url。 This can be achieved by passing appropriate view to the reverse() . 这可以通过将适当的视图传递给reverse()来实现。

success_url = reverse('products:admin_productos')

Your success URL is invalid - you need to pass an actual URL, not the name of one - ie, you need to reverse that name first. 您的成功URL无效-您需要传递一个实际URL,而不是一个URL的名称-即,您需要首先将该名称reverse Change it to: 更改为:

from django.urls import reverse_lazy

class CreateProduct(CreateView):

    success_url = reverse_lazy("products:admin_productos")

See the documentation for why reverse_lazy() is appropriate here instead of reverse() : 请参阅文档,以了解为什么在这里使用reverse_lazy()而不是reverse()

It is useful for when you need to use a URL reversal before your project's URLConf is loaded. 这对于在加载项目的URLConf之前需要使用URL反向的情况很有用。 Some common cases where this function is necessary are: 需要此功能的一些常见情况是:

  • providing a reversed URL as the url attribute of a generic class-based view. 提供反向URL作为基于类的通用视图的url属性。

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

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