Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Using the Django Admin Interface

In this video, you’re going to learn about the Django admin interface and how you can use it to conveniently interact with your database. Django comes with a few things built in from the get-go. In your settings.py file, there were apps that came pre-installed. One of them is the admin app.

00:00 Hey! In this video, we’re going to talk about the Django admin interface and how it allows us to conveniently interact with our database.

00:11 So as you know, Django comes built-in with a couple of things from the get-go. We’ve seen that—inside of our settings file—we saw that there’s a lot of apps that come already installed. One of them is the admin app, and we haven’t seen this one yet, but we’ve encountered it before.

00:28 We’ve seen it here in INSTALLED_APPS, and in our main urls.py file there’s this endpoint called 'admin/' that takes us somewhere.

00:38 Now, you already understand what’s happening here. So in this case, it’s going to some other URL configuration, but you can see that typing in /admin should be taking us to somewhere.

00:48 Let’s give this a try. If instead of /projects I type /admin, let’s see where we go to. See? There’s like a whole nicely-styled page that’s just sitting there, that is part of our project that we didn’t even know about.

01:04 It’s called Django administration. So, how would we be able to log in here? We can’t yet, because we haven’t made this superuser that we need to be able to log into our Django admin app.

01:18 So, what we need to do first is we need to create a superuser. I’m going to head over to the command line and do that right away.

01:27 With an open terminal window, I’m going to say python manage.py, as nearly always, createsuperuser. That’s the command that we need to—you guessed it—create a superuser.

01:41 So when I run this, it asks me for a username—I’m going to put in my name here. Email address—I’m just going to leave that blank. And then pass in the password—“Pass in the password.” All right. Repeat.

01:58 Oh, haha. All right.

02:03 You’ve got to make passwords that actually match, type two times the same thing. And then it works, there you go. Our superuser was created successfully.

02:12 And now I’m able to log into this admin interface. Let’s go ahead and check it out.

02:20 I’m putting in my credentials. Let’s see if I can type my password correctly this time. It looks good. Okay. So, I’ve entered the Django administration in here and we can see, like, it looks like we’re probably able to do some things here: Add, Change, Userswhat do we have in here? Let’s take a look. Interesting, so this is the user that I just created, and then there’s another superuser that I created earlier on.

02:49 So, there’s currently two users. And this looks very much like a table. So, what we’re interested in, actually, is not the groups and users, though. We’re interested in the projects, so that we would be able to add projects, edit projects, and delete projects from a convenient admin interface in here.

03:08 Django makes that actually very easy. Some things we need to do, and the first thing is we need to register our models in the admin interface. For that, we’re going to open up a file that we haven’t looked at yet inside of our project. That’s admin.py.

03:28 We don’t really have anything in here at the moment, but Django gives us a nice prompt, again: Register your models here. So, it’s talking about my models.

03:37 I want to have the Project model, so I will have to import it. Let’s start off with that.

03:51 Then, to register it, what I need to type is

03:58 admin.site.register and then pass in the name of my model that I just imported. And that’s really all. It’s just these two lines of code, and this already allows me to have access to my models inside of the Django interface. Let’s take a look.

04:18 When I reload this page,

04:22 my projects turn up here and I can take a look at them. I have my Project object (1) and (2). Cool! And everything is in here, right?

04:33 So I can go ahead and edit directly in here. I can call this…

04:45 and then go ahead and SAVE. I’ll open up a new window so that we can see the changes right away.

05:03 And there you go! The title changed simply by typing it in here, which makes it very easy. Let’s go ahead and try and add a project. Title is going to be…

05:25 We need an image. Some suggestions of some that I did before, project/img/daily.png. We have this one inside of our static/ files already—there it is—which means I’ll be able to link to it. I SAVE this.

05:45 It just has the number 5 here because I previously added and deleted some. It just keeps counting forward, but that shouldn’t be a problem for us.

05:56 Let’s take a look. It correctly renders! And here it is with a new image. Can I also click in here? I can! Great. And as you can see, because we’re using the id, or the primary key, this is also number 5, up here. Number 3, and number 4 were created, but then deleted, but the database didn’t update the IDs.

06:17 So, it’s just going to keep counting upwards. For you, probably this is going to be number 3.

06:27 Cool! So, let’s go ahead and try the other things that I just mentioned. I can also delete an object in here. Let’s get rid of this one. So I can say, Project object (2), Delete, and Go.

06:41 Yes, I’m sure, let’s get rid of it. Heading over to our app again, there it is! It’s gone. So as you can see, this Django administration is an awesome tool for interacting and updating your app, and this already comes built-in with Django.

06:57 So, the only thing you need to do is create your project and make sure that the admin app is installed, register your models. And then you’re able to very, very conveniently interact with your database, add projects, remove projects, or edit projects—the text in there, the image, the technology, whatever you want.

07:16 So, that’s a great way of keeping your site easily up-to-date. With this, we’re at the end of this course. So, in the next video, we’re going to do a quick recap of what we learned in this section, and then I have a couple of suggestions for you in how you can still improve this project to make it even more awesome than it already is.

07:37 See you in the next video.

Avatar image for Ricky White

Ricky White RP Team on March 13, 2020

Mark. Do as Martin said above then we’ll assess. The only amendment I’d make, is as you’re on Windows, in 5. you activate your virtual environment with .env\Scripts\activate.bat.

Avatar image for Mark R Baker

Mark R Baker on March 13, 2020

Hello Martin and Ricky! You are too kind. I tried all these steps again. What happened: after I redid all this, admin worked for a few seconds, but when I tried to login, it quit as before:

