Assignment Question Django
Assignment Question Django
1. Request Arrival:
o When a user makes an HTTP request (http://127.0.0.1:8000/hello/), Django’s
server receives the request.
2. Settings File Lookup:
o Django starts by looking for a settings file (setting.py) in the same directory
as the main script (manage.py).
o The settings file contains various configurations for the Django project,
including uppercase settings like TEMPLATE_DIRS, DATABASE_NAME etc.
3. URLconf Determination:
o The most crucial setting in the settings file is ROOT_URLCONF.ROOT_URLCONF
o ROOT_URLCONF.ROOT_URLCONF specifies which Python module should be used
as the URL configuration (URLconf) for this particular website.
4. URL Pattern Matching:
o Django loads the URL configuration (ROOT_URLCONF) pointed to by URLS.PY
o It examines each URL pattern in the URL configuration sequentially.
o For each pattern, Django compares the requested URL (e.g., /hello/) with the
pattern.
o If it finds a matching pattern, it proceeds to the associated view function.
5. View Function Invocation:
o When a matching pattern is found, Django calls the view function associated
with that pattern.
o The view function receives an HttpResponse object (representing the
request) as its first parameter.
o The view function processes the request (e.g., handling form submissions,
database queries, etc.).
6. Response Generation:
o The view function must return an HttpResponse (HTTP response).
o Once the view function returns the response, Django takes over.
o Django converts the Python object (the response) into a proper HTTP
response.
o This includes setting the appropriate HTTP headers and constructing the body
(content) of the web page.
7. Final Result:
o The properly formatted HTTP response is sent back to the client’s browser.
o The browser displays the web page based on the response received.
Assignment q3:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Assignment</title>
</head>
<body>
<h1>Table of {{num1}} from 1 to {{num2}}</h1>
{% for i in table%}
<p>{{i}}</p>
{%endfor %}
</body>
</html>
def Table(request,offset1,offset2):
table = []
table2=[]
for i in range(1, offset2+1):
result = offset1 i
table.append((offset1,i, result))
for i in table:
table2.append(f"{i[0]} {i[1]} = {i[2]}")
return render(request,
'assign.html',{'num1':offset1,'num2':offset2,'table':table2})
urlpatterns = [
path('table/<int:offset1>/<int:offset2>', views.Table,),
Assign q4
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Number of Vowels in {{word}} is {{num_vowels}}</h1>
{% for v in vowels_list%}
<p>{{v}}</p>
{% endfor %}
</body>
</html>
def Word(request,words):
vowels = "aeiouAEIOU"
consonants = []
vowel_list = []
num_vowels = len(vowel_list)
num_consonants = len(consonants)
return
render(request,'words.html',{'word':words,'num_vowels':num_vowels,'num_consona
nts':num_consonants,'consonants':consonants, 'vowels_list':vowel_list})
urlpatterns = [
path('table/<str:words>', views.Word),
]
from django.contrib import admin
from django.urls import path,include
from .import views
urlpatterns = [
path('',include('assignment.urls'))]
q5
1. Exception Details:
o At the top of the page, you’ll find essential information about the exception:
▪ The type of exception (e.g., “unsupported type” in this case).
▪ Any parameters associated with the exception.
▪ The file where the exception occurred.
▪ The specific line number that triggered the exception.
2. Python Traceback:
o Below the exception details, you’ll see a complete Python traceback.
o Each level (or “frame”) in the stack is displayed:
▪ File name.
▪ Function/method name.
▪ Line number.
▪ Source code snippet for that frame.
3. Source Code Context:
o Clicking on a source code line (in dark gray) reveals additional lines before
and after the erroneous line.
o This context helps you understand the surrounding code.
4. Local Variables:
o Under each frame, you can click “Local vars” to view local variables and their
values at the exact point of the exception.
o This debugging information is valuable for diagnosing issues.
5. Copy-and-Paste View:
o The “Switch to copy-and-paste view” option provides an alternate traceback
format that’s easy to copy and share.
6. Sharing Traceback:
o The “Share this traceback on a public Web site” button posts the traceback to a
public site (e.g., http://www.dpaste.com/).
o You’ll receive a distinct URL to share with others for technical support.
7. Request Information:
o The “Request information” section includes details about the incoming Web
request:
▪ GET and POST data.
▪ Cookie values.
▪ CGI headers.
8. Django Settings:
o The “Settings” section lists all settings for this Django installation.
o These settings impact the behavior of your application.
Q6 Method call something something
Method-call behavior in templates, such as in Django, is more intricate than other types of dot
lookups (like attribute access or dictionary key access). Understanding this behavior is crucial for
safely and effectively using template engines. Here's a detailed note on how method calls work in
templates and how they differ from other dot lookups.
1. Exception Handling:
- If a method raises an exception during lookup, the exception is propagated unless it has an
attribute `silent_variable_failure` set to `True`.
- If the exception does have the `silent_variable_failure` attribute, the variable will render as an
empty string.
Example:
```python
>>> p = PersonClass3()
...
AssertionError: foo
...
>>> p = PersonClass4()
```
2. Argument Requirements:
- A method call will only work if the method has no required arguments. If arguments are required,
the system will skip the method and move to the next lookup type (such as list-index lookup).
- Some methods have side effects (e.g., modifying data), which can pose security risks if executed
inadvertently through a template.
- For instance, a `delete()` method on a `BankAccount` object could potentially delete the account if
accessed through a template, which would be unsafe.
- To prevent the template system from executing methods that alter data, the method should have
the attribute `alters_data` set to `True`.
Example:
```python
def delete(self):
delete.alters_data = True
```
- If a method is marked with `alters_data=True`, the template system will not execute it. Instead, it
will fail silently if attempted to be accessed through the template.
```python
```
1. Attributes:
- Attribute lookups are straightforward; they directly access the value of the attribute without
execution.
- Example:
```python
{{ person.first_name }}
```
2. Dictionary Keys:
- Example:
```python
{{ my_dict.key }}
```
3. Methods:
- Method lookups involve calling the method and using the return value.
- Methods requiring arguments are skipped, while attributes and dictionary keys are always
accessed if they exist.
Q7
Q8