Python Basics and How to Use It With Django

Learn Python Berkeley California

This guide covers the basics of Django CMS and how to best use it with Python. If you live in the Bay Area and are considering enrolling in a San Francisco data class, this tutorial will help you understand the fundamentals. 

Why Choose Django as a Python web framework

Django is connected with the Django Software Foundation and is sponsored by Instagram and JetBrains, among other companies. It is a versatile framework that can solve web development problems including sessions, security features, template processing, localization and other issues. It can also quickly create secure and reliable web applications and comes with extensive features including caching, ORM, form processing, a server for testing and development, a template engine and more. 

Django is standardized and open-source with a large developer community, providing extensive user support and a variety of libraries, allowing developers to perform nearly any programming task. 

Installing Django in Python*

*This tutorial assumes you are working on a Windows operating system.

Before using Django, make sure you have installed Virtualenv, Python and Django on your computer. While a virtual environment is not completely necessary, it’s a good idea to use one when you’re getting started, since it provides each project with its own environment and prevents them from clashing. Otherwise, you will have to rely on external libraries.

Look for the latest version of Python here. Select the appropriate download for your operating system.

Once you have downloaded the file, go to your Downloads folder and select “Run as administrator.” You should see a confirmation that the installation was successful. Next, open your Command Prompt program, to ensure it is working. Type this command into your program:

The output should include the correct Python version number and “python –version.” Once you have confirmed that Python is working on your machine, use pip to install virtualenv. Use this command in your Command Prompt program:

Everything else will be automatically downloaded to the the virtual environment, which will let you install Django and play around without causing any damage. Create a project folder to organize all your files by typing this command into your program:

We will store everything related to Django in the projectfolder folder. Run this command to create the virtual environment for Django:

Now, activate it by typing in this command:

Finally, all we need to do to install the Django CMS program is to run this command:

Starting Your First Project 

Start your Django project by running this command:

This will create the structure of the project folders. One benefit of using Django is its basic web browser, which allows you to run your project locally. To test it, run this command:

Working With Django/Python Apps

A data class is likely to explain the importance of apps and projects when using Django. An app, or “web application,” does something online, while a project is a collection of one or many apps. Keep in mind, you cannot run a Django app without a project, however, you can create a full website within a single app. Django has six built-in apps offering functions such as sessions, management for images and authentication.

For this project, we will create a website discussion board. First, go to the manage.py file in your directory and run this command:

That’s it! You have successfully created your first app. After you add the discussion board app to your list of installed apps, it will look like this:

Creating a Page in Django CMS

Now, write the first view, or page, in Django. A view is a  function that will get an HttpResponse object and then return an object. Thus, the basic flow is receiving a parameter request and getting a response as a result. Go inside your boards app and add this code:

In this case, we have defined a view named “home” to return the message, “Hello!” 

Although we have the view, Django CMS does not know when to serve it. Use a urls.py file to run the code:

Django uses regex to match the URL you have requested, which is http://127.0.0.1:8000. This matches an empty path to the homepage. However, you could also make the URL http://127.0.0.1:8000/homepage. You would put this code above:

To ensure that your page is working, run this URL: http://127.0.0.1:8000. You have now created your first view using Python and Django!

Closing Thoughts

Throughout this tutorial, you have learned to install Python, work with a virtual environment and start your first Django project. If it has inspired you to learn more, consider enrolling in a data boot camp.

*Please note, these articles are for educational purposes and the topics covered may not be representative of the curriculum covered in our boot camp. Explore our curriculum to see what you’ll learn in our program.

Get Program Info

Back
Back
Back
Back
Back
Back
Back
Back
Back
0%

Step 1 of 6


Ready to learn more about Berkeley Data Analytics Boot Camp in San Francisco? Contact an admissions advisor at (510) 306-1218.