无法在Django中获取表单来创建POST请求(并保存到数据库)

时间:2015-05-12 11:13:17

标签: python django django-forms

我想指出这是我的第一个Django项目,而且我没有太多其他编程经验,所以我的错误可能很简单。尽管如此,我花了几天时间试图修复它并没有任何进展。这也是我的第一个问题,所以我希望我能做好一切。

以下内容来自我的表单页面的模板。表格显示正常。当"提交"它不会导致页面重新加载。单击按钮,它肯定不会提交POST请求。

<form role="form" action="." method="POST">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons submit='Add new book' reset="Cancel" %}{% endbuttons %}
</form>

models.py:

class Book(models.Model):
    title = models.CharField(max_length=80)
    photo = models.ImageField(upload_to='book_images', blank=True)
    author = models.CharField(max_length=150)
    category = models.CharField(max_length=50)
    product_url = models.CharField(max_length=200)
    num_of_chapters = models.IntegerField()

forms.py:

class AddBookForm(forms.ModelForm):

    class Meta:
         model = Book
         fields = ('title', 'photo', 'author', 'product_url', 'category', 'num_of_chapters')

views.py:

def add_new_book(request):
    if request.method == 'POST':
        form = forms.AddBookForm(data=request.POST)
        if form.is_valid():
            form.save()

    form = AddBookForm()
    return render(request, 'main/add_new_book.html', {'form': form})

页面顶部有一个进度条,只要加载页面就会显示。当我尝试提交新书的信息时,它仍会显示,但PyCharm表示页面没有被重新加载,也没有发布任何帖子请求。数据库中也没有条目。

顺便说一句,这也发生在我项目中的所有其他表单上。看起来像一个新手的错误,但我找不到类似的讨论。

谢谢!

更新

由于您的意见,我正在取得进展,但我仍然遇到错误。这是我在尝试提交新书信息时得到的结果:

[12/May/2015 16:12:54]"GET /main/add-new/?csrfmiddlewaretoken=FGaEtjH6ntyLSNm923OuYxBoOm9BQ0KN&title=Spiritual+Seduction&author=Jerry+Stocking&product_url=http%3A%2F%2Fwww.amazon.com%2FSpiritual-Seduction-Jerry-Stocking%2Fdp%2F0962959383&category=Spirituality&num_of_chapters=26 HTTP/1.1" 200 24399
[12/May/2015 16:12:54]"GET /static/assets/components/forms_elements_bootstrap-select/bootstrap-select.init.js?v=v2.0.0-rc1&sv=v0.0.1.2&1431436374743 HTTP/1.1" 200 185
[12/May/2015 16:12:54]"GET /static/assets/components/ui_sliders_jqueryui/jqueryui-sliders.init.js?v=v2.0.0-rc1&sv=v0.0.1.2&1431436374743 HTTP/1.1" 200 5282
[12/May/2015 16:12:54]"GET /static/assets/components/core/core.init.js?v=v2.0.0-rc1&1431436374743 HTTP/1.1" 200 7688
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 54178)
----------------------------------------
[12/May/2015 16:12:55]"GET /main/add-new/assets/images/logo/app-logo-style-default.png HTTP/1.1" 200 33624
Traceback (most recent call last):
  File "C:\Python27\lib\wsgiref\handlers.py", line 86, in run
self.finish_response()
  File "C:\Python27\lib\wsgiref\handlers.py", line 128, in finish_response
    self.write(data)
  File "C:\Python27\lib\wsgiref\handlers.py", line 217, in write
self._write(data)
  File "C:\Python27\lib\socket.py", line 324, in write
self.flush()
  File "C:\Python27\lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine
 [12/May/2015 16:12:55]"GET /main/add-new/assets/images/logo/app-logo-style-default.png HTTP/1.1" 500 59
Traceback (most recent call last):
  File "C:\Python27\lib\SocketServer.py", line 599, in process_request_thread
self.finish_request(request, client_address)
  File "C:\Python27\lib\SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
  File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 102, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "C:\Python27\lib\SocketServer.py", line 657, in __init__
self.finish()
  File "C:\Python27\lib\SocketServer.py", line 716, in finish
self.wfile.close()
  File "C:\Python27\lib\socket.py", line 279, in close
self.flush()
  File "C:\Python27\lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine
[12/May/2015 16:12:55]"GET /main/add-new/assets/images/people/100/15.jpg HTTP/1.1" 200 33625
Traceback (most recent call last):
  File "C:\Python27\lib\wsgiref\handlers.py", line 86, in run
self.finish_response()
  File "C:\Python27\lib\wsgiref\handlers.py", line 128, in finish_response
    self.write(data)
  File "C:\Python27\lib\wsgiref\handlers.py", line 217, in write
    self._write(data)
  File "C:\Python27\lib\socket.py", line 324, in write
[12/May/2015 16:12:55]"GET /main/add-new/assets/images/people/100/16.jpg HTTP/1.1" 200 33624
    self.flush()
  File "C:\Python27\lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine
[12/May/2015 16:12:55]"GET /main/add-new/assets/images/people/100/15.jpg HTTP/1.1" 500 59
Traceback (most recent call last):
  File "C:\Python27\lib\SocketServer.py", line 599, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Python27\lib\SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
  File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 102, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "C:\Python27\lib\SocketServer.py", line 657, in __init__
self.finish()
  File "C:\Python27\lib\SocketServer.py", line 716, in finish
self.wfile.close()
  File "C:\Python27\lib\socket.py", line 279, in close
self.flush()
  File "C:\Python27\lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 54179)
----------------------------------------
[12/May/2015 16:12:55]"GET /main/add-new/assets/images/people/100/17.jpg HTTP/1.1" 200 33625

2 个答案:

答案 0 :(得分:1)

我不知道你的意思&#34;绝对不提交POST请求&#34;,也不知道任何与PyCharm有什么关系。

您的观点存在两个问题:您在有效的POST后没有重定向,并且在帖子无效时您没有重新显示错误。修复:

def add_new_book(request):
    if request.method == 'POST':
        form = forms.AddBookForm(data=request.POST)
        if form.is_valid():
            form.save()
            return redirect('/')  # or wherever you want

    else:
        form = forms.AddBookForm()

    return render(request, 'main/add_new_book.html', {'form': form})

答案 1 :(得分:1)

是真的:

def add_new_book(request):
 if request.method == 'POST':
   form = AddBookForm(request.POST)
     if form.is_valid():
        form.save()
 form = AddBookForm(request.POST)
 return render(request, 'main/add_new_book.html', {'form': form})