Django表单在调用POST方法时出错

0 投票
4 回答
1332 浏览
提问于 2025-04-18 10:37

我正在使用Django 1.6.5版本,想要创建一个简单的表单。

这是我的视图代码:

   def create(request):
       if request.method == 'POST': # If the form has been submitted...
           form = WatchForm(request.POST) # A form bound to the POST data
           if form.is_valid(): # All validation rules pass
               return HttpResponseRedirect('Watch/index.html') # Redirect after POST
           else: 
            return render(request, 'Watch/create.html', {'form': form,})
        else:
            form = WatchForm() # An unbound form

       return render(request, 'Watch/create.html', {
       'form': form,
        })

这是我的表单:

    from django import forms
    from Watch.models import Watch

    class WatchForm(forms.Form):
        idNumber=forms.CharField(max_length=30)
        brand = forms.CharField(max_length=200)
        #relisedDate =forms.DateTimeField('date published')
        referenceNumber = forms.CharField(max_length=30)
        sortDescription=forms.CharField(max_length=200)
        fullDescription=forms.CharField(max_length=600)

我还创建了一个模板,叫做create.html。

    <form action="" method="post">{% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Submit" />
    </form>

这是我的URL配置:

url(r'^create$',views.create),

所以当我在浏览器中使用这个网址 /watch/create/ 时,表单确实会显示出来,但当我提交时却收到这个错误:

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

   Using the URLconf defined in miWatch.urls, Django tried these URL patterns, in this          order:
   ^watch/ ^$ [name='index']
   ^watch/ ^create$
   ^watch/ ^(?P<watch_id>[0-9]+)/$ [name='detail']
   ^watch/ ^(?P<watch_id>[0-9]+)/results/$ [name='results']
   ^watch/ ^(?P<watch_id>[0-9]+)/vote/$ [name='vote']
   The current URL, watch/create/, didn't match any of these.

这是监视器的输出:

     [23/Jun/2014 07:53:52] "GET /watch/create HTTP/1.1" 200 1084
     [23/Jun/2014 07:54:01] "POST /watch/create/ HTTP/1.1" 404 2766
     [23/Jun/2014 07:54:08] "GET /watch/create/ HTTP/1.1" 404 2765

有没有人能告诉我为什么会这样,以及我缺少了什么呢?谢谢大家!

4 个回答

0

在你的网址后面加一个斜杠

url(r'^watch/create/$', views.create)

当表单不合法时,返回重新渲染的表单,里面包含数据和错误信息。

from django.shortcuts import render

def create(request):
    # POST
    if request.method == 'POST': # If the form has been submitted...
        form = WatchForm(request.POST) # A form bound to the POST data
    if form.is_valid(): # All validation rules pass
        # Process the data in form.cleaned_data
        # ...
        return HttpResponseRedirect('Watch/index.html') # Redirect after POST
    else:
        # Not valid, re-render form with data and error messages
        return render(request, 'Watch/create.html', {'form': form,})

    return render(request, 'Watch/create.html', {
      'form': form,
    })

在你的表单模板中修正操作:

<form action="." method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>
0

你的网址设置是在根目录下吗?如果是的话,你需要修正一下路径,改成

url(r'^watch/create$',views.create)
2

你缺少一个结尾的斜杠:

Using the URLconf defined in miWatch.urls, Django tried these URL patterns, in this          order:
   ^watch/ ^$ [name='index']
   ^watch/ ^create$ #  <-- No Trailing Slash! 

   The current URL, watch/create/, didn't match any of these.

在你的网址后面加一个斜杠:

url(r'^create/$',views.create),

你在访问这个网址时看到表单显示,是因为你没有加斜杠,而且是手动加载页面。Django这个框架喜欢网址后面有斜杠——别去跟它对着干。至于为什么在一个空的动作上提交时会加上斜杠,这听起来更像是HTML的问题,而不是Django的问题。

2

把你的表单中的 action 属性改成下面这样:

<form action="" method="post">

这样做会把表单提交到当前的网址。

撰写回答