Hi Everyone!
I am new to learning Django. I created a modelform and rendering it on browser to send data from front-end to back end but all I am seeing is just save button on browser not form’s input fields. Here is my code:
from django.db import models
class Reservation(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
days = models.IntegerField()
room_no = models.CharField(max_length=3)
comments = models.CharField(max_length=500)
from django import forms
from .models import Reservation
class ReservationForm(forms.ModelForm):
class Meta:
fields = "__all__"
from django.shortcuts import render
from django.views import View
from django.http import HttpResponse
from .forms import Reservation
# Create your views here.
def Reservationview(request):
form = Reservation(request)
if request.method == "POST":
form = Reservation(request.POST)
if form.is_valid():
form.save()
return HttpResponse("Submission is successful")
return render(request, 'index.html', {'form': form})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hotel Reservation</title>
</head>
<body>
<form mathod="POST">
{% csrf_token %} {{form.as_p}}
<button type="submit">Save</button>
</form>
</body>
</html>
First chunk of code: models
Second chunk of code: forms
Third chunk of code: views
fourth chunk of code: index.html
Anyone please who could help me in this. I shall be very thankful to you.
Welcome @shanAweb !
Side Note: When posting code here, enclose the code between lines of three
backtick - ` characters. This means you’ll have a line of ```, then your code,
then another line of ```. This forces the forum software to keep your code
properly formatted. (I have taken the liberty of correcting your original post.
Please remember to do this in the future.)
1 Like
There are three things I see right away.
First:
You’re not identifying the model associated with your ReservationForm
. You should have a model
attribute defined in your Meta
class within the form class definition. See the example at Creating forms from models | Django documentation | Django
Second:
You don’t pass the request in to the form class. See the example at Working with forms | Django documentation | Django
Third:
This should be “method”, not “mathod”
Thanks, sir, it’s my first post here on the forum, so I had no idea. Thank you for these guidelines and for editing the post.
I will correct all these mistakes and come back to mark the solution (given by you) for the problem. Thank you
from django.urls import path
from . import views
urlpatterns = [
path("reserve", views.Reservationview)
]
from django.shortcuts import render
from django.views import View
from django.http import HttpResponse
from .forms import Reservation
# Create your views here.
def Reservationview(request):
form = Reservation()
if request.method == "POST":
form = Reservation(request.POST)
if form.is_valid():
form.save()
return HttpResponse("Submission is successful")
return render(request, 'index.html', {'form': form})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hotel Reservation</title>
</head>
<body>
<form method="POST">
{% csrf_token %} {{form.as_p}}
<button type="submit">Save</button>
</form>
</body>
</html>
from django import forms
from .models import Reservation
class ReservationForm(forms.ModelForm):
class Meta:
model = Reservation
fields = "__all__"
from django.db import models
class Reservation(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
days = models.IntegerField()
room_no = models.CharField(max_length=3)
comments = models.CharField(max_length=500)
I have corrected the above mistakes, still the issue is same
The name of the form that you are importing in your view:
is not the same as the name of the form class that you are showing here:
Thanks, sir. It’s solved with your guidance!