Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Ladies & Gentlemen, I present you the Car Shop owned by billionaire Vlatco Spasev.
- # models.py and admin.py are a bit underpowered but the show must go on!
- ----------------------------models.py------------------------------------
- from django.db import models
- from django.contrib.auth.models import User
- # Create your models here.
- class Manufacturer(models.Model):
- name = models.CharField(max_length=50)
- user = models.ForeignKey(User, on_delete=models.CASCADE)
- year = models.IntegerField()
- number_of_workers = models.IntegerField()
- def __str__(self):
- return self.name
- class Car(models.Model):
- CAR_TYPE = [
- ("SE", "Sedan"),
- ("SU", "SUV"),
- ("HB", "Hatchback"),
- ("CO", "Coupe"),
- ]
- #sedan, SUV, hatchback, coupe
- manifacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
- chassis_number = models.IntegerField()
- price = models.IntegerField()
- model = models.CharField(max_length=50)
- color = models.CharField(max_length=50)
- year = models.IntegerField()
- distance_travelled = models.IntegerField()
- type = models.CharField(max_length=3, choices=CAR_TYPE)
- image = models.ImageField(upload_to='cars/',null=True,blank=True)
- ----------------------------admin.py------------------------------------
- from django.contrib import admin
- from .models import Car,Manufacturer
- # Register your models here.
- class CarAdmin(admin.ModelAdmin):
- pass
- admin.site.register(Car,CarAdmin)
- class ManifacturerAdmin(admin.ModelAdmin):
- def save_model(self, request, obj, form, change):
- obj.user = request.user
- super().save_model(request, obj, form, change)
- admin.site.register(Manufacturer,ManifacturerAdmin)
- ----------------------------settings.py------------------------------------
- import os
- from pathlib import Path
- INSTALLED_APPS = [
- 'django.contrib.admin',
- 'django.contrib.auth',
- 'django.contrib.contenttypes',
- 'django.contrib.sessions',
- 'django.contrib.messages',
- 'django.contrib.staticfiles',
- 'CarsApp'
- ]
- STATIC_URL = 'static/'
- MEDIA_ROOT = os.path.join(BASE_DIR,'media/')
- MEDIA_URL = '/media/'
- ----------------------------urls.py------------------------------------
- from django.contrib import admin
- from django.urls import path
- from django.conf.urls.static import static
- from django.conf import settings
- from CarsApp.views import index, newProduct, details, editProduct, deleteProduct
- urlpatterns = [
- path('admin/', admin.site.urls),
- path('index/', index, name='index.html',),
- path('newProduct/', newProduct, name='newProduct.html'),
- path('details/<int:pk>/',details, name='details.html' ),
- path('editProduct/<int:pk>/', editProduct, name='editProduct.html'),
- path('deleteProduct/<int:pk>/', deleteProduct, name='deleteProduct.html'),
- ] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
- ----------------------------views.py------------------------------------
- from django.shortcuts import render, redirect
- from .models import Car
- from .forms import CarForm
- from django.shortcuts import get_object_or_404
- # Create your views here.
- def index(request):
- # if request.method=="POST":
- # form = CarForm(request.POST, request.FILES)
- # if form.is_valid():
- # car = form.save(commit=False)
- # car.image = form.cleaned_data['image']
- # car.save()
- # return redirect("index.html")
- # else:
- queryset = Car.objects.all()
- context = {'cars': queryset,}
- # 'form': CarForm,
- return render(request,'index.html', context=context)
- def newProduct(request):
- if request.method=="POST":
- form = CarForm(request.POST, request.FILES)
- if form.is_valid():
- car = form.save(commit=False)
- car.image = form.cleaned_data['image']
- car.save()
- return redirect("index.html")
- else:
- # queryset = Car.objects.all()
- context = { 'form': CarForm, }
- return render(request,'newProduct.html',context= context)
- def details(request, pk):
- car = get_object_or_404(Car,pk=pk)
- return render(request,'details.html',{'car':car})
- def editProduct(request, pk):
- car = get_object_or_404(Car, pk=pk)
- if request.method=="POST":
- form = CarForm(request.POST, request.FILES, instance=car)
- if form.is_valid():
- form.save()
- return redirect("index.html")
- else:
- form = CarForm(instance=car)
- return render(request,'editProduct.html', {'form': form})
- def deleteProduct(request, pk):
- car = get_object_or_404(Car, pk=pk)
- if request.method=="POST":
- car.delete()
- return redirect("index.html")
- else:
- return render(request,'deleteProduct.html',{'car': car})
- ----------------------------forms.py------------------------------------
- from django import forms
- from .models import Car
- class CarForm(forms.ModelForm):
- def __init__(self, *args, **kwargs):
- super(CarForm, self).__init__(*args, **kwargs)
- for field in self.visible_fields():
- field.field.widget.attrs['class'] = "form-control"
- class Meta:
- model = Car
- exclude = ['user',]
- ----------------------------templates------------------------------------
- #import the bootstrap framework in each .html file's head tag:
- <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
- <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
- ----------------------------index.html------------------------------------
- <body>
- <nav class="navbar bg-primary" data-bs-theme="dark">
- <div class="container-fluid">
- <a class="navbar-brand " href="#" style="border: black">Car Shop</a>
- <a class="navbar-brand " href="#">Home</a>
- <a class="navbar-brand " href="{% url 'newProduct.html' %}" style="border: black">Add New Product</a>
- </div>
- </nav>
- <h1 style="text-align: center;">ALL CARS</h1>
- <div class="container" style="padding-top: 40px; background-color: whitesmoke; box-shadow: black;">
- <div class="row">
- {% for car in cars %}
- <div class="col-md-3">
- <div class="card" style="width: 18rem;">
- <img src= "{{ MEDIA_URL }} {{ car.image.url }}" alt="...">
- <div class="card-body">
- <h4 class="card-title"><b> {{car.manifacturer}} {{car.model}}</b></h4>
- <h5 class="card-title">{{car.manifacturer}}</h5>
- <h5 class="card-title">{{car.distance_travelled}} km</h5>
- <h5 class="card-title">{{car.price}} $</h5>
- <button><a href="{% url 'details.html' car.pk %}" class="btn btn">Details</a></button>
- <button><a href="{% url 'editProduct.html' car.pk %}" class="btn btn-primary">Edit</a></button>
- <button><a href="{% url 'deleteProduct.html' car.pk %}" class="btn btn-danger" ">Delete</a></button>
- </div>
- </div>
- </div>
- {% endfor %}
- </div>
- </div>
- </body>
- ----------------------------newProduct.html------------------------------------
- <body>
- <nav class="navbar bg-primary" data-bs-theme="dark">
- <div class="container-fluid">
- <a class="navbar-brand " href="#">Car Shop</a>
- <a class="navbar-brand" href={% url 'index.html' %}>Home</a>
- <a class="navbar-brand" href="#">Add New Product</a>
- </div>
- </nav>
- <div class="container" style="padding-top: 40px; background-color: whitesmoke; box-shadow: black;">
- <form method="post" enctype="multipart/form-data">
- {% csrf_token %}
- {{ form.as_p }}
- <button class="btn btn-primary" type="submit">Submit</button>
- </form>
- </div>
- </body>
- ----------------------------details.html------------------------------------
- <body>
- <nav class="navbar bg-primary" data-bs-theme="dark">
- <div class="container-fluid">
- <a class="navbar-brand " href="#">Car Shop</a>
- <a class="navbar-brand" href={% url 'index.html' %}>Home</a>
- <a class="navbar-brand" href="{% url 'newProduct.html' %}">Add New Product</a>
- </div>
- </nav>
- <h1 style="text-align: center;">Details for car {{ car.pk }}</h1>
- <div class="container" style="padding-top: 40px; background-color: whitesmoke; box-shadow: black;">
- <div class="row">
- <div class="col-md-6">
- <div style="width: 18rem;">
- <img src= "{{ MEDIA_URL }} {{ car.image.url }}" alt="..." style="width: 600px; height: 450px">
- </div>
- </div>
- <div class="col-md-6">
- <div style="width: 18rem;">
- <div>
- <h4><b> {{car.manifacturer}} {{car.model}}</b></h4>
- <h5>Manifacturor: {{car.manifacturer}}</h5>
- <h5>Chassis Number: {{car.chassis_number}}</h5>
- <h5>Color: {{car.price}}</h5>
- <h5>Year of birth: {{car.price}}</h5>
- <h5>Distance travelled: {{car.price}} km</h5>
- <h5>Type: {{car.get_type_display}}</h5>
- <h5>Price: {{car.price}} $</h5>
- <a href="{% url 'index.html' %}" class="btn btn-success" >Back</a>
- </div>
- </div>
- </div>
- </div>
- </div>
- </body>
- ----------------------------editProduct.html------------------------------------
- <body>
- <h2 style="text-align: center">Edit the Car {{ car.pk }}</h2>
- <div class="container" style="padding-top: 40px; background-color: whitesmoke; box-shadow: black;">
- <form method="post" enctype="multipart/form-data">
- {% csrf_token %}
- {{ form.as_p }}
- <button class="btn btn-primary" type="submit">Submit</button>
- </form>
- </div>
- </body>
- ----------------------------deleteProduct.html------------------------------------
- <body>
- <h2 style="text-align: center">Are you sure you want to delete this car?</h2>
- <div class="container" style="padding-top: 40px; background-color: whitesmoke; box-shadow: black;">
- <form method="post">
- {% csrf_token %}
- <button style="horiz-align: center" class="btn btn-danger" type="submit">Submit</button>
- <a href="{% url 'index.html' %}" class="btn btn" >Cancel</a>
- </form>
- </div>
- </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement