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

时间:2022-02-01 13:19:33

I'd like to point out that this is my first Django project and I don't have much other programming experience, so my error might be simple. Still, I spent a few days trying to fix it with no progress. It's also my first question here, so I hope I'm doing everything right.

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

The following is from the template of my form page. The form is displaying alright. It's just not causing the page to reload when the "submit" button is clicked and it's definitely not submitting a POST request.

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

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

models.py:

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:

forms.py:

class AddBookForm(forms.ModelForm):

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

views.py:

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})

There is a progress bar at the top of the page, which shows up whenever a page is loaded. It still shows up when I try to submit information for a new book, but PyCharm indicates that the page hasn't been reloaded and no post request has happened. There's no entry in the database as well.

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

By the way, that happens on all my other forms in the project as well. Seems like a rookie mistake, but I couldn't find a similar discussion.

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

Thanks!

谢谢!


UPDATE

UPDATE

I'm making progress, thanks to your input, but still I'm getting an error. This is what I get when I try to submit information for a new book:

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

[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

1 个解决方案

#1


1  

I don't know what you mean by "definitely not submitting a POST request", nor what any of that has to do with PyCharm.

我不知道你的意思是“绝对不提交POST请求”,也不知道这与PyCharm有什么关系。

There are two issues with your view: you're not redirecting after a valid POST, and you're not redisplaying errors when the post is invalid. To fix:

您的视图有两个问题:在有效的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  

I don't know what you mean by "definitely not submitting a POST request", nor what any of that has to do with PyCharm.

我不知道你的意思是“绝对不提交POST请求”,也不知道这与PyCharm有什么关系。

There are two issues with your view: you're not redirecting after a valid POST, and you're not redisplaying errors when the post is invalid. To fix:

您的视图有两个问题:在有效的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})