Curso Django. Creación de primera página. Vídeo 3
Introduction to Django and Creating the First Page
In this section, we will learn about creating our first page in Django. We will focus on setting up the view and configuring the URL.
Creating the First View and Configuring the URL
- The first step is to create a view that will handle the request for our first page.
- We will not connect to any databases or create templates for this simple "Hello World" page.
- The view is responsible for receiving the request and sending back a response.
- We will work with two important objects:
request(for handling incoming requests) andhttp response(for sending back responses).
- These objects are defined in the
django.httpmodule, so we need to import it in our views file.
Setting Up Visual Studio Code
- Visual Studio Code is used as an example editor in this course, but you can use any editor of your choice.
- It is recommended to visit the official Django documentation website for more information on using Visual Studio Code or any other editor.
- Create a workspace in Visual Studio Code and store your project folder inside it.
Creating a Views File
- In your project folder, create a new file called
views.py.
- This file will contain all the views for your project.
- Use either the "Find File" menu or shortcuts provided by Visual Studio Code to create this file quickly.
Writing Our First View
- Declare a Python function inside
views.py, which will serve as our first view.
- You can name it anything you like; here, we'll call it
saludo.
- This function should take one parameter, which is conventionally named
request.
- The purpose of this function is to return a response containing some text. For example, "Hello students! This is our first page with Django."
Configuring the URL
- We need to link a URL to our view so that when we enter that URL in the browser, it will display our desired page.
- Save the changes made in
views.py.
- The URL configuration is done in another file called
urls.py, which is located in your project folder.
- Open
urls.pyand add a new line of code to associate a URL pattern with our view.
- This can be done using regular expressions or simple string matching.
Conclusion
In this section, we learned how to create our first page in Django. We set up the view, configured the URL, and created a basic response.
Setting Up URLs in Django
In this section, the speaker explains how to set up URLs in Django by defining views and linking them with specific URLs.
Defining URL Paths and Views
- To set up URLs in Django, import the necessary modules and define a table of paths.
- Inside the table, specify the URL path using quotes and assign it a name (e.g., "saludo").
- Associate the URL with a view function (e.g., "saludo") that will be called when the URL is accessed.
- Import the view function from another module if it is defined in a different file.
Testing the Setup
- Start the development server by running
python manage.py runserver.
- Enter
localhost:8000/saludoin the browser to access the defined URL.
- The browser should display an HTML document with the text returned by the corresponding view function.
Naming URLs in Django
This section explains that URLs do not have to be named after their corresponding view functions and demonstrates how to give them custom names.
Customizing URL Names
- The name of a URL does not have to match its associated view function's name.
- Use any desired name for clarity and coherence.
- Assign a custom name to a URL by specifying it as an argument after the path definition (e.g.,
path('saludo', saludo, name='custom_name')).
Importing Functions from Other Modules
This section highlights that when using functions defined in other modules, they need to be imported into your current module.
Importing Functions
- When using a function defined in another module, import it into your current module.
- Use the
importstatement followed by the module name and function name (e.g.,import saludo).
Creating Multiple Views in Django
This section demonstrates how to create multiple views in Django by defining additional view functions and linking them with new URLs.
Creating a Second View
- Define a new view function, such as "despedida", below the existing views in the views file.
- The new view function should also accept the
requestparameter.
- Inside the new view function, return an HTTP response object with desired content.
- Register a new URL that links to the newly created view function within the table of paths in urls.py.
Testing Multiple Views
This section explains how to test multiple views by accessing their respective URLs in a browser.
Accessing Different Views
- Start the development server if not already running.
- Enter different URLs, such as
localhost:8000/saludoandlocalhost:8000/despedida, in the browser's address bar.
- Each URL should display its corresponding HTML document or content returned by its associated view function.