Emma
Diverse decks of slide mainly about Django
By Emmanuelle Delescolle - @EmmaDelescolle
sudo pip install cookiecutter
cookiecutter bb:emma_nuelle/cc_plr_django_intro
We will focus on steps between 1 and 2 only Django and a little bit of HTML
Image found on Know your Meme
Unlike with PHP or static HTML, the web server will not try to find script files according to the requested URL. URL's are passed "as is" to Django who will take care of parsing them.
Parsing of URL's happens according to <project_dir>/urls.py. A bit like in the router of RoR (without resources).
url(r'^$', TemplateView.as_view(template_name='base.html'), name='home'),
If you'd like to know more about what REGEXes are, you may want to check out the official Python documentation.
Let's see what happens if we replace base.html with another template.
Django looks for templates in every INSTALLED_APP's templates directory.
Models are entities that you would like to persist (or save).
Models have properties called fields.
In the case of a ToDo item, we have 2 properties:
Models are defined in <app_name>/models.py
In order for Django to be aware of your models the application <app_name> has to be listed in INSTALLED_APPS in your settings.py file
Django Models are actually stored in a database.
In order for your project to work properly, the Models you define in your models.py files also have to be defined in the database.
For that to happen, every time you change something in a models.py file, you have to ask Django to create a migration and then you'll have to apply it.
Migration files are just files with a set of instructions only a database engine can understand.
In order to create a migration, go to the terminal window (with your virtualenv activated) and run the following command:
Now, we have to apply the migration by running:
./manage.py makemigations
./manage.py migrate
In order for you to easily add/edit/delete items (model instances), Django provides an easy to use admin.
To make your newly model available in the admin, all you have to do is register it.
In order to make the ModelAdmin more user friendly, Django offers plenty of options so that you can customize each and every admin page to your needs.
Let's try a couple of those!
A template can be the exact copy of another template and only override portions (blocks) of it. It's called template inheritance.
The logic of your application in Django is placed in a file called views.py (inside the application directory).
Since the logic behind a lot of web pages is often the same (list all items from a model, create/ edit/ delete one of those items) Django provides a set of Generic Class-Based Views which are nicely documented on CCBV.co.uk
(in a good way) - We love lasagna!
Garfieldยฉ is copyrighted but I found another kitten to sacrifice on a decent subreddit. Also checkout this one
Don't worry, with those lasagnas, you can pick away any layer you don't like and replace it with one better suited to your liking!
Let's see it in action by creating our first ListView!
๐ง ๐จ ๐ฉ
With Django Debug Toolbar
pip install django-debug-toolbar
In Django views, a context is a dictionary of values that the view passes to the template.
In a Class-Based View, you can easily add information to the context by overridding get_context_data.
Let's add the number of ToDo's left in the footer
As with templates, you can create new views on top of other views and only override the parts that change.
Photo by Hannah Whitaker, found on NYMAG.com
Let's build filtered views with some custom Querysets.
Let's take a closer look!
Illustration by Persian Poet Gal
def get_context_data(self, **kwargs):
context = super(ToDoListView, self).get_context_data(**kwargs)
context['todos_left'] = self.get_queryset().filter(done=False).count()
context['active_menu'] = self.active_menu
return context
def get_queryset(self):
qs = super(DoneToDoListView, self).get_queryset()
return qs.filter(done=True)
Vs
Let's fix that by using a different Queryset
Lasagnas are like cards, you can shuffle them!
(Please don't try this at home with real lasagnas)
The problem when shuffling 2 lasagnas together is that both lasagnas have a melted cheese top layer.
It would be much easier to shuffle a full lasagna with a lasagna stub!
When it comes to Class-Based Views, Django provides lasagna stubs which are called mixins.
When clicking on a ToDo's checkbox, we want Django to update the ToDo and then redirect us back to the homepage.
To do that, we will shuffle a SingleObjectMixin which will provide us with the ToDo we need to update, with a RedirectView which will send us back to the homepage.
in order for our View to know which ToDo it needs to fetch, we have to give it a clue, that clue is called a pk.
url(r'^toggle/(?P<pk>\d+)/$', ToggleDoneView.as_view(), name='toggle'),
All you really need to remember from this is that the bizarre string (?P<pk>\d+) is how you tell Django that the URL will contain a pk.
It's easier to write on the template side:
{% url 'toggle' todo.pk %}
CreateView's don't have a mixin equivalent but ListView's do, it's called MultipleObjectMixin.
It will only take an extra few lines of code to allow us to create more ToDo's (haven't we done enough yet?)
To delete a ToDo, the logic is almost the same as to toggle one.
Let's stay DRY, nobody likes wet lasagna anyways!
Picture of Gรฉrard Majax
Let's tie it all up together and add the ability to update a ToDo item with an UpdateView.
The only thing left to do now is to enable AJAX for ToDo items creation!
By Emma
Supporting deck of slides for PyLadies Remote Introduction to Django live class