Django 1.10重定向视图不起作用

时间:2016-12-18 10:04:03

标签: python django python-2.x

我的重定向到另一个视图在django 1.10中不起作用

Views.py

from django.shortcuts import render, redirect

# Create your views here.
from forms import DocumentForm
from models import Document
from django.apps import apps
myapp = apps.get_app_config('django_celery_results')
myapp.models.TASK_MODEL = myapp.models['taskresult']

def tasks_view(request):
    tasks = TASK_MODEL.objects.all()
    return render(request, 'saved.html', {'tasks': tasks})

def SaveDocument(request):
    saved = False
    if request.method == "POST":
        #Get the posted form
        MyDocumentForm = DocumentForm(request.POST, request.FILES)

        if MyDocumentForm.is_valid():
            print 'It enters here'
            document = Document()
            document.name = MyDocumentForm.cleaned_data["name"]
            document.doc = request.FILES["document"]
            print document.doc
            print document.name
            print 'request type', request.method
            document.save()
            saved = True
            return redirect('tasks_view')
        else:
            print 'Fails'
    else:
        MyDocumentForm = DocumentForm()

    return render(request, 'saved.html', locals())

urls.py

from django.conf.urls import url
from django.views.generic import TemplateView
from core.views import *
urlpatterns = [
url(r'profile/',TemplateView.as_view(
      template_name = 'profile.html')), 
url(r'saved/', SaveDocument, name = 'SaveDocument'),
url(r'check/', tasks_view, name = 'tasks_view,')
]

我不明白为什么我总是在Reverse for 'tasks_view' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

中得到这个例外

网址重定向到NoReverseMatch at /my_app/saved/。为什么它没有重定向到任务视图并转到/ my_app / check /而不是?

2 个答案:

答案 0 :(得分:1)

重定向后首先反转(" view_name")

from django.core.urlresolvers import reverse
def SaveDocument(request):
    saved = False
    if request.method == "POST":
        #Get the posted form
        MyDocumentForm = DocumentForm(request.POST, request.FILES)

        if MyDocumentForm.is_valid():
            print 'It enters here'
            document = Document()
            document.name = MyDocumentForm.cleaned_data["name"]
            document.doc = request.FILES["document"]
            print document.doc
            print document.name
            print 'request type', request.method
            document.save()
            saved = True
            return redirect(reverse('tasks_view'))
        else:
            print 'Fails'
    else:
        MyDocumentForm = DocumentForm()

    return render(request, 'saved.html', locals())

答案 1 :(得分:1)

from django.conf.urls import url
from django.views.generic import TemplateView
from core.views import *
urlpatterns = [
url(r'profile/',TemplateView.as_view(
      template_name = 'profile.html')), 
url(r'saved/', SaveDocument, name = 'SaveDocument'),
url(r'check/', tasks_view, name = 'tasks_view')
]

您的代码中存在小错误,您在url(r'check/', tasks_view, name = 'tasks_view')

添加了昏迷

现在这肯定会奏效。