Hello, previously we created a view, which is a
class that connects the model with the template.
Now in this video, I want to enter
some dummy data in our database table.
So our database file is db.sqlite3,
and it has several tables inside.
I want to use the DB browser for
SQLite and then go to Open Database.
And then this is a root directory and go
to db.sqlite3 and double click there.
Then you want to go to Browse
Data, and find the blog_post table.
So these are the fields of the database table.
Do you want to expand them a
little bit, to make them more visible?
And date_created, slug, status of the post.
So draft or published and the author_id, which could be a number.
So the first author that will be created
has an id of one and so on.
So let us manually enter a new table row.
Of course, as I told you, these
should be entered through the Admin interface.
But for now, just for testing, we want
to go to this button, press that, and
that will insert a new, data, table row.
So the id was automatically generated.
So for the first row it will be 1, for the
second, it will auto increment to 2 and so on.
So let's put something for the
title like Dogs and content,
Dogs are good.
That's enough for now. date_created,
this should be in format like 2022, 12 for
the month, 10 for the day of the month.
slug like that dogs, and Django will
take care of building the complete URL.
So example, add .com, it will add a slash and
then it will add the slug dogs, after
the slash. status, this was draft or published.
So, in here, you see we have zero or ones.
So let's put 1for published. author_id,
the first user of the database, we
did create already a super user.
So that user has the id 1, right.
Once you have done that, you want to go
to write changes to the database file, all right?
And then you can just close the
database and the row is there.
Now, let's connect that slug, that
URL to our views BlogView.
To do that you need to go to your
blog folder, and right click and go to New
File and create a urls.py file, urls.
And in here you want to import
from the current location, import views.
So we are importing the views module.
It's a file, but when you import things,
that is a module, it becomes a module.
That's how we call it.
So Views now is available here, and we also
need to import something else from django.urls.
A module urls is another module of django import path.
And we need this Path now, which is a function,
to create a urls, urlpatterns list.
So I'm splitting that because this
may contain different urlpatterns.
So the first pattern we are interested
about is created through the path function.
So what is a pattern?
Well, it is slug, a colon and slug
again, with these opening and closing brackets.
So when the user visits a certain URL,
let's say, let me write down something here.
For example, example.com, slug
of course they write like dogs, but dogs, we are
telling Django to look for dogs in the slug field.
So Django will go to our table, database table
and it will search for, in each row for
dogs, in each cell of slug actually.
So in each row of the slug
field, it will search for dogs.
And if it finds a row that has dogs, it's going to
execute BlogView, delete the parentheses,
BlogView.as_view, so execute that class as a view.
And there's also something else we need to
give here which we will use later.
It's a name, which should be a
string and let's say blog_view.
This is a name we may need later.
So it's a good idea to have it here now.
So I hope you are following so far.
We also need to do something else regarding URLs.
You see that, there is another urls.py file, but it is inside my
site, which contains the configurations of the
Django project, not the Django apps.
So this file, looks like this already,
it's built by Django automatically.
And, here is, this one
here is also a URL patterns variable,
just like this one in here, urlpatterns, urlpatterns.
But this now, wants to know about
the urls of the Blog app.
So with each app you create, you want
to declare the urlpattern in here.
So after the comma we create
another listitem which is path.
This time it has to be like this.
So an empty string which denotes the home directory.
So when the home directory is visited and then
something else, after the slash is written by the
user in the address bar of the browser.
And this something else is defined by the blogs.
blog is the name of the app.
So blog, that folder name comes in here, .urls.
urls is the name of the urls.py file of the blog app.
So blog.urls.
You could also write something else here.
So you could write my blogs for example.
And in that case. Django would be expecting
users, not example.com/dogs, but example.com/ myblogs/dogs.
But we don't want that, so let's keep it like that.
example.com/dogs
Now we get this error, include is not defined and we need to import
it from django.urls import path and include
[No Audio]
save.
[No Audio]
Also make sure to save all the files.
So save this as well.
Ctrl+S, save the views as well.
So everything you see with that black dot in Visual
Studio Code, that means it hasn't been saved yet.
Now, let's see what errors we get.
type object 'BlogView' has no attribute 'as_view'.
So see what's going on here.
This happens in this file.
So urls.py, blog/urls.py, which is here, hmm.
So this class does not have that attribute.
Let's take a look.
This class, of course it doesn't have that
attribute, it only has model and template_name
attributes. No methods called as_view,
that is because, I forgot to inherit from a specialized
view class ,which we need to import.
from django, so from django views import generic and then
from generic, we can access the DetailView
Detail, not DeleteView, DetailView class.
So delete the parentheses.
So that means this class actually
has an as_view method inside.
So you can right click and Go to Definition.
And of course this does not have any methods
here, but it inherits from some other methods.
So if we go to BaseDetailView, go
to definition, this is also inheriting from these two.
So maybe View has that, as_view function.
Yeah, now we found it, so that's the method.
So there's a chain of inheritance and we have access
to that method now, from our BlogView class, great.
[No Audio]
Great, save this file
[No Audio]
and now it's time to try out our app.
And you want to go to this URL.
So, this is to be expected.
So the homepage is not configured yet, we
don't have any URL pattern that, tell Django
what to do when people visit the homepage.
So what you should look instead is, one of your blogs.
So currently we have dogs, the dogs slag, you remember that,
I inserted it into the table and we get this error,
TemplateDoesNotExist, an easy error to fix at /dogs.
So Django recognized that we do have a
dogs entry in the database, because if you
tried for cats, you'd get another error.
So this time you'd get, Page not found.
And Django tried these patterns and
didn't find any slug for cats.
So dogs is a different thing,
but the Template is not found.
[No Audio]
So blog.html, this usually is a problem with
miscommunication with Django, so Django is not being able
to find the path of blog.html.
So let's check, blog.html is correct here.
So the View has the name correctly, and then let's
look at the settings,
[No Audio]
templates, the name is correct here.
So this looks fine but, if you scroll
up in here, you have this TEMPLATES variable.
And this now has some parameters about TEMPLATES,
and one of these parameters is DIRS.
So these are keys of that dictionary, BACKEND, DIRS key.
The DIRS key expects a value and currently
it's an empty list, but you need to
declare here, the list of TEMPLATE directories.
So in this case we only have one because
you could have more but, we have one.
So we have to declare that in there, save
[No Audio]
and we get this other error, TEMPLATES_DIR is not defined of course,
because we are using it in here, before we defined it.
So cut that from there, and put it above its usage.
So above thisTEMPLATES, variable output it just here,
[No Audio]
save, and our server was interrupted.
So we want to execute again,
python manage.py runserver.
Let's see what we get this time.
Go to the browser, and whoop, we get the blog.
Dogs, Dogs are good!
and ardit, the name of the author.
It's working fine.
So that concludes this video.
So let me wrap it up so you know where you stand.
[No Audio]
So, so far we are able to address the user URL concerns.
So when they enter example.com/dogs, for example,
this urls.py is being triggered, and this
deploys the views and the models.
So it connects the two together,
to query data from the database.
And those data are injected into these, variables
with curly brackets in the HTML Template.
And of course, this then, these are rendered in the browser for the user.
And as you saw, this time for now, I
just entered some data manually using a third party
program, that accesses the SQLite 3 databases.
But normally as you see in the next videos,
we want to get data through an admin interface.
So this is what we're going to do next.
We are going to create the admin
interface, and enter some data in there, see you.