调用请求POST方法时出现Django Forms错误

时间:2014-06-21 15:21:35

标签: python django django-forms

所以我使用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>

这是我的urlpatterns条目         URL(R '^创建$',views.create),

因此,当我使用此URL / 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 :(得分:2)

将表单中的action属性更改为

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

这会将表单提交到当前网址。

答案 1 :(得分: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),

当您获取URL时看到表单显示的原因是您没有添加斜杠,而是手动加载页面。 Django喜欢拖尾 - 不要那么打。不确定为什么发布到空操作会附加斜杠,但这听起来像是HTML而不是Django问题。

答案 2 :(得分:0)

您的网址设置是否在根级别定义?如果是这样,您需要修复

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

答案 3 :(得分: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>