Curso Django. Parámetros en URL. Vídeo 4
Introduction to Passing Parameters in Django URLs
In this video, the instructor introduces the concept of passing parameters through URLs in Django. This is a common requirement when working with dynamic websites. The instructor also mentions that they will cover how to display dynamic content and address concerns about working with HTML documents.
Passing Parameters in URL
- To pass parameters through a URL in Django, it is necessary to work with the
views.pyandurls.pyfiles.
- The instructor demonstrates how to start the server using either the system console or Visual Studio Code's integrated terminal.
- After starting the server, the instructor suggests checking if the webpage from previous videos is displayed correctly.
- It is possible to change the format of text by adding HTML tags within the response parameter of
HttpResponse.
- For example, adding
<h1>tags can create a heading for the text.
- It is recommended to store such HTML code as a variable for better organization.
Displaying Dynamic Content
- The instructor explains that displaying static content involves showing strings of characters without any dynamic elements.
- To demonstrate dynamic content, an example of displaying current date and time on a webpage is introduced.
- A new view function called "dame_fecha" (Spanish for "give me date") is created in
views.py.
- The
datetimemodule is imported to access functions related to date and time.
- Importing modules before using their functions helps with code completion in Visual Studio Code.
- The current date and time are stored in a variable called "fecha_actual" (Spanish for "current date").
- Similar to before, HTML tags can be used within the response parameter of
HttpResponseto format text.
- A placeholder is added where the current date and time should be displayed.
Creating Dynamic Content: Displaying Current Date and Time
In this section, the instructor demonstrates how to create dynamic content by displaying the current date and time on a webpage using Django.
Creating a View for Current Date and Time
- A new view function called "dame_fecha" is created in
views.py.
- The
datetimemodule is imported to access functions related to date and time.
- The current date and time are stored in a variable called "fecha_actual".
- HTML tags can be used within the response parameter of
HttpResponseto format text.
- A placeholder is added where the current date and time should be displayed.
Conclusion
In this final section, the instructor concludes the video by summarizing the concepts covered.
Summary
- Passing parameters through URLs in Django involves working with
views.pyandurls.py.
- Static content can be displayed by adding HTML tags within the response parameter of
HttpResponse.
- Dynamic content can be created by storing relevant data in variables and using HTML tags for formatting.
- An example of displaying current date and time on a webpage was demonstrated.
The transcript provided does not cover all sections of the video.
Fixing a small error
The speaker mentions that there was a small error in the code due to having two percentage symbols instead of one. They mention that they will fix this issue.
Fixing the error
- There was an error in the code with two percentage symbols.
- The speaker will correct this by removing one of the percentage symbols.
Creating a URL for a new view
The speaker explains how to create a URL for a new view in Django.
Creating the URL
- In the urls.py file, add a new path for the desired URL.
- Use a simple and descriptive URL, such as "fecha/".
- Point this URL to the newly created view.
Importing the new view
The speaker demonstrates how to import the newly created view into Django.
Importing the view
- In the urls.py file, import the newly created view using
import dame_fecha.
- This allows Django to recognize and use the new view.
Testing the changes
The speaker suggests testing the changes made so far by accessing the new URL in a web browser.
Testing
- Open a web browser and enter "fecha" instead of "saludo" in the URL.
- Verify that it displays "Fecha Ver Actuales" with an h2 heading.
- Make any necessary formatting adjustments and save changes if needed.
Introduction to passing parameters through URLs
The speaker introduces passing parameters through URLs in Django and explains its importance.
Passing parameters through URLs
- Passing parameters through URLs is a common practice in server-side technologies.
- Django simplifies this process and avoids using symbols like "?" commonly used in PHP.
Django's URL-friendly feature
The speaker explains Django's URL-friendly feature and how it simplifies URLs for better search engine optimization.
URL-friendly feature
- Django uses simple and clean URLs to ensure proper indexing by search engines.
- It avoids using symbols like "?" that can cause issues with SEO.
- While similar naming conventions can be used, it is recommended to follow Django's guidelines.
Example of passing parameters through the URL
The speaker provides an example of passing parameters through the URL in Django.
Creating a simple example
- Create a view that calculates the age a person will have in a future year.
- The view will receive a parameter named "year" indicating the future year.
- Start by assuming the person's current age is 18.
Creating the view for calculating age
The speaker demonstrates how to create a view that calculates someone's age based on a given year.
Creating the view
- Create a new view called "calcula_edad" in views.py.
- This view will receive two parameters, "request" and "year".
- Set an initial value for the variable "edad_actual" (current age), such as 18.
Calculating future age based on input year
The speaker explains how to calculate someone's future age based on the input year parameter.
Calculating future age
- Create another variable called "periodo", which represents the difference between the input year and 2019.
- This calculates the number of years that have passed since 2019.
- Create a variable called "edad_futura" by adding "edad_actual" and "periodo".
- This gives the future age based on the input year.
Creating HTML response for displaying results
The speaker demonstrates how to create an HTML response to display the calculated age.
Creating HTML response
- Create a variable called "documento" to store the HTML content.
- Use basic HTML tags like
<body>and<h2>for formatting.
- Include placeholders for displaying the input year and calculated age using percentage symbols.
Formatting placeholders in HTML response
The speaker explains how to format placeholders in the HTML response to display dynamic values.
Formatting placeholders
- Use percentage symbols followed by numbers (e.g.,
%s) as placeholders in Python strings.
- Replace these placeholders with actual values using the
%operator.
- In this case, replace
%swith the input year and calculated age.
Configuring URL patterns
The speaker explains how to configure URL patterns in Django to include parameters.
Configuring URL patterns
- Open urls.py file and add parentheses after
path()function call.
- Inside these parentheses, specify which part of the URL will contain a parameter.
- In this case, specify that "year" is a parameter by enclosing it in angle brackets (
< >).
Passing Parameters in Django URLs
In this section, the speaker explains how to pass parameters in Django URLs and how to indicate which function should be called.
Importing and Configuring the URL Path
- To pass a parameter in the URL, convert it to an integer by adding
int:before the parameter.
- Import the function that will handle the URL path.
- Use the
pathfunction to specify the URL pattern and point it to the corresponding function.
Simplification with Django 2.0
- Before Django 2.0, regular expressions were used instead of
pathfor handling parameters, making it more complex.
- The introduction of
pathsimplifies parameter handling by limiting the use of regular expressions.
Testing Parameter Passing
- Save changes and test if passing parameters works correctly.
- Enter
/ages/followed by a year in the browser's address bar to calculate someone's age for that year.
Passing Multiple Parameters in Django URLs
This section covers how to pass multiple parameters through Django URLs.
Preparing URL Path for Multiple Parameters
- Modify the path in
urls.pyto include another parameter named "age" separated by a comma from "year".
Modifying View Function
- Update the view function to receive two parameters: "age" and "year".
- Calculate future age based on these parameters.
Summary
This section provides a summary of passing parameters in Django URLs and passing multiple parameters.
- To pass a parameter in a Django URL, convert it to an integer using
int:before the parameter name.
- In earlier versions of Django, regular expressions were used instead of
path, making parameter handling more complex.
- With Django 2.0, using
pathsimplifies parameter handling.
- To pass multiple parameters, modify the URL path in
urls.pyand update the view function to receive and process these parameters.
The transcript provided does not cover all the content of the video.