python - 从另一个 View 重定向时 Django 渲染不工作

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

我有以下两种看法

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/

相关文章:

python - 无法使用 beautifulsoup 3 从 json 脚本中提取所有 URL

python - 使用Webtest 和/或nose 测试cherrypy WSGI 应用程序?

django - 使用 Django 中的多个字段进行计数(不同)

python - 任何好的 pdf417 条码库用于 Python?

python - 如何在 App Engine 上使用适用于 Python 的 Google API 客户端库的 PATCH 方法?

Django 自定义成功和失败的响应

python - Django ModelForm 中的一个字段(ManyToMany)未保存到数据库

Apache 重定向到一个干净的 URL

java - Android:延迟后重定向到另一个 Activity

c++ - STDOUT 重定向后,CloseHandle 在 Studio 2010 中抛出异常