【问题标题】:Django render not working when redirected from another view从另一个视图重定向时,Django 渲染不起作用
【发布时间】:2017-09-11 05:28:18
【问题描述】:

我有两种看法

def home(request):
    levels = levelData.objects.all()
    context ={
            "levels" : levels
    }
    print "abcde"
    return render(request, 'home.html', context)

还有一个

def create(request):
    if request.method == "POST":
        # doing something with data with saveData as variable of model type
        saveData.save()
        return redirect('home')
    return render(request, 'create.html')

现在,在创建视图中,我希望在保存数据后重定向到主视图。重定向工作正常,并且在执行主视图中的打印语句并在终端中打印“abcde”时将其重定向到主目录。但是,home.html 没有被渲染,它仍然在 create.html 上。此外,网址不会改变。

这是我的 urls.py 中的主页视图

url(r'^$', home, name='home'),

我做错了什么?

【问题讨论】:

  • 您是通过 ajax 发布数据吗?(js/jquery/或类似的东西)
  • 是的,通过来自 js 的 ajax
  • 感谢您的评论。我可以猜到问题并用谷歌搜索,显然添加top.location.href = "/"; 达到了目的。我无法理解这其中的原因。如果您能对此做出回应,那就太好了。

标签: python django redirect model-view-controller django-views


【解决方案1】:

如果你通过AJAX发帖,你不能这样做。你必须这样做

window.location.href = '/redirecturl/'

在 AJAX 中 success 回调。

【讨论】:

  • 因为设置了 href 位置...这就是 ajax 的工作原理...关于客户端和服务器端操作之间的区别的教程有很多很多...
猜你喜欢
  • 2016-11-02
  • 2018-10-26
  • 2020-01-01
  • 2017-05-03
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多