Django URL持续时间更长

时间:2018-01-23 18:16:35

标签: python django django-views django-urls

我是django的新手,我正在研究网址重定向。在我的观点我使用HttpResponseRedirect和渲染函数来切换视图。

问题是在视图之间进行几次切换后,URL会越来越长。这是我在addcostumer和delete costumer views之间切换后Chrome上的URL

http://127.0.0.1:8000/interface/addcostumer/deletecostumer/addcostumer/deletecostumer/preview/preview/addcostumer/deletecostumer/

如何制作网址

http://127.0.0.1:8000/interface/addcostumer/

当我在addcostumer视图和

http://127.0.0.1:8000/interface/deletecostumer/

当我在deletecostumer视图中

每当我在视图之间切换时,都会一个接一个地附加它们。

class AddCostumerView(FirstPageView):

def __init__(self):
    super()
    self.main_template = "addcostumer.html"

def get(self, request):
    form = CostumerForm()
    return render(request, template_name=self.base_template,
                  context={"company_list": list(database.get_companies()),
                           "sister_page": self.main_template,
                           "form": form})

@method_decorator(csrf_protect)
def post(self, request):
    print("this line is running now")
    form = CostumerForm(request.POST)

    if request.FILES["myfile"]:
        myfile = request.FILES["myfile"]
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)


    if form.is_valid():
        data = form.cleaned_data
        #database.add_costumer(**data)

        #preview the data and preview the file

        #Add for final submission
        #messages.success(request, filename)

        return HttpResponseRedirect("preview/")  # TODO change to proper page
    return HttpResponseRedirect("firstpage.html")


class DeleteCostumerView(FirstPageView):

def __int__(self):
    super()
    self.main_template = "deletecostumer.html"

def get(self, request):
    companies = database.get_companies()

    print("delete get request")

    return render(request, template_name= self.base_template,
                  context={
                      "company_list": companies,
                      "sister_page" : "deletecostumer.html",
                      "form": DeleteForm(),
                  })

def post(self, request):

    submission = request.POST
    form = DeleteForm(submission)

    if form.is_valid():
        companyID = form.cleaned_data["company"]

        database.delete_costumer(**{"companyID":companyID})
        return HttpResponseRedirect("deleteconstumer.html")

    return HttpResponseRedirect("deleteconstumer.html")

1 个答案:

答案 0 :(得分:6)

HttpResponseRedirect("preview/")是一个相对路径,并且将始终附加到执行此代码的路径,使用类似'/preview/'的绝对路径或更好地使用Djangos reverse实用程序函数构建你的重定向。