Module3 FS
Module3 FS
Module-3
Django Admin Interfaces and Model Forms
The Django admin application can use your models to automatically build a site area that you
can use to create, view, update, and delete records. This can save a lot of time during
development, making it very easy to test your models and get a feel for whether you have
the right data. The admin application can also be useful for managing data in production,
depending on the type of website. The Django project recommends it only for internal data
management (i.e. just for use by admins, or people internal to your organization), as the model-
centric approach is not necessarily the best possible interface for all users, and exposes a lot of
unnecessary detail about the models.
The Django admin site is entirely optional, because only certain types of sites need this func
tionality. That means you’ll need to take a few steps to activate it in your project.
these three packages. (If you’re following along with our ongoing mysite project, note
them now.)
auth.middleware.AuthenticationMiddleware'
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; oth
erwise you won’t be able to log in to the admin site. (Potential gotcha: the python manage.py
APPS.)
Third, add the admin site to your URLconf (in urls.py, remember). By default, the urls.py
admin, and all you have to do is uncomment it. For the record, here are the bits you need to
admin.autodiscover()
urlpatterns = patterns('',
# ...
(r'^admin/', include(admin.site.urls)),
# ...
To start a project with Django it also provides a default admin interface that can be used to
perform create, read, update, and delete operations on the model directly. It reads a set of data
that explains and gives information about data from the model, to provide an instant interface
where the user can adjust the contents of the application. This is an in-built module designed
to execute admin-related work for the user.
The Superuser is the one who can access the Django administration panel and can perform
CURD operation on the created models which are shown in the admin panel. The admin
app(django.contrib.admin) is enabled by default and already added to the INSTALLED_APPS
list present in the settings.py file.
Note: we are assuming that you have deployed your project on local server.
It prompts for login details, if no login id is created before, then a new superuser can be created
by using the command given below:
Now, access the admin login page after starting the Development Server which can be done by
using the command given below.
This is what is called a Django Admin Dashboard where one can add, delete and update data
belonging to any registered model.
After starting your project in django and creating or resetting your django superuser you can
now access the django administrator and through django admin panel you can perform CRUD
operations on your models but the model you created in your app only shows in the admin
panel when you register them in your `admin.py` of the app you created.
Now in the myapp folder we have the file models.py in which we create our model and after
creating those model we need to register them in the admin.py file of the myapp folder after
that we make migrations of the model created.
After model creation you have to register the model in django by registering them into admin.py
file of `myapp` folder.
First import your model at the top of the admin.py file if the model you are importing is in the
same file you can import by
Now we have to register the model in the admin.py file so that it can we visible on the admin
panel
admin.site.register<Model.name>.
Now the admin page will have the model Faculty_details but make sure the server is up and
running.
Django admin by default is highly responsive GUI, which offers various features and an overall
CRUD application to help developers and users. Moreover, Django admin can be customized
to fulfill one’s needs such as showing fields on the home page of the table, etc. In this
1.Changemodelname:
If you want to change name of model which is States here so open model.py file and add
verbose_name attribute in meta section.
state/model.py
class State(models.Model):
name = models.CharField(max_length = 50)
is_active = models.IntegerField(default = 1,
blank = True,
null = True,
help_text ='1->Active, 0->Inactive',
choices =(
(1, 'Active'), (0, 'Inactive')
))
created_on = models.DateTimeField(default = timezone.now)
updated_on = models.DateTimeField(default = timezone.now,
null = True,
blank = True
)
def __str__(self):
return self.name
class Meta:
db_table = 'state'
# Add verbose name
verbose_name = 'State List'
Output :
One can show multiple fields data from model. Add some lines of code in your admin.py file.
state/admin.py:
class StateAdmin(admin.ModelAdmin):
list_display = ('name', 'active', 'created_on')
active.boolean = True
admin.site.register(State, StateAdmin)
Output :
state/admin.py:
class StateAdmin(admin.ModelAdmin):
list_display = ('name', 'active', 'created_on')
active.boolean = True
admin.site.register(State, StateAdmin)
Output:
state/admin.py:
class StateAdmin(admin.ModelAdmin):
list_display = ('name', 'active', 'created_on')
active.boolean = True
admin.site.register(State, StateAdmin)
Output:
state/admin.py:
class StateAdmin(admin.ModelAdmin):
list_display = ('name', 'active', 'created_on')
active.boolean = True
admin.site.register(State, StateAdmin)
Django Forms
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:
• Receiving and processing submitted forms and data from the client.
Syntax : Django Fields work like Django Model Fields and have the syntax:
field_name = forms.FieldType(**options)
# creating a form
class RvitmForm(forms.Form):
title = forms.CharField()
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:
class FormName(forms.Form):
field_name = forms.Field(**options)
# creating a form
class InputForm(forms.Form):
Django form fields have several built-in methods to ease the work of the developer but
sometimes one needs to implement things manually for customizing User Interface(UI). A form
comes with 3 in-built methods that can be used to render Django form fields.
To render this form into a view, move to views.py and create a home_view as below
context ={}
context['form']= InputForm()
return render(request, "home.html", context)
In view, one needs to just create an instance of the form class created above in forms.py. Now
let’s edit templates > home.html
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. Now when we have our project ready, create a model in Rvitm/models.py,
URLConf Ticks
As a Django application grows in complexity, its URLconf grows, too, and keeping those
imports can be tedious to manage. (For each new view function, you have to remember to
import it, and the import statement tends to get overly long if you use this approach.) It’s
possible to avoid that tedium by importing the views module itself. This example URLconf
depicts that
A further shortcut you can take when using the string technique is to factor out a common
“view prefix.” In our URLconf example, each of the view strings starts with 'mysite.views',
which is redundant to type. We can factor out that common prefix and pass it as the first argu
ment to patterns(), like this