Creating Django Project With Example
Creating Django Project With Example
Install Virtualenv
Virtualenv is a tool to create isolated Python environments. It's not mandatory, but it's a good practice to
use it to avoid conflicts between different projects.
Install Django
With your virtual environment activated, you can now install Django using pip:
Example:
To create your app, make sure you’re in the same directory as manage.py and type this command:
Let’s write the first view. Open the file polls/views.py and put the following Python code in it:
polls/views.py
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
This is the simplest view possible in Django. To call the view, we need to map it to a URL - and for this we
need a URLconf.
polls/urls.py
urlpatterns = [
path('', views.index, name='index'),
]
The next step is to point the root URLconf at the polls.urls module
mysite/urls.py
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
Errors in Django
2. View Errors
Problems in view functions:
4. Database Errors
Issues with database interactions:
5. Server Errors
General server issues:
MODULE-2
Template-System Basics
A Django template is a string of text used to separate the presentation of a document from its data.
It includes placeholders and basic logic (template tags) to control how the document is displayed.
Templates are typically used to produce HTML but can generate any text-based format.
2. Example Template:
<!DOCTYPE html>
<html>
<head>
<title>Greeting Page</title>
</head>
<body>
<h1>Welcome! </h1>
<p>Hello, {{ user_name }}!</p>
<p>Today is {{current_date|date:"F j, Y”}}. </p>
{% if is_member %}
<p>Thanks for being a member! </p>
{% else %}
<p>Consider joining our membership program for exclusive benefits. </p>
{% endif %}
</body>
</html>
3. Variables:
4. Template Tags:
Surrounded by curly braces and percent signs, e.g., `{% if ordered_warranty %}`.
Direct the template system to perform actions, such as loops and conditionals.
Example tags:
`{% for item in item_list %}`: Loops over items.
`{% if ordered_warranty %}`: Conditional logic.
5. Filters:
Modify the formatting of variables.
Example: `{{ship_date|date:"F j, Y" }}` uses the `date` filter to format `ship_date`.
Filters are attached using a pipe character (`|`).
Filters
Filters in Django templates are used to modify the value of a variable before it gets displayed. They are
simple functions that take an input value and return a modified output. Filters are attached to variables
using a pipe character (`|`).
Common Filters:
Tags
Tags in Django templates provide logic and control flow capabilities. They are more complex than filters and
can do things like loop through data, perform conditional operations, and more. Tags are surrounded by `{%
%}`.
Common Tags:
2. `if`: Evaluates a condition and includes the enclosed template code only if the condition is true.
```html
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}!</p>
{% else %}
<p>Please log in.</p>
{% endif %}
```
- Checks if `user.is_authenticated` is true and displays the appropriate message.
3. `block` and `endblock`: Used in template inheritance to define blocks of content that child templates can
override.
```html
{% block content %}
<p>This is the default content.</p>
{% endblock %}
```
- Defines a block of content that can be replaced in child templates.
To create your app, make sure you’re in the same directory as manage.py and type this command:
polls/views.py
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
This is the simplest view possible in Django. To call the view, we need to map it to a URL - and for this we
need a URLconf.
polls/urls.py
urlpatterns = [
path('', views.index, name='index'),
]
The next step is to point the root URLconf at the polls.urls module
mysite/urls.py
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
Program DateTime:
1. Develop a Django app that displays current date and time in server.
Step 1: Create a Django App named myapp.
Step 2: Define a View: Open the views.py file inside your myapp directory and define a view to display
the current date and time.
from django.http
import HttpResponse from datetime import datetime
def current_datetime(request):
now = datetime.now()
html = f"<html>
<body>
<h1>Current Date and Time:</h1>
<p>{now}</p>
</body>
</html>"
return HttpResponse(html)
Step 3: Create a URL Configuration: Open the urls.py file inside your myapp directory and create a URL
configuration to map the view you just defined to a URL.
Step 4: Update Project URL Configuration: Open the urls.py file inside your project directory
from django.contrib import admin
from django.urls import path, include # Import include