Professional Documents
Culture Documents
Class-Base Function Base CRUD PDF
Class-Base Function Base CRUD PDF
UPDATE 2: I added a new example application with user access, user must login and each user has his own
list.
Sample Code: You can download a sample application from https://github.com/rayed/django_crud
In this post I briefly cover the step needed to create a CRUD app in Django, the steps we will need are:
Create an App
Create the Model
Create the Admin Interface (optional)
Create the View
Define the URLs (i.e. URL to View mapping)
Create the Templates
We will also need to register the new app in our Django project, add the app ‘server’ to the INSTALLED_APPS
in your django_proj_name/settings.py:
INSTALLED_APPS = (
:
'servers',
:
)
converted by Web2PDFConvert.com
from django.db import models
from django.core.urlresolvers import reverse
class Server(models.Model):
name = models.CharField(max_length=200)
ip = models.GenericIPAddressField()
order = models.IntegerField()
def __unicode__(self):
return self.name
def get_absolute_url(self):
return reverse('server_edit', kwargs={'pk': self.pk})
./manage.py makemigrations
./manage.py migrate
The Views
We will use Django Class-based views to crete our app pages, the file servers/views.py would look like:
converted by Web2PDFConvert.com
Define the URLs
We need to define app URLs in the file __servers/urls.py:
This URLs wouldn’t work unless you include the servers/urls.py in the main URLs file
django_proj_name/urls.py:
urlpatterns = patterns('',
:
url(r'^servers/', include('servers.urls')),
:
)
Templates
templates/servers/server_form.html This file will be used by Edit and Update views:
<h1>Servers</h1>
<ul>
{% for server in object_list %}
<li>{{ server.name }} :
<a href="{% url "server_edit" server.id %}">{{ server.ip }}</a>
<a href="{% url "server_delete" server.id %}">delete</a>
</li>
{% endfor %}
</ul>
<a href="{% url "server_new" %}">New</a>
converted by Web2PDFConvert.com
Function Based View Version
The example above uses Class Based Views (or CBV for short) to implement the views, what I will cover now
is how to implement the same functionality but with Function Based Views i.e. using functions instead of
classes, we will be using the same templates:
servers/views.py:
servers/urls.py:
p.s. personally I prefer using FBV over CBV, the article Django’s CBVs were a mistake explains why.
converted by Web2PDFConvert.com
July 19, 2013 Rayed Uncategorized django, python
OLDER COMMENTS
Sachin Panchal
September 18, 2016 at 9:40 pm
Thank you …
dave
October 18, 2016 at 3:37 pm
instead of “manage.py syncdb” use “manage.py migrate” if Yo are using Django newer than 1.07
Rayed
October 18, 2016 at 3:45 pm
Gio
November 17, 2016 at 4:43 pm
Thanks for tutorial! I’ve a question regarding ClassBased views. How django understands which template to
use. For example in case of Update, how it knows that server_form.html located in
templates/servers/server_form.html is the right template…
Rayed
November 18, 2016 at 1:33 am
@Gio Django will use the value of “template_name” attribute as a template file, and the default value would
depend on the class you would inherit from, e.g.:
You can also override the suffix only using the attribute “template_name_suffix”, for example if you would
like to have different template for “CreateView” and “UpdateView” you would change
converted by Web2PDFConvert.com
“template_name_suffix” from the default “_form” to “_create_form” and “_update_form” respectively.
Gio
November 18, 2016 at 8:58 pm
Rayed
November 19, 2016 at 3:09 am
@Gio Sorry I didn’t understand, are you trying to build a CRUD to manage Django users?
Gio
November 19, 2016 at 9:52 am
@Rayed To manage users I will use Django admin, but I’m trying to allow registered users edit their profiles,
which I already did. But I have one issue… I’ve extended user registration with extra fields, it means that
extended fields are located in a separate table ind DB. Now when I’m retrieving records from User model
only default fields are selected. Now thinking how I can take other fields also.. for example: first_name,
last_name, username, email, address.
Why you asked? Am I doing something wrong? I’m new in Django.
Rayed
November 19, 2016 at 2:31 pm
@Gio Not sure how you can do it using CBV but with FBV you would use a view similar to “server_update”
with two forms instead of one.
Gio
November 23, 2016 at 10:18 am
@Rayed Thanks
MD Aftab Alam
December 14, 2016 at 12:11 pm
Fred
January 11, 2017 at 9:30 pm
Great Job
Thank you
Alok Lal
converted by Web2PDFConvert.com
January 28, 2017 at 4:21 am
Nicely captured detail on simple but fundamental model actions. I particularly appreciate the parallel
implementations of the class based views and function based views. I personally also prefer the function
based views. Sure, it’s more verbose but I think it makes for clear, intuitive code.
OLDER COMMENTS
Leave a Reply
Your email address will not be published. Required fields are marked *
COMMENT
NAME *
EMAIL *
WEBSITE
POST COMMENT
converted by Web2PDFConvert.com
Proudly powered by WordPress
converted by Web2PDFConvert.com