Locked lesson.
About this lesson
There may be times when you have different Python programs you want to run on your Django project. In this lesson, we'll discuss how to use and import an external file that does something unique into your Django project.
Exercise files
Download this lesson’s related exercise files.
Using External Python Programs.docx57.4 KB Using External Python Programs - Solution.docx
57.7 KB
Quick reference
Using External Python Programs
Django allows you to use external Python programs in your Django project.
When to use
Use them whenever you need to run or use an external Python program in your Django project.
Instructions
Save the python program in your app directory of your Django project. Then, import it into the view where you want to use it.
from resume.program_name import program_function
Then pass the output of the program into your web page in the context dictionary.
Finally, reference the output that you passed through your context dictionary on the actual web page using a variable.
Hints & tips
- from resume.program_name import program_function
- 00:04 Okay, in this video I want to o show you how to use external Python programs in
- 00:08 your Django project and on your web pages.
- 00:10 So I'm just going to come up here to our home function and
- 00:13 get rid of the stuff that we did in the last video.
- 00:16 We don't really need that anymore.
- 00:18 So there may be times when you have different Python programs you want to run
- 00:23 on your Django project.
- 00:25 Some external file that does something unique that you want to kind of use and
- 00:30 import into your Django project.
- 00:33 And that's one of the really cool things about Django,
- 00:35 it makes it really easy to do that.
- 00:37 So let's create just a little simple Python program that we want to use
- 00:42 in our app.
- 00:42 So I'm going to go to our resume directory.
- 00:45 This is just the main directory we've been working in all this time.
- 00:48 And let's create a new file.
- 00:49 And let's save this as names, names.py, right?
- 00:56 So let's define a function called names.
- 00:58 And this is a function.
- 01:01 And inside of here, just let's just make this really simple.
- 01:04 Let's say return,
- 01:07 My name is still John Elder, right?
- 01:12 Now let's go ahead and save this file.
- 01:14 This is just a very basic function.
- 01:15 All it does is returns this string, right?
- 01:18 So but let's say we want to call this file, we want to run this program and
- 01:21 we want to use the results of this program in our web page.
- 01:25 Let's say we want to use it on our homepage.
- 01:26 So now we just need to import this file into our views.
- 01:31 So to do that we just go from
- 01:36 resume.names, import names.
- 01:42 So what we're doing here is we're saying hey,
- 01:45 from this directory resume, and inside of this directory,
- 01:50 inside the file names, which is just this file we created, right?
- 01:55 We want to import the function names, right?
- 01:59 So if this was Bob, we would import Bob, right?
- 02:04 But it's names.
- 02:06 So do that.
- 02:08 Now we can pass in this, whatever this returns into our
- 02:13 context dictionary, like we have already in the past.
- 02:17 So let's call this names and it's just names.
- 02:20 And again, if we call this Bob.
- 02:25 This would be Bob, vice versa.
- 02:26 So whatever this is called here, we want to pass that in there, right?
- 02:31 So let's save this.
- 02:33 Now let's head over to our home.html page and let's get rid of this for
- 02:36 loop from the last video.
- 02:37 Now we can just call this names, right?
- 02:41 So if we save this, head back over to our website,
- 02:45 reload the homepage, we see my name is still John.
- 02:49 It's returning whatever that program returns.
- 02:51 So that's very cool.
- 02:52 And this names.py function was very simple, but
- 02:56 we could do anything you wanted in this file.
- 03:00 We could go 2 + 3, right?
- 03:03 So 2+3 is 5, this is going to return 5 when we call names,
- 03:08 which here we're importing it, passing it in.
- 03:12 So if we go back to our homepage and hit reload,
- 03:14 we would expect instead of seeing my name is still John Elder, we would see 5.
- 03:19 If we hit reload, boom, we get 5.
- 03:21 So I hope you can see how powerful this is because there may be times when you have
- 03:26 complicated programs that you want to run that are hundreds of lines of code.
- 03:31 This names.py file could be thousands of lines of code.
- 03:35 Whenever it does, you can return it onto your homepage, just this easily.
- 03:39 So that's really powerful.
- 03:42 So that's more of an advanced topic if you're doing more advanced things, but
- 03:46 I just wanted to show you how to do that, since it is so easy.
- 03:48 It's very cool.
- 03:49 So that's all for
- 03:50 the sort of technical Python programming behind the scenes things.
- 03:55 In the next video, we're going to start looking at how to use CSS images and
- 03:59 JavaScript to make our web pages look better.
- 04:02 And that'll be starting in the next video.
Lesson notes are only available for subscribers.