MODULE 3- FSD-Notes
MODULE 3- FSD-Notes
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING
Chapter 6
• Activating Admin Interfaces
• Using Admin Interfaces
• Customizing Admin Interfaces - Customizing Field Labels, Custom
ModelAdmin Classes(Customizing Change Lists, Customizing Edit
Forms)
• Reasons to use Admin Interfaces -
Chapter 7
• Form Processing
• Creating Feedback forms
• Form submissions
• Custom validation
• Creating Model Forms
Chapter 8
• URLConf Ticks
• Including Other URLConfs.
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING
CHAPTER 6
Second, run python manage.py syncdb. This step will install the extra database tables that the admin
interface uses. The first time you run syncdb with 'django.contrib.auth' in INSTALLED_APPS, you’ll be
asked about creating a superuser. If you don’t do this, you’ll need to run python manage.py
createsuperuser separately to create an admin user account; otherwise you won’t be able to log in to the
admin site.
Third, add the admin site to your URLconf (in urls.py, remember). By default, the urls.py generated by
django-admin.py startproject contains commented-out code for the Django admin, and all you have to do
is uncomment it. For the record, here are the bits you need to make sure are in there:
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING
• Visit the admin site and log in with the superuser credentials you created.
• If you can't log in, ensure you’ve created a superuser by running python manage.py
createsuperuser.
• After logging in, you'll see the admin home page listing all data types available for
editing. Initially, it includes only Groups and Users.
Data Management:
• Each data type in the admin site has a change list and an edit form.
• Change List: Displays all records of a data type, similar to a SELECT * FROM <table> SQL query.
• Edit Form: Allows you to add, change, or delete individual records.
• Add Record: Click Add in the appropriate column on the admin home page to access an
• Delete Record: Click the Delete button at the bottom left of an edit form. Confirm the
deletion on the subsequent page, which may list dependent objects to be deleted as well.
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING
One can also add more built-in field validations for applying or removing certain constraints on a
particular field. null=True will make the field accept NULL values. Blank values for Django field
types such as DateTimeField or ForeignKey will be stored as NULL in the database.
CHAPTER 7
FORM PROCESSING
The behavior of a hypothetical perfect form:
• It should ask the user for some information, obviously. Accessibility and usability matter here, so
smart use of the HTML <label> element and useful contextual help are important.
• The submitted data should be subjected to extensive validation. The golden rule of Web application
security is “never trust incoming data,” so validation is essential.
• If the user has made any mistakes, the form should be redisplayed with detailed, informative error
messages. The original data should be prefilled, to save the user from having to reenter everything.
• The form should continue to redisplay until all of the fields have been correctly filled.
• When one creates a Form class, the most important part is defining the fields of the form. Each field
has custom validation logic, along with a few other hooks.
• Forms are used for taking input from the user in some manner and using that information for logical
operations on databases. For example, Registering a user by taking input such as his name, email,
password, etc.
• Django maps the fields defined in Django forms into HTML input fields. Django handles three
distinct parts of the work involved in forms:
o Preparing and restructuring data to make it ready for rendering.
o Creating HTML forms for the data.
o Receiving and processing submitted forms and data from the client.
Creating a form in Django is completely similar to creating a model, one needs to specify what fields would
exist in the form and of what type. For example, to input, a registration form one might need First Name
(CharField), Roll Number (IntegerField), and so on.
Syntax:
FORM SUBMISSIONS
Talking about forms, In HTML, a form is a collection of elements inside <form>…</form> that allow a
visitor to do things like entering text, select options, manipulate objects or controls, and so on, and then
send that information back to the server. Basically, it is a collection of data for processing it for any purpose
including saving it in the database or fetching data from the database. Django supports all types of HTML
forms and rendering data from them to a view for processing using various logical operations.
Render HTML Forms (GET & POST) in Django.
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING
CUSTOM VALIDATION
A validator is a callable that takes a value and raises a ValidationError if it doesn’t meet criteria. Validators
can be useful for re-using validation logic between different types of fields.
Django Custom Field Validation Explanation for Django Forms
Django ModelForm is a class that is used to directly convert a model into a Django form. If you’re building
a database-driven app, chances are you’ll have forms that map closely to Django models. For example, a
User Registration model and form would have the same quality and quantity of model fields and form
fields. So instead of creating a redundant code to first create a form and then map it to the model in a view.
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING
CHAPTER 8
URLConf TICKS
There’s nothing “special” about URLconfs — like anything else in Django, they’re just Python code.
The following path convertor types are available in Django
The syntax for named regular expression groups is (?Ppattern), where name is the name of the group and
pattern is some pattern to match.
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING
8. Special-Casing Views
Sometimes you’ll have a pattern in your URLconf that handles a large set of URLs, but you’ll need to
special-case one of them. In this case, take advantage of the linear way a URLconf is processed and
put the special case first.
For example, the “add an object” pages in Django’s admin site are represented by this URLconf line:
Note that int() itself raises a ValueError when you pass it a string that is not composed solely of digits,
but we’re avoiding that error in this case because the regular expression in our URLconf has ensured
that only strings containing digits are passed to the view function.
The regular expressions in this example that point to an include() do not have a $ (end-of-string match
character) but do include a trailing slash. Whenever Django encounters include(), it chops off whatever
part of the URL matched up to that point and sends the remaining string to the included URLconf for
further processing.
Prepared By,
Ranjitha J
Assistant
ProfessorDept, of
ISE
SJB Institute of Technology