Professional Documents
Culture Documents
Experiment-05 Full Stack Django Search Creators
Experiment-05 Full Stack Django Search Creators
Experiment-05
Develop a simple Django app that displays an unordered list of fruits and ordered list of
Step-01: This app will be created in the Django project we made earlier.
Locate the INSTALLED_APPS list and add the name of your new app to the list:
def home(request):
fruits = ['Apple', 'Banana', 'Orange', 'Mango', 'Pineapple']
students = ['John', 'Jane', 'Mike', 'Sarah', 'Tom']
context = {
'fruits': fruits,
'students': students,
}
return render(request, 'home.html', context)
• Here, we define a view function home that creates two lists: fruits and students. These
lists are then passed to the template as a context dictionary.
• In your app's directory (e.g., listfruitapp), create a new folder named templates (if it
doesn't already exist).
• Inside the templates folder, create another folder with the same name as your app (e.g.,
listfruitapp).
• Inside the fourdate_timeapp folder, create a new file named home.html.
• Open home.html and add the following code.
<!DOCTYPE html>
<html>
<head>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
rel="stylesheet">
<style>
html, body {
height: 100%;
body {
display: flex;
justify-content: center;
align-items: center;
.container {
text-align: center;
.list-container {
display: inline-block;
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col">
<h1>Fruits</h1>
{% endfor %}
</ul>
</div>
<div class="col">
<h1>Selected Students</h1>
{% endfor %}
</ol>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
• In this template, we use Django's template tags to loop through the fruits and students
lists and render them as an unordered list and an ordered list, respectively.
• Open the urls.py file in your Django app's directory (e.g., listfruitapp/urls.py).
• Import the view function at the top of the file
• Add a new URL pattern to the urlpatterns list
urlpatterns = [
path('', include(listfruitapp.urls')),
• This includes the URL patterns from your app's urls.py file.
• In the VS Code terminal, navigate to your Django project's directory (the one containing
manage.py).
• Run the development server