(.env) C:\Users\mrbak\python_code\django-portfolio\django-portfolio>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
March 13, 2020 - 16:02:58
Django version 3.0.4, using settings 'portfolio.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[13/Mar/2020 16:03:23] "GET /projects/ HTTP/1.1" 200 4501
[13/Mar/2020 16:03:23] "GET /static/projects/img/testproject.png HTTP/1.1" 200 155219
[13/Mar/2020 16:03:23] "GET /static/projects/img/daily.png HTTP/1.1" 200 157031
[13/Mar/2020 16:03:23] "GET /static/projects/img/todo.png HTTP/1.1" 200 105814
[13/Mar/2020 16:03:26] "GET /projects/1 HTTP/1.1" 200 1748
[13/Mar/2020 16:03:28] "GET /projects/ HTTP/1.1" 200 4501
[13/Mar/2020 16:03:30] "GET /projects/2 HTTP/1.1" 200 1686
[13/Mar/2020 16:03:32] "GET /projects/ HTTP/1.1" 200 4501
[13/Mar/2020 16:03:34] "GET /projects/3 HTTP/1.1" 200 1785
[13/Mar/2020 16:03:36] "GET /projects/ HTTP/1.1" 200 4501
[13/Mar/2020 16:04:52] "GET /admin/ HTTP/1.1" 302 0
[13/Mar/2020 16:04:52] "GET /admin/login/?next=/admin/ HTTP/1.1" 200 1913
[13/Mar/2020 16:04:52] "GET /static/admin/css/base.css HTTP/1.1" 200 16378
[13/Mar/2020 16:04:52] "GET /static/admin/css/login.css HTTP/1.1" 200 1233
[13/Mar/2020 16:04:52] "GET /static/admin/css/responsive.css HTTP/1.1" 200 18052
[13/Mar/2020 16:04:52] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[13/Mar/2020 16:04:52] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
[13/Mar/2020 16:04:52] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
[13/Mar/2020 16:05:22] "POST /admin/login/?next=/admin/ HTTP/1.1" 302 0

(.env) C:\Users\mrbak\python_code\django-portfolio\django-portfolio>

And then, all further tries do the same thing as before. Server shuts down as soon as I try to access localhost:8000/admin/ Maybe I will just have to do all python/django stuff on the iMac. My son will be disappointed, as he prefers the iMac for games. Thanks again for all your help. Cheers, Mark

Avatar image for Martin Breuss

Martin Breuss RP Team on March 14, 2020

That’s interesting. It shows that it initially accesses the resources from your admin app to load the interface, but then breaks after the POST request, which is, from what you wrote, after submitting your login information and attempting to redirect you to the admin portal.

Another quick question: Have you created a superuser beforehand with:

python manage.py createsuperuser

You server shouldn’t quit also if you hadn’t made a superuser yet, but I’m just wondering.

Any ideas @Ricky how we could keep Mark’s son playing iMac games? 👾

Avatar image for Ricky White

Ricky White RP Team on March 14, 2020

My mind went to the same as yours, Martin. There is a problem with the POST request. I’d be tempted to delete the sqlite.db and start a fresh, making sure to create a superuser and migrate etc. I’ve done all my Django development on Windows, and never seen this.

Avatar image for Mark R Baker

Mark R Baker on March 14, 2020

The github version seems to already have the superuser I created earlier, so I tried to login with that userid/password:

C:\Users\mrbak\python_code\django-portfolio\django-portfolio>python manage.py createsuperuser
Username (leave blank to use 'mrbak'): mrbaker1917
Error: That username is already taken.

Should I create a new one? Thanks for all your help. Mark

Avatar image for Martin Breuss

Martin Breuss RP Team on March 15, 2020

As Ricky mentioned, I’d also suggest to delete your db.sqlite3 file, then run your migration commands again:

python manage.py makemigrations
python manage.py migrate

And, after that, create a new superuser:

python manage.py createsuperuser

Something might be off in your database file, so deleting it and re-creating it might do the fix.

Databases and Version Control

Generally you want to avoid committing your database file to open source. Having it up there means that all the data you put in is also accessible to everyone on the web.

This is not a big deal for a training app, but down the road your database might contain sensible information about your users.

You can add your db.sqlite3 file to your .gitignore file to avoid pushing it to your remote repository.

Avatar image for R morel

R morel on May 5, 2020

ProgrammingError at /projects/ relation “projects_project” does not exist LINE 1: …t”.”technologey”, “projects_project”.”image” FROM “projects_… ^ Request Method: GET Request URL: localhost:8000/projects/ Django Version: 2.2.1 Exception Type: ProgrammingError Exception Value: relation “projects_project” does not exist LINE 1: …t”.”technologey”, “projects_project”.”image” FROM “projects_… ^ Exception Location: /Users/morel893/Desktop/env/lib/python3.7/site-packages/django/db/backends/utils.py in _execute, line 84 Python Executable: /Users/morel893/Desktop/env/bin/python Python Version: 3.7.7 Python Path: [‘/Users/morel893/Desktop/django-portfolio’, ‘/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip’, ‘/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7’, ‘/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload’, ‘/Users/morel893/Desktop/env/lib/python3.7/site-packages’] Server time: Tue, 5 May 2020 04:33:45 +0000

help plis

Avatar image for Martin Breuss

Martin Breuss RP Team on May 5, 2020

If you get a ProgrammingError it often means that there’s something to be done with your database. Especially when it mentions “relations” like in your case.

Halp: makemigrations and migrate :) Hope this helps!

Avatar image for R morel

R morel on May 5, 2020

ok

Avatar image for R morel

R morel on May 6, 2020

thanks for mi is work thanks ever

Become a Member to join the conversation.