4. Django views
A Django view function, or view for short, is a Python function that takes a Web request and returns a Web response.
This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image … or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response.
This code can live anywhere you want, but the convention is to put views in a file called views.py, placed in your project or application directory.
Django uses request and response objects to pass state through the system.
When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function. Each view is responsible for returning an HttpResponse object. More information on these at https://docs.djangoproject.com/en/3.0/ref/request-response/
This is a very simple views.py file which returns some html.
from django.http import HttpResponse def simple_view(request): html = "Hello world!" return HttpResponse(html)
Let’s edit our little simple view so that we can access the IP address of the requesting client and show it to the user.
We want the HTTP request header data and that’s stored in an object called meta within the request object, and then from the header we’ll retrieve the IP address, which is a dict with a key called remote address. Then if we want to show remote address to our user, we can edit our little toy HTML Document, “Your IP address is,”
def simple_view(request): header = request.META ip = header['REMOTE_ADDR'] html = "Your IP Address is: "+ip+"" return HttpResponse(html, content_type='text/html')
Wednesday 20 October 2021, 765 views
Next post: 5. Django models Previous post: 3. Creating a new hello app
Advanced Web Development index
- 38. Writing API tests
- 37. Testing in Django
- 36. Class-based views in the Django REST framework
- 35. Building a RESTful web service in Django
- 34. Introduction to CRUD, REST and APIs
- 33. Refactoring with generic views in Django
- 32. Django validators
- 31. Django forms (2) – using the ModelForm class
- 30. Django forms (1)
- 29. JavaScript basics
- 28. Adding CSS to the template
- 27. Django templating
- 26. Deleting and updating records
- 25. Joins, filters and chaining commands
- 24. Using the ORM in views.py
- 23. Adding to the database by writing a script
- 22. Adding to the database with Django Admin
- 21. Migrations
- 20. ORM – work through example
- 19. An introduction to the Object-Relational Mapper
- 18. Altering the database
- 17. SQL functions and summaries
- 16. SQL Query performance
- 15. Queries and table joins in SQL
- 14. Inserts and queries in SQL
- 13. Good practice in relational database design
- 12. Limitations to database modelling
- 11. Building a database using SQL
- 10. Introduction to PostgreSQL
- 9. How to start writing a new application in Django
- 8. Building a lightweight project
- 7. Django URLs
- 6. Django templates
- 5. Django models
- 4. Django views
- 3. Creating a new hello app
- 2. Creating a new virtual environment
- 1. Setting up Django
Leave a Reply