aceminer
aceminer

Reputation: 4295

Django 1.10 redirect view not working

My redirect to another view is not working in 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,')
]

I do not understand why do i always get this exception in Reverse for 'tasks_view' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

The url redirects to NoReverseMatch at /my_app/saved/. Why doesnt it redirect to the tasks view and goes to /my_app/check/ instead?

Upvotes: 0

Views: 872

Answers (2)

Farhan Ansari
Farhan Ansari

Reputation: 299

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')
]

Here you have small mistake in your code, you added coma at url(r'check/', tasks_view, name = 'tasks_view')

now this will definitely work.

Upvotes: 1

Elnur Mammadli
Elnur Mammadli

Reputation: 116

At first reverse("view_name") after redirect

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())

Upvotes: 1

Related Questions