Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
Delete View (FBV) not working with a POST request but works only with a GET reuqest
Post Body

I am encountering a really weird error which I am unable to troubleshoot and fix. I have created a Delete View which is a function based view. For some strange reason, it works only with a GET request. With a POST request, I am getting the error as shown below in command prompt. I have tried looking for what the ConnectionResetError means but just not able to figure out what the source of error is. All other CRUD views work just fine. It is only this delete view that is giving me the problem.

I am using the latest stable versions of both Django (3.1.7) and Python (3.9.2) on Windows 10 (x64). Any help on how to fix this error would be greatly appreciated.

Error Message:

Exception occurred during processing of request from ('127.0.0.1', 55298)
Traceback (most recent call last):
  File "c:\python\python39\lib\socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
  File "c:\python\python39\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "c:\python\python39\lib\socketserver.py", line 720, in __init__
    self.handle()
  File "D:\Programs\Django\phonebook\venv\lib\site-packages\django\core\servers\basehttp.py", line 174, in handle
    self.handle_one_request()
  File "D:\Programs\Django\phonebook\venv\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "c:\python\python39\lib\socket.py", line 704, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

models.py:

class Contact(models.Model):
    added_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, null=True)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100, blank=True, default='')
    slug = models.SlugField(unique=True, max_length=255, blank=True, default='')
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = 'Contact'
        verbose_name_plural = 'Contacts'
        ordering = ['first_name', 'last_name', 'pk']

    def get_absolute_url(self):
        return reverse('contacts-detail', kwargs={'slug': self.slug})

views.py:

def contacts_delete(request, slug):
    instance = get_object_or_404(Contact, slug=slug, added_by=request.user)
    if request.method == 'POST':
        messages.success(request, f"The contact was deleted successfully.")
        instance.delete()
        return redirect('contacts-list')
    else:
        messages.error(request, "WARNING: Something went wrong. Delete operation aborted.")
    context = {
        'title': 'Delete',
        'object': instance,
    }
    return render(request, 'contacts/contacts_delete.html', context)

urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.contacts_home, name='contacts-home'),
    path('contacts/', views.contacts_list, name='contacts-list'),
    path('contacts/create/', views.contacts_create, name='contacts-create'),
    path('contacts/<slug:slug>/', views.contacts_detail, name='contacts-detail'),
    path('contacts/<slug:slug>/update/', views.contacts_update, name='contacts-update'),
    path('contacts/<slug:slug>/delete/', views.contacts_delete, name='contacts-delete'),
]

contacts_delete.html template:

{% extends 'base.html' %}

{% block content %}
    <form method="POST" enctype="multipart/form-data">
        {% csrf_token %}
        Are you sure you want to delete <span style="color:red"><strong>{{ object.first_name }}</strong></span>? This cannot be undone.

        <div class="text-center mt-2">
            <button class="btn btn-outline-success" type="submit">Delete</button>
            <a class="btn btn-outline-primary" type="submit" href="{% url 'contacts-home' %}">Home</a>
        </div>
    </form>
{% endblock content %}

Author
User Disabled
Account Strength
0%
Disabled 4 months ago
Account Age
5 years
Verified Email
Yes
Verified Flair
No
Total Karma
6,531
Link Karma
663
Comment Karma
5,737
Profile updated: 4 days ago
Posts updated: 1 year ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
3 years ago