0% found this document useful (0 votes)
13 views12 pages

App Development Django

DJANGO

Uploaded by

Ginzil Haman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views12 pages

App Development Django

DJANGO

Uploaded by

Ginzil Haman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

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.

How to verify if django is installed

Type this on the terminal – python

>>import djang0

>>print(django.get_version())

Setting up Environment
Virtual environment

1. Python3 –m venv env/or pip install virtualenv ====virtualenv env


2. . env/scripts/activate /or
3. Pip install django
4. django-admin startproject ( paddle’can put anything’)
5. cd “project”
6. python manage.py runserver

Creating App

Called “farm”

python manage.py startapp farm

Now telling django that the app exist

Settings.py -- Installed app ( add - ‘farm’)

Now move to views.py

def index(request): #get or post request

return render(request,'farm/index.html')

now we create the template


farm--newfolder( Templatenew folder(farmnew file(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>

<div class="px-6 py-6">


<h1>The front page</h1>
</div>
</body>
</html>

Now we connect to urls.py

From farm.views import index

from django.contrib import admin


from django.urls import path
from farm.views import index

urlpatterns = [
path('', index, name='index'),
path('admin/', admin.site.urls),
]

REUSING CODES FOR MORE TEMPLATES USING BASE.HTML

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>

<div class="px-6 py-6">


{% block content %}

Creating a navigating bar with items like log in and sign up

<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>

<a href="#" class="px-6 py-3 text-lg font-semibold bg-teal-


500 text-white rounded-xl hover:bg-teal-700">Sign up</a>
<a href="#" class="px-6 py-3 text-lg font-semibold bg-gray-
500 text-white rounded-xl hover:bg-gray-700">Log in</a>
</nav>
Footer

<footer class="py-6 px-6 flex justify-between bg-gray-800">


<div class="w-2/3 pr-10">
<h3 class="mb-5 font-semibold text-gray-400">About</h3>

<p class="text-lg text-gray-500">Lorem


wjekeiowinonwnwchuwqqhfnugnqxngfynr7gnxgfngg7pnayaggaf.</p>
</div>

<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>

Categories and items .

Here we will need 1 more django app for us to work on categories and item

python manage.py startapp item

we now want both category and item to be located in this app so we need to register this with
django

Go to installed apps and add it


We start with category because it is the simplest database model.

Go to item-model.py-- here we create new database model called class category

Here -class Category(models.Model):

name = models.CharField(max_length=255)

Now we update the database by going to the command line

python manage.py makemigrations

after this we now tell django to execute the script-python manage.py migrate

S C:\Users\Haman\Paddle\paddle> python manage.py migrate

Operations to perform:

Apply all migrations: admin, auth, contenttypes, item, sessions

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

Now it has been created

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

python manage.py createsuperuser

everything will workout from there

lets start server again

python manage.py runserver

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

GO to ITEM -- admin.py

from .models import Category

# Register your models here.

admin.site.register(Category)

now it will work perfectly


But how do we deal with categorys error and category object 1 error

Go to model.py and add

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)

then go to command center

ully installed Pillow-10.2.0

PS C:\Users\Haman\Paddle\paddle> python manage.py makemigrations

Migrations for 'item':

item\migrations\0002_alter_category_options_item.py

- Change Meta options on category

- Create model Item

PS C:\Users\Haman\Paddle\paddle> python manage.py migrate

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>

AFTER THIS NOW GO TO ADMIN.PY AND REGISTER IT

from .models import Category, Item

# Register your models here.

admin.site.register(Category)
admin.site.register(Item)

Go to settings.py and add the ones for media

MEDIA_URL = ‘media/’

MEDIA_ROOT = BASE_DIR / ‘me’

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

After this go farm and on the views you add

from django.shortcuts import render

from item.models import Category, Item

# 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')

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,
})

We set in categorically below New Items


<div class="mt-6 px-6 py-12 bg-gray-100 rounded-xl">
<h2 class="mb-12 text-2xl text-center">Categories</h2>

<div class="grid grid-cols-3 gap-3">


{% for category in categories %}

<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 title %}{{ items.name }}{{% endblock %}}

{% block content %}
<div class="grid grid-cols-5 gap-6">
<div class="col-span-3">

<img src="{{ item.image.url }}" class="rounded-xl">


</div>
</div>
{% endbloc %}

urls

from django.conf import settings


from django.conf.urls.static import static

from django.contrib import admin


from django.urls import path, include
from farm.views import index, contact

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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy