#Beginnerlevel Form sending get instead of post request

41 views
Skip to first unread message

chaitanya sakoji

unread,
Apr 22, 2021, 11:20:26 AM4/22/21
to Django users

Previously this code was sending post request. Suddenly after restarting server, it started sending get. If I take some working post code for form from internet and then go on trying and erasing then my code starts sending post request. But again after some time when I put my original code, this will start sending get request instead of post request.

user_registration_form.html
<form method="post">
{% csrf_token %}
{{ form }}
</form>
<input type="submit" value="Register">

 forms.py
from django.forms import ModelForm
from django import forms
from .models import StudentData


class StudentRegister(ModelForm):

class Meta:
model = StudentData
fields = "__all__"
widgets = {'password': forms.PasswordInput(),
'rpassword': forms.PasswordInput()}

def clean(self):
cleaned_data = super().clean()
password, rpassword = cleaned_data.get('password'), cleaned_data.get('rpassword')
if password != rpassword:
error_msg = "Both passwords must match"
self.add_error('rpassword', error_msg)


views.py
from django.shortcuts import render
from .forms import StudentRegister
from django.contrib import messages


def login_register(request):
return render(request, 'users/login_register.html')


def guest_register(request):
return render(request, 'users/guest_register.html')


def student_data(request):
if request.method == 'POST':
print('post')
form = StudentRegister(request.POST)
if form.is_valid():
print('valid form')
form.save()
messages.success(request, f"You are registered successfully, {form.cleaned_data.get('name')}")
return render(request, "<h3>Registration done</h3>")
else:
print('get')
form = StudentRegister()
return render(request, 'users/user_registration_form.html', {'form': form})

model.py
from django.db import models
from django.core.validators import MaxLengthValidator


class StudentData(models.Model):
name = models.CharField(max_length=100)
room_no = models.CharField(max_length=4)
email = models.EmailField()
mobile = models.CharField(max_length=10)
password = models.CharField(max_length=20)
rpassword = models.CharField(max_length=20)

def __str__(self):
return f'{self.name} data'

Thanks,
Chaitanya

Kasper Laudrup

unread,
Apr 22, 2021, 3:47:59 PM4/22/21
to django...@googlegroups.com
On 22/04/2021 17.14, chaitanya sakoji wrote:
> Previously this code was sending post request. Suddenly after restarting
> server, it started sending get.

I *think* I might understand what you mean.

The code you posted doesn't send anything. It handles POST request sent
from your browser. If you load a page with your browser normally, it
will send a GET request.

My guess is, that you've sent a POST request to the page (which works
fine) and then you load the same page in your browser sending a GET request.

Could that explain it.

Just to be clear. Your code will not start changing behavior without you
doing anything (self modifying code is indeed a thing, but that's not
relevant here at all).

Kind regards,

Kasper Laudrup

OpenPGP_signature

Chaitanya Sakoji

unread,
Apr 22, 2021, 9:58:05 PM4/22/21
to django...@googlegroups.com
Thanks Kasper for your reply.
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/x6_U7_hCeOY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4c96bea3-db71-ca42-7158-9e1f3a9bc6ea%40stacktrace.dk.

Agoua David

unread,
Apr 23, 2021, 3:39:04 AM4/23/21
to django...@googlegroups.com
I think it's because tour submit button IS not in the form

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/BN6PR08MB23882B77FEB28076EDA82B1A97459%40BN6PR08MB2388.namprd08.prod.outlook.com.

Chaitanya Sakoji

unread,
Apr 23, 2021, 4:32:53 AM4/23/21
to django...@googlegroups.com

Thank Agoua. It worked after update.

Reply all
Reply to author
Forward
0 new messages