TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'smo_temp')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
signup_student.html
{% extends 'temp_basic.html' %}
{% block content %}
{{msg}}
<form action='signup_student_output' method=POST>
{% csrf_token %}
name :<input type=text name=name><br>
class :<input type=text name=class><br>
Mark :<input type=text name=mark><br>
Gender :<input type=text name=gender><br>
<input type=submit value=Submit>
</form>
{% endblock %}
temp_basic.html
<html>
<head>
<title>This is basic template file </title>
</head>
<body>
{% block content %}
{% endblock %}
<br><br>
<center>Displayed by using template file temp_basic.html</center>
</body>
</html>
Here four inputs are taken from user. To keep it simple we are not validating any input data here and expecting integer to be entered by user in mark field.
from django.urls import path
from . import views
# Create your views here.
urlpatterns=[
path('',views.index,name='index'),
path('student/signup_student',views.signup_student,name='signup_student'),
path('student/signup_student_output',views.signup_student_output,name='signup_student_output2')
]
from django.db import models
# Create your models here.
class student(models.Model):
name =models.CharField(max_length=100)
cl=models.CharField(max_length=10,db_column='class')
mark =models.IntegerField()
gender =models.CharField(max_length=6)
class Meta:
db_table='student'
Here watch the way we used one column name class, because class is a reserved keyword in Python.
class Meta:
db_table='student'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'database_name',
'USER' : 'userid_here',
'PASSWORD' :'password_here',
'HOST' : 'localhost'
}
}
python manage.py makemigrations
You need to apply this.
python manage.py migrate
You will get output like this .path('student/signup_student',views.signup_student,name='signup_student')
Here signup_student() is declared inside views.py file ( check the code below. With this we display the html form inside our signup_student.html file.
<form action='signup_student_output' method=POST>
Inside our urls.py ( check above for code of urls.py ) we have this line.
path('student/signup_student_output',views.signup_student_output,name='signup_student_output2')
Here signup_student_output() is declared inside views.py ( check the code for views.py below ). from django.shortcuts import render
from django.http import HttpResponse
from .models import student
# Create your views here.
def index(request):
return render(request,'form.html')
def signup_student(request):
return render(request,'signup_student.html',{'msg':'Student Signup'})
def signup_student_output(request):
en=student(name=request.POST.get('name'),cl=request.POST.get('class'),
mark=request.POST.get('mark'),gender=request.POST.get('gender'))
en.save()
str1="Data inserted to student table"
return render(request,'signup_student.html',{'msg':str1})
def signup_student_output(request):
en=student(name=request.POST.get('name'),cl=request.POST.get('class'),
mark=request.POST.get('mark'),gender=request.POST.get('gender'))
en.save()
str1="Data inserted to student table with id:" + str(en.id)
return render(request,'signup_student.html',{'msg':str1})
Watch the id displayed after adding the record to our student table. 22-01-2023 | |
Hi, I've wll executed all what you taught in your tutorial, I am having some errors that tell me my db_table = 'user' name is not defined. What to do ? |