0% found this document useful (0 votes)
13 views

How To Create Forms

This document provides instructions for creating a Django form by making a forms.py file, adding it to views.py, and including it in an HTML template. It also configures the project settings.

Uploaded by

samueltamiru2008
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

How To Create Forms

This document provides instructions for creating a Django form by making a forms.py file, adding it to views.py, and including it in an HTML template. It also configures the project settings.

Uploaded by

samueltamiru2008
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

How to create forms.

py

Create forms.py under application

Form.py

from django import forms


class studentform(forms.Form):
name=forms.CharField()
marks=forms.IntegerField()

under views.py

from django.shortcuts import render


from app1.forms import studentform
# Create your views here.
def std_frm_view(request):
stdfrm=studentform()
dict_frm={'stdfrm':stdfrm}
return render (request, 'app1/std.html', dict_frm)

under urls.py

from django.contrib import admin


from django.urls import path
from app1 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('stdv/',views.std_frm_view),
]

Html file

<body>

<h1>student input form</h1>


<form method="post">
{{stdfrm.as_p}}
{%csrf_token %}
<input type="submit" values="submit">
</form>
</div>
</body>
Setting.py

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.


BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES_DIR=BASE_DIR/'templates'
STATIC_DIR=BASE_DIR/'static'

# Quick-start development settings - unsuitable for production


# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!


SECRET_KEY = 'django-insecure-@y#-lwi8n!n*-9j1+ums4-xwau&o6@uh!n1s1(@di4pj#d8xch'

# SECURITY WARNING: don't run with debug turned on in production!


DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'form.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'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',
],
},
},
]

WSGI_APPLICATION = 'form.wsgi.application'

# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}

# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME':
'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME':
'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME':
'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]

# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)


# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_URL = 'static/'
STATICFILES_DIRS=[STATIC_DIR,]

# Default primary key field type


# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

You might also like