Thursday, April 22, 2021

#Beginnerlevel Form sending get instead of post request

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

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/88869687-aba7-48f3-99d1-08bfe5643821n%40googlegroups.com.

No comments:

Post a Comment