We simply defined the model Movies by defining two fields, title and genre. We also defined user as the Foreign Key so that we’ll be able to use the built-in user model. By defining __unicode__() method, Django will call it when it needs to render an object in a context where a string representation is needed. get_absolute_url() method will tell Django how to calculate the canonical URL for an object. In other words, this method will return a string that can be used to refer to the object over HTTP.
Now apply the migrations as follows:
python manage.py makemigrations
and
python manage.py migrate
Let’s register our model to admin interface. Modify CRUD_FBVs/admin.py file as follow,
from django.contrib import admin
from .models import Movies
admin.site.register(Movies)
We also need to create a simple form to perform the CRUD operations. Create a new python file inside your app and name it forms.py. Append the following code to it.
from django import forms
from .models import Movies
class MoviesForm(forms.ModelForm):
class Meta:
model = Movies
fields = ['title', 'genre']
Function Based views uses decorators to achieve the special functionality. Let’s see how that works. Edit views.py file as follow:
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404, redirect
We imported @login_required decorator which will limit the functionality of the CRUD operations to the logged in user. Remaining code is pretty simple and self-explanatory. You might have noticed the templates mentioned in the above code which we haven’t created yet. Let’s do that. Create templates folder in your app and create the HTML files as follow,
That’s it. Now your app is ready to be up and running. Start your server by either executing the following command or directly by running it from your IDE.
python manage.py runserver
You can create superuser and use that to test the functionality or feel free to extend it.
Is there any con?
Function based views are easy to read and simple to implement but there’s also a downside to it. They are hard to customize or extend the functionality. Also they don’t allow the code reuse in true sense so repetitiveness still exists. To deal with all these issues, Class Based Views were introduced.
CRUD using Class Based Views
CRUD operations can be implemented in no time using CBVs, one of the biggest advantages being that if model evolves, changes would be automatically reflected in CBVs if done properly. Thus you can save tens of lines of code by implementing CBVs. They are easily extendable and also allow code reuse. Moreover, Django has built-in generic CBVs which make the life of a developer easy. Let’s implement them and see how the things turn out to be.
We are not going to just dump whatever we have done so far so keep calm. We just need to change our views and urls file as follow.
from django.views.generic import ListView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
We imported the generic Create, Update and Delete Views from Django which are pretty much performing all the CRUD operations. Now let’s edit urls.py file as follows:
原文地址:http://www.open-open.com/lib/view/open1346857871615.html
使用Java Mail API来发送邮件也很容易实现,但是最近公司一个同事封装的邮件API实在让我无法接受,于是便打算改用Spring Mail API来发送邮件,顺便记录下这篇文章。 【Spring Mail API】
Spring Mail API都在org.spri