data model 2.Modify the index to retrieve all entries from the database 3.Create a function add that will add entries to the database (no longer done by Javascript)
class Entry(models.Model):! image = models.CharField(max_length=200)! name = models.CharField(max_length=200)! webpage = models.CharField(max_length=200) WebDirectory/models.py With Django, we define a class representing the data structure • https://docs.djangoproject.com/en/1.7/ref/models/fields/
... Database img name url http:// Bart http:// http:// Lisa http:// For each entry in the database ... Templates <html>! <head>! <title> ... ... create the corresponding HTML entry in the template webdirectory/
WebDirectory.models import Entry! ! def index(request):! entry_list = Entry.objects.all()! return render(request,\! 'WebDirectory/index.html',\! {'entry_list': entry_list}) Fetch all entries in the database Call the template index.html with the list of all entries as argument
How to define add <html>! <head ... Controller def add(request):! ...! ! def index(request):! ... 1) extract the arguments image - name - webpage add/?name=... 3) redirect to the (updated) index page 2) add the entry in the database http:// Marge http://
django.core.urlresolvers import reverse! from WebDirectory.models import Entry! ! def add(request):! e = Entry(image= request.GET['image'],\ ! name = request.GET['name'],\ ! webpage = request.GET['website'])! e.save() ! return HttpResponseRedirect(reverse('index')) Create the new entry Save the entry in the database redirect to the index page