-4

The redirect function is not working properly.It is showing the error

NoReverseMatch at /post/new/ Reverse for 'post_detail' with arguments '()' and keyword arguments '{'pk': 9}' not found. 0 pattern(s) tried: []

this is my views.py

from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.models import User
from django.views.generic import View
from django.utils import timezone
from django.contrib.auth import ( 
    authenticate, 
    get_user_model, 
    login, 
    logout,
    )
from .forms import UserLoginForm, UserRegisterForm, UserForm, PostForm
from .models import Post

def login_view(request):
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']
        u = User.objects.get(username=username)
        posts = objects.for_user(request.user)
        if username and password:
            user = authenticate(username= username, password= password)
            if not user:
                return render(request, 'post/login.html',{'error_message': 'Incorrect username or password'})
        if not user.is_active:
            return render(request, 'post/login.html', {'error_message': 'Your account has been disabled'})
        if user.is_active:
            login(request, user)
            return render(request, 'post/home.html', {'u' : u, 'posts': posts})
return render(request, 'post/login.html')

class register_view(View):
    form_class = UserRegisterForm
    template_name = 'post/register.html'

    def get(self, request):
        form = self.form_class(None)
        return render(request, self.template_name, {'form': form}) 

    def post(self, request):
        form = self.form_class(request.POST)   

        if form.is_valid():
            user = form.save(commit=False)
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user.set_password(password)
            user.save()

            new_user = authenticate(username= username, password=     password)

            if new_user is not None:

                if user.is_active:        
                    login(request, new_user)
                    return redirect('post/home.html')

        return render(request, self.template_name, {'form' : form})

def logout_view(request):
    logout(request)
    form = UserForm(request.POST or None)

    return render(request, 'post/login.html', {'form' : form})

def post_list(request):
    posts =     Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')

    return render(request, 'post/post_list.html', {'posts': posts})


def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)

    return render(request, 'post/post_detail.html', {'post': post})

def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm()

     return render(request, 'post/post_edit.html', {'form': form})
kuro
  • 3,214
  • 3
  • 15
  • 31
Sanjana
  • 63
  • 2
  • 11
  • [This answer](http://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) might be useful. It's impossible to help further, because you haven't shown your URL patterns. – Alasdair May 10 '17 at 10:39

1 Answers1

0

Use HttpResponseRedirect instead of redirect function,

from django.http import HttpResponseRedirect

return HttpResponseRedirect(reverse('post_detail', kwargs={'pk':pk}))

Maybe that should work. You cant pass arguments using redirect function. It is used for just redirection.If you want to pass arguments along with the redirection, HttpResponseRedirect is the best way.

zaidfazil
  • 9,017
  • 2
  • 24
  • 47
  • I did the way. But got another error saying NameError at /post/new/ name 'reverse' is not defined – Sanjana May 10 '17 at 13:06
  • corrected it. but got another error saying NoReverseMatch at /post/new/ Reverse for 'post_detail' with arguments '()' and keyword arguments '{'pk': 12}' not found. 0 pattern(s) tried: [] – Sanjana May 10 '17 at 13:14
  • Can you post your​ urls.py.. Also if you are using namespace in the "project/urls.py" then try changing the url in the reverse of HttpResponseRedirect function to "your_namespace:url_name". Maybe it should do the trick.. – zaidfazil May 10 '17 at 13:36
  • corrected it. I used return redirect(reverse('post_detail' , kwargs={'pk':pk})). Thank you for helping – Sanjana May 11 '17 at 05:42
  • Glad I could be of some help... Could you do me a favour and give me an upvote? – zaidfazil May 11 '17 at 06:15