简体   繁体   中英

Django Redirecting to a created view (pk)

I am trying to redirect to a created post after filling up the form. I tried following Django redirect to created post after form but I encountered this error

TypeError at /inquiry/
inquiry() missing 1 required positional argument: 'pk'
def inquiry(request,pk):

    form = RescueeForm()

    if request.method == 'POST':
        form = RescueeForm(request.POST)
        if form.is_valid():
            form.save()
        return redirect ('rescuee', form.pk)

    context = {
        'form' : form
    }

    return render(request, "inquiry_page.html", context)

urls.py

urlpatterns = [
    path('', views.index, name='index'),
    path('inquiry/', views.inquiry, name='inquiry'),
    path('rescuee/<int:pk>', views.rescueeview, name='rescuee'),
]

Edit: added comma

In the snippet you posted, it looks like there's a comma missing in the call to redirect . Also, as pointed out by others, you're passing a pk argument to inquiry that you don't use, which is causing the error. Once you've fixed this, you pointed out that there's an attribute error for form.pk , which just means that form doesn't have an attribute called pk . Perhaps you meant form.id ?

Try

def inquiry(request):

    form = RescueeForm()

    if request.method == 'POST':
        form = RescueeForm(request.POST)
        if form.is_valid():
            form.save()
        return redirect('rescuee', form.id)

    context = {
        'form' : form
    }

    return render(request, "inquiry_page.html", context)

Alternatively, if you require the pk of the model created by the form, you could try:

def inquiry(request):

    form = RescueeForm()

    if request.method == 'POST':
        form = RescueeForm(request.POST)
        if form.is_valid():
            rescuee_instance = form.save()
        return redirect('rescuee', rescuee_instance.pk)

    context = {
        'form' : form
    }

    return render(request, "inquiry_page.html", context)

I see you forgot a , between rescuee and form.pk : return redirect ('rescuee', form.pk)

Your inquiry view takes in an argument pk . In your urls.py , your path for inquiry does not take the pk argument. Change your urls.py like this:

 urlpatterns = [
    path('', views.index, name='index'),
    path('inquiry/<int:pk>', views.inquiry, name='inquiry'), # Inquiry view takes 'pk' argument
    path('rescuee/<int:pk>', views.rescueeview, name='rescuee'),
]

Why you are using pk in inquiry view, when your url don't accept any value for pk.

I think this is the solution as you didn't use the pk in views code. Try it:

def inquiry(request):
    form = RescueeForm()
    #other codes

Note: After getting your reply, I am assuming there is a problem in form validation. I will suggest you to try this code :

 # views.py def inquiry(request): form = RescueeForm() if request.method == 'POST': form = RescueeForm(request.POST) if form.is_valid(): form.save() return redirect ('rescuee', form.pk) else: print("validation error!") context = { 'form' : form } return render(request, "inquiry_page.html", context)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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