简体   繁体   中英

django - how to redirect page after save modelform

I want to redirtect page, after saving modelform. when i pushed save button, page redirecte, but no any things saved.

def channelAdd(request):
    if request.method == 'POST':
        form = ChannelForm(request.POST)
        if form.is_valid():
            channelid = form.cleaned_data['channelid']
            form.save()

            return HttpResponseRedirect(reverse('updateChannelInfo', args=[channelid]))

    else:
        form = ChannelForm()

    return render(request, 'web/channelAdd.html', {'form':form})

This will get you closer to the solution. I'm not positive if you have 'updateChannelInfo' as the name in urls.py (so please double-check that). I think the complexity here is getting the correct channelId to be sent

def channelAdd(request):
    if request.method == 'POST':
        form = ChannelForm(request.POST)
        if form.is_valid():
            channelid = form.cleaned_data['channelid']
            form.save()
            return HttpResponseRedirect(reverse('updateChannelInfo', args = [self.object.id])))
    else:
        form = ChannelForm()
    return render(request, 'web/channelAdd.html', {'form':form})

If you are willing to share your urls.py and forms.py files, this would help with getting the correct names into arguments

Another way I have had success with the dynamic direct after form submission is to use

def add_channel (request):
    if request.method == 'POST':
        form = ChannelForm(request.POST)
        if form.is_valid():
            channel.save()
            return HttpResponseRedirect(reverse('channel_detail', args=[channel.id]))
    else:
        form = ChannelForm()
        return render(request, 'channel_example.html', {'form': form})

Edit your view like this,

if form.is_valid():
    form.save()
    return redirect('updateChannelInfo', channelId=self.object.id)

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