我有以下两种看法
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')
现在,在创建 View 中,我希望在保存数据后重定向到主页 View 。重定向工作正常,并且在主视图中执行打印语句并在终端中打印“abcde”时,它被重定向到主目录。但是,home.html 不会被渲染,它仍然在 create.html 上。此外,网址不会更改。
这是我的 urls.py 中的主页 View
url(r'^$', home, name='home'),
我做错了什么?
最佳答案
如果您通过 AJAX
发帖,则不能这样做。您必须这样做
window.location.href = '/redirecturl/'
在 AJAX success
回调中。
关于python - 从另一个 View 重定向时 Django 渲染不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43420253/