2016-05-23 21 views
0

私はまだ簡単なもので苦労しているとは思わない!私はform.save()に複数のURLパラメータを渡して、詳細ビューにリダイレクトする必要がありますが、愚かなことにこだわっていて、時間を無駄にしています。私は詳細とリストビューだけでなく、URLも含めました。ご協力ありがとうございました。post.save()のDjangoで複数のURLパラメータを渡す

post_detailビュー

def post_detail(request, year, month, day, post): 
    posts = Post.published.all() 
    post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month, publish__day=day) 
    return render(request, 'press/post_detail.html', {'post': post, 'posts': posts}) 

私がある、働くものだけを残すようにアウトすべての私のトライアルコードを剥奪

post_newビュー - それは投稿しますが、リダイレクトされません。

def post_new(request): 
    posts = Post.published.all() 

    if request.method == 'POST': 
     form = PostForm(request.POST) 

     if form.is_valid(): 
      post = form.save(commit=False) 
      form.save() 
      form.save_m2m() 
      return render(request, 'press/post_detail.html', {'post': post}) 
    else: 
     form = PostForm() 

    return render(request, 'press/post_edit.html', {'posts': posts, 'form': form}) 

detail_viewのURLたぶん

url(r'^press/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>[-\w]+)/$', views.post_detail, name='post_detail'), 

答えて

1

redirectではなく、renderを使用する必要があります。値を渡してURLを入力する必要があります。

return redirect('post_detail', kwargs={'year': post.publish.year, 'month': post.publish.month, 'day': post.publish.day, 'post': post.slug}) 
0

あなたが使用できるだけで "from django.shortcuts import redirect" ことによってそれをインポートし、 をリダイレクトですジャンゴのライブラリーを内蔵しており、あなたはこの操作を行います。

return redirect('url-name',{'posts': posts, 'form': form}) 

これが役立つことを願っています。

+0

https://docs.djangoproject.com/en/1.9/topics/http/shortcuts/#redirect – FeFiFoFu

関連する問題