如何使用Django表单修复'找不到页面'

时间:2018-12-24 11:52:26

标签: python django

请帮助解决以下错误: 发布表单后出现错误

Page not found (404)
Request Method: POST
Request URL:    http://127.0.0.1:8000/beam_diagram/

使用表单保存数据后,我想重定向到另一个视图。没有test_id

,一切正常

views.py

def new_beam(request, test_id):
    if request.method == 'POST':
        print("test1")
        beam_lengthform = beam_lengthForm(request.POST)
        if beam_lengthform.is_valid():
            beam_length = beam_lengthform.cleaned_data['beam_length']
            beam = beam_lengthform.save(commit=False)
            beam.beam_length = beam_length
            beam = beam_lengthform.save()

            return redirect('new_beam:beam_diagram', beam_id=91)
    else:
        beam_lengthform = beam_lengthForm()
        context = {
            'beam_lengthform': beam_lengthform,
        }
        return render(request, 'new_beam.html', context)

urls.py

app_name = 'new_beam'

urlpatterns = [
    url(r'^(?P<test_id>\d+)$', views.new_beam, name='new_beam'),
]

1 个答案:

答案 0 :(得分:0)

您的网址正则表达式期望数字而不是字母字符,并传递http://127.0.0.1:8000/10/之类的数字-在视图中将传递给test_id。并以/这样的网址定义中的...url(r'^(?P<test_id>\d+)/$结尾