MMEEUR
MMEEUR

Reputation: 57

How to use redirect() properly with parameters?

Error Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name.

    return redirect('post_detail', slug=post.slug)

This is my comment view:

def post_detail(request, year, month, day, slug):
    post = get_object_or_404(Post, slug=slug, status='published', publish__year=year, publish__month=month, publish__day=day)
    tags = Tag.objects.all()
    tagsList = []
    for tag in post.tags.get_queryset():
        tagsList.append(tag.name)
    profile = Profile.objects.get(user=request.user)
    comments = post.comments.filter(active=True)
    new_comment = None
    if request.method == 'POST':
        comment_form = CommentForm(data=request.POST)
        if comment_form.is_valid():
            new_comment = comment_form.save(commit=False)
            new_comment.profile = profile
            new_comment.post = post
            new_comment.save()
            
            return redirect('post_detail', slug=post.slug)
    else:
        comment_form = CommentForm()
        
    post_tags_ids = post.tags.values_list('id', flat=True)
    similar_posts = Post.published.filter(tags__in=post_tags_ids).exclude(id=post.id)
    similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by('-same_tags', '-publish')[:3]
        
    return render(request, 'blog/post/detail.html', {'post': post, 'comments': comments, 'new_comment': new_comment, 'comment_form': comment_form, 'similar_posts': similar_posts, 'tagsList': tagsList, 'tags': tags})

my urls.py

from django.urls import path, register_converter
from django.urls.converters import SlugConverter
from . import views

app_name = 'blog'

class PersianSlugConvertor(SlugConverter):
    regex = '[-a-zA-Z0-9_ضصثقفغعهخحجچگکمنتالبیسشظطزژرذدپوءآ]+'
    
register_converter(PersianSlugConvertor, 'persian_slug')

urlpatterns = [
    path('', views.post_list, name='post_list'),
    path('search', views.post_search, name='post_search'),
    path('tag/<persian_slug:tag_slug>', views.post_list, name='post_list_by_tag'),
    path('<int:year>/<int:month>/<int:day>/<persian_slug:slug>', views.post_detail, name='post_detail'),
]

Template:

                         <div id="post_comments">
                              <h4>نظرات</h4>
                              <div class="comment">
                                   {% for comment in comments %}
                                   <div class="row">
                                        <figure class="col-sm-2 col-md-2"> <img width="90" height="90" class="img-circle" src="{{ comment.profile.photo.url }}" alt="عکس کاربر"> </figure>
                                        <div class="col-sm-10 col-md-10">
                                             <div class="comment_name">{{ comment.profile }}<a class="reply" href="#">&nbsp;</a> </div>
                                             <div class="comment_date"><i class="fa-time"></i>{{ comment.created }}</div>
                                             <div class="the_comment">
                                                  <p>{{ comment.body|linebreaks }}</p>
                                             </div>
                                        </div>
                                   </div>
                                   {% empty %}
                                        <h5>هیچ نظری وجود ندارد</h5>
                                   {% endfor %}
                              </div>
                         </div>
                         {% if request.user.is_authenticated %}
                         <div class="new_comment">
                              {% if new_comment %}
                              <h4>نظر شما با موفقیت ثبت شد و در حال بررسی است.</h4>                                                            
                              {% else %}
                              <h4>نظر خود را اضافه کنید</h4></br>
                              <form method="post">                               
                                   <div class="row" dir="rtl">
                                        <div class="col-sm-12 col-md-8">
                                             {{ comment_form.body|attr:"class:form-control"|attr:"type:text" }}
                                        </div>
                                   </div>
                                   {% csrf_token %} 
                                   <div class="row"><br/>
                                        <div class="col-sm-12 col-md-8"> <input type="submit" value="ارسال نظر" class="btn send btn-primary" href="#"></input> </div>
                                   </div>                                  
                              </form>
                              {% endif %}
                         </div>
                         {% else %}
                         <p>برای انتشار دیدگاه خود <a href="{% url 'account:login' %}">وارد پروفایل کاربری</a> خود شوید یا در سایت <a href="{% url 'account:register' %}">ثبت نام</a> کنید.</p>
                         {% endif %}
                    </div>

Is there anything i missed in redirect function?

Upvotes: 0

Views: 130

Answers (1)

Sunderam Dubey
Sunderam Dubey

Reputation: 8837

The post_detail view requires the four url params and you are only passing one param, so kindly pass all the params as:

return redirect('blog:post_detail',year=year, month=month, day=day, slug=post.slug)

For redirecting in the same page simply use:

return HttpResponseRedirect(request.path_info)

Upvotes: 1

Related Questions