【问题标题】:Django 1.10 redirect view not workingDjango 1.10 重定向视图不起作用
【发布时间】:2017-05-03 14:25:51
【问题描述】:

我重定向到另一个视图在 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/?

【问题讨论】:

  • tasks_view, 中有一个逗号。你应该删除它。

标签: python django python-2.x


【解决方案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')添加了逗号

现在这肯定会奏效。

【讨论】:

    【解决方案2】:

    重定向后的第一个 reverse("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())
    

    【讨论】:

      猜你喜欢
      • 2017-08-01
      • 2014-11-22
      • 2018-05-14
      • 2017-07-05
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 2017-07-13
      相关资源
      最近更新 更多