简体   繁体   English

Django 重定向到表单后创建的帖子

[英]Django redirect to created post after form

I want to redirect to the post i actually created.我想重定向到我实际创建的帖子。 So i have form, that added a post to the website, and after submit form i would like to redirect to this post.所以我有表单,在网站上添加了一个帖子,提交表单后我想重定向到这个帖子。 This is my urls这是我的网址

urlpatterns = [
    path('', views.home, name='home'),
    path('detail/<int:pk>/', views.detail, name='detail'),
    path('form/', views.formularz, name='formularz'),]

and my views:和我的看法:

def formularz(request):
    form = NewJobForm(request.POST)  
    if form.is_valid():
        firma = form.save(commit=False)
        firma.save() 
        return redirect('search:home')
    else:
        firma = NewJobForm() 
    context = {
            'form': form,

    }

    return render(request, 'search/home-form.html', context)

I understand how redirect is working, but have no idea how to redirect to the int:pk page我了解重定向的工作原理,但不知道如何重定向到int:pk页面

You can do it like this.你可以这样做。

 if form.is_valid():
      firma = form.save()
      return redirect('detail', firma.pk)
return redirect('/formatted/url')

will redirect you to the url you specify.会将您重定向到您指定的 url。 I am not totally sure which URL you want to redirect to but adding that code will redirect your user to that specific URL.我不完全确定您要重定向到哪个 URL,但添加该代码会将您的用户重定向到特定的 URL。 You can also pass in whatever variable you wanted to as you format your URL string.您还可以在格式化 URL 字符串时传入您想要的任何变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM