App Development Django
App Development Django
1164125607
INSTALL PYTHON
Setting up database
Set up database when dealing with large database engine like MySQL, PostgreSQL, or Oracle,
caunsault django documentation on how to do that.
>>import djang0
>>print(django.get_version())
Setting up Environment
Virtual environment
Creating App
Called “farm”
return render(request,'farm/index.html')
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
<title>Paddle</title>
</head>
<body>
urlpatterns = [
path('', index, name='index'),
path('admin/', admin.site.urls),
]
Block content
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
<title>{% block title%}{%endblock%} | Paddle</title>
</head>
<body>
<nav class="py-6 px-6 flex justify-between items-center border-b
border-grey-200">
<a href="/" class="text-xl font-semibold">Farm</a>
<div class="space-x-6">
<a href="#" class="text-lg font-semibold hover:text-gray-
500">New item</a>
</nav>
<body>
<nav class="py-6 px-6 flex justify-between items-center border-b
border-grey-200">
<a href="/" class="text-xl font-semibold">Farm</a>
<div class="space-x-6">
<a href="#" class="text-lg font-semibold hover:text-gray-
500">New item</a>
<a href="#" class="text-lg font-semibold hover:text-gray-
500">Browse</a>
<div class="w-1/3">
<h3 class="mb-5 font-semibold text-gray-400">Menu</h3>
<ul class="space-y-2">
<li><a href="#" class="text-lg text-teal-500
hover:text-teal-700">About</a></li>
<li><a href="#" class="text-lg text-teal-500
hover:text-teal-700">Contact</a></li>
<li><a href="#" class="text-lg text-teal-500
hover:text-teal-700">Privacy policy</a></li>
<li><a href="#" class="text-lg text-teal-500
hover:text-teal-700">Term of use</a></li>
</ul>
</div>
</footer>
Here we will need 1 more django app for us to work on categories and item
we now want both category and item to be located in this app so we need to register this with
django
name = models.CharField(max_length=255)
after this we now tell django to execute the script-python manage.py migrate
Operations to perform:
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying item.0001_initial... OK
Applying sessions.0001_initial... OK
To make it possible to add data or categories, we need to log in as admin interface. To do this we
need a user so lets create one
We will notice that when we log into admin the category will not be there even though the
database should have it already but we just need to tell django that we want database table show
in the admin instaface
admin.site.register(Category)
In models.py
class Meta:
verbose_name_plural = 'Categories'
def __str__(self):
return self.name
WE now go to another database model which is item model
class Item(models.Model):
category = models.ForeignKey(Category, related_name='items',
on_delete=models.CASCADE)
name = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
price = models.FloatField()
image = models.ImageField(upload_to='item_images',blank=True,
null=True)
is_sold = models.BooleanField(default=False)
created_by = models.ForeignKey(User, related_name='items',
on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
item\migrations\0002_alter_category_options_item.py
Operations to perform:
Apply all migrations: admin, auth, contenttypes, item, sessions
Running migrations:
Applying item.0002_alter_category_options_item... OK
PS C:\Users\Haman\Paddle\paddle>
admin.site.register(Category)
admin.site.register(Item)
MEDIA_URL = ‘media/’
HERE NOW WE ADD BACKEND TO THE FRONT END , WE DO LINK THEM UP..AND INORDER TO DO
THAT WE ADD CODES TO THE VIEWS
def contact(request):
return render(request, 'farm/contact.html')
# Create your views here.
def index(request): #get or post request
items = Item.objects.filter(is_sold=False)[0:6]
categories = Category.objects.all()
return render(request,'farm/index.html', {
'categories': categories,
'items': items,
})
<div>
<a href="#">
<div class ="p-6 bg-white rounded-b-xl">
<h2 class="text-2xl">{{ category.name }}</h2>
<p class="text-gray-500">{{
category.items.count }} items</p>
</div>
</a>
</div>
{% endfor %}
</div>
</div>
Detail.html where you give so much detail then go to urls after which you now You go to views
{% extends 'core/base.html' %}
{% block content %}
<div class="grid grid-cols-5 gap-6">
<div class="col-span-3">
urls
urlpatterns = [
path('', index, name='index'),
path('items/', include('item.urls')),
path('contact/', contact, name='contact'),
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)