60
loading...
This website collects cookies to deliver better user experience
mkdir django_flutterwave && cd django_flutterwave
virtualenv env
source env/bin/activate
pip install django requests django-environ Pillow
django-admin startproject django_store .
python manage.py startapp electronics
settings.py
.electronics/models.py
and add the following code to it:from django.db import models
# Create your models here.
class Product(models.Model):
name=models.CharField(max_length=250)
image=models.ImageField(upload_to='product_images')
price=models.FloatField()
def __str__(self) -> str:
return str(self.name)
#Register it in admin
from django.contrib import admin
admin.site.register(Product)
makemigrations
and finally migrate
commands to register our database table. runserver
command.createsuperuser
command and login to (http://localhost:8000/admin/) and add a couple of electronic products. settings.py
to reflect this. At the bottom of the file add the following code:MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py
and add the following code:from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
DIRS
part:os.path.join(BASE_DIR, 'templates')
views.py
.from django.shortcuts import render
from .models import Product
# Create your views here.
def list_products(request):
products=Product.objects.all()
ctx={
'products':products
}
return render(request, 'products.html', ctx)
urls.py
like so:from os import name
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from electronics.views import list_products
urlpatterns = [
path('admin/', admin.site.urls),
path('', list_products, name='list'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)