Advertisement
dzocesrce

[DNICK] Car Shop

Jun 6th, 2024
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.38 KB | None | 0 0
  1. # Ladies & Gentlemen, I present you the Car Shop owned by billionaire Vlatco Spasev.
  2. # models.py and admin.py are a bit underpowered but the show must go on!
  3.  
  4. ----------------------------models.py------------------------------------
  5. from django.db import models
  6. from django.contrib.auth.models import User
  7.  
  8. # Create your models here.
  9.  
  10. class Manufacturer(models.Model):
  11.     name = models.CharField(max_length=50)
  12.     user = models.ForeignKey(User, on_delete=models.CASCADE)
  13.     year = models.IntegerField()
  14.     number_of_workers = models.IntegerField()
  15.  
  16.     def __str__(self):
  17.         return self.name
  18. class Car(models.Model):
  19.     CAR_TYPE = [
  20.         ("SE", "Sedan"),
  21.         ("SU", "SUV"),
  22.         ("HB", "Hatchback"),
  23.         ("CO", "Coupe"),
  24.     ]
  25.     #sedan, SUV, hatchback, coupe
  26.     manifacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
  27.     chassis_number = models.IntegerField()
  28.     price = models.IntegerField()
  29.     model = models.CharField(max_length=50)
  30.     color = models.CharField(max_length=50)
  31.     year = models.IntegerField()
  32.     distance_travelled = models.IntegerField()
  33.     type = models.CharField(max_length=3, choices=CAR_TYPE)
  34.     image = models.ImageField(upload_to='cars/',null=True,blank=True)  
  35.    
  36. ----------------------------admin.py------------------------------------
  37. from django.contrib import admin
  38. from .models import Car,Manufacturer
  39. # Register your models here.
  40.  
  41. class CarAdmin(admin.ModelAdmin):
  42.     pass
  43.  
  44. admin.site.register(Car,CarAdmin)
  45.  
  46. class ManifacturerAdmin(admin.ModelAdmin):
  47.     def save_model(self, request, obj, form, change):
  48.         obj.user = request.user
  49.         super().save_model(request, obj, form, change)
  50.  
  51. admin.site.register(Manufacturer,ManifacturerAdmin)
  52.  
  53. ----------------------------settings.py------------------------------------
  54. import os
  55. from pathlib import Path
  56.  
  57. INSTALLED_APPS = [
  58.     'django.contrib.admin',
  59.     'django.contrib.auth',
  60.     'django.contrib.contenttypes',
  61.     'django.contrib.sessions',
  62.     'django.contrib.messages',
  63.     'django.contrib.staticfiles',
  64.     'CarsApp'
  65. ]
  66.  
  67. STATIC_URL = 'static/'
  68. MEDIA_ROOT = os.path.join(BASE_DIR,'media/')
  69. MEDIA_URL = '/media/'
  70.  
  71. ----------------------------urls.py------------------------------------
  72. from django.contrib import admin
  73. from django.urls import path
  74. from django.conf.urls.static import static
  75. from django.conf import settings
  76. from CarsApp.views import index, newProduct, details, editProduct, deleteProduct
  77. urlpatterns = [
  78.     path('admin/', admin.site.urls),
  79.     path('index/', index, name='index.html',),
  80.     path('newProduct/', newProduct, name='newProduct.html'),
  81.     path('details/<int:pk>/',details, name='details.html' ),
  82.     path('editProduct/<int:pk>/', editProduct, name='editProduct.html'),
  83.     path('deleteProduct/<int:pk>/', deleteProduct, name='deleteProduct.html'),
  84. ] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
  85.  
  86. ----------------------------views.py------------------------------------
  87. from django.shortcuts import render, redirect
  88. from .models import Car
  89. from .forms import CarForm
  90. from django.shortcuts import get_object_or_404
  91. # Create your views here.
  92. def index(request):
  93.     # if request.method=="POST":
  94.     #     form = CarForm(request.POST, request.FILES)
  95.     #     if form.is_valid():
  96.     #         car = form.save(commit=False)
  97.     #         car.image = form.cleaned_data['image']
  98.     #         car.save()
  99.     #         return redirect("index.html")
  100.     # else:
  101.         queryset = Car.objects.all()
  102.         context = {'cars': queryset,}
  103. # 'form': CarForm,
  104.         return render(request,'index.html', context=context)
  105.  
  106. def newProduct(request):
  107.     if request.method=="POST":
  108.         form = CarForm(request.POST, request.FILES)
  109.         if form.is_valid():
  110.             car = form.save(commit=False)
  111.             car.image = form.cleaned_data['image']
  112.             car.save()
  113.             return redirect("index.html")
  114.     else:
  115.         # queryset = Car.objects.all()
  116.         context = { 'form': CarForm, }
  117.         return render(request,'newProduct.html',context= context)
  118.  
  119. def details(request, pk):
  120.     car = get_object_or_404(Car,pk=pk)
  121.     return render(request,'details.html',{'car':car})
  122.  
  123. def editProduct(request, pk):
  124.     car = get_object_or_404(Car, pk=pk)
  125.     if request.method=="POST":
  126.         form = CarForm(request.POST, request.FILES, instance=car)
  127.         if form.is_valid():
  128.             form.save()
  129.             return redirect("index.html")
  130.     else:
  131.         form = CarForm(instance=car)
  132.         return render(request,'editProduct.html', {'form': form})
  133.  
  134. def deleteProduct(request, pk):
  135.     car = get_object_or_404(Car, pk=pk)
  136.     if request.method=="POST":
  137.         car.delete()
  138.         return redirect("index.html")
  139.     else:
  140.         return render(request,'deleteProduct.html',{'car': car})
  141.    
  142. ----------------------------forms.py------------------------------------
  143. from django import forms
  144. from .models import Car
  145.  
  146. class CarForm(forms.ModelForm):
  147.     def __init__(self, *args, **kwargs):
  148.         super(CarForm, self).__init__(*args, **kwargs)
  149.         for field in self.visible_fields():
  150.             field.field.widget.attrs['class'] = "form-control"
  151.     class Meta:
  152.         model = Car
  153.         exclude = ['user',]
  154.        
  155. ----------------------------templates------------------------------------
  156. #import the bootstrap framework in each .html file's head tag:
  157. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
  158.     <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
  159.  
  160.  
  161. ----------------------------index.html------------------------------------
  162. <body>
  163. <nav class="navbar bg-primary" data-bs-theme="dark">
  164.   <div class="container-fluid">
  165.     <a  class="navbar-brand " href="#" style="border: black">Car Shop</a>
  166.       <a class="navbar-brand "  href="#">Home</a>
  167.       <a class="navbar-brand "  href="{% url 'newProduct.html' %}" style="border: black">Add New Product</a>
  168.   </div>
  169. </nav>
  170. <h1 style="text-align: center;">ALL CARS</h1>
  171. <div class="container" style="padding-top: 40px; background-color: whitesmoke; box-shadow: black;">
  172.     <div class="row">
  173.         {% for car in cars %}
  174.          <div class="col-md-3">
  175.              <div class="card" style="width: 18rem;">
  176.   <img src= "{{ MEDIA_URL }} {{ car.image.url }}" alt="...">
  177.   <div class="card-body">
  178.       <h4 class="card-title"><b> {{car.manifacturer}} {{car.model}}</b></h4>
  179.     <h5 class="card-title">{{car.manifacturer}}</h5>
  180.     <h5 class="card-title">{{car.distance_travelled}} km</h5>
  181.       <h5 class="card-title">{{car.price}} $</h5>
  182.     <button><a href="{% url 'details.html' car.pk %}" class="btn btn">Details</a></button>
  183.       <button><a href="{% url 'editProduct.html' car.pk %}" class="btn btn-primary">Edit</a></button>
  184.       <button><a href="{% url 'deleteProduct.html' car.pk %}" class="btn btn-danger" ">Delete</a></button>
  185.  </div>
  186. </div>
  187.         </div>
  188.  
  189.        {% endfor %}
  190.    </div>
  191. </div>
  192. </body>
  193.  
  194. ----------------------------newProduct.html------------------------------------
  195. <body>
  196. <nav class="navbar bg-primary" data-bs-theme="dark">
  197.  <div class="container-fluid">
  198.    <a class="navbar-brand "  href="#">Car Shop</a>
  199.       <a class="navbar-brand"  href={% url 'index.html' %}>Home</a>
  200.       <a class="navbar-brand"  href="#">Add New Product</a>
  201.   </div>
  202. </nav>
  203.  
  204.  
  205. <div class="container" style="padding-top: 40px; background-color: whitesmoke; box-shadow: black;">
  206.     <form method="post" enctype="multipart/form-data">
  207.         {% csrf_token %}
  208.         {{ form.as_p }}
  209.         <button class="btn btn-primary" type="submit">Submit</button>
  210.     </form>
  211. </div>
  212. </body>
  213.  
  214. ----------------------------details.html------------------------------------
  215. <body>
  216. <nav class="navbar bg-primary" data-bs-theme="dark">
  217.   <div class="container-fluid">
  218.     <a class="navbar-brand "  href="#">Car Shop</a>
  219.       <a class="navbar-brand"  href={% url 'index.html' %}>Home</a>
  220.       <a class="navbar-brand"  href="{% url 'newProduct.html' %}">Add New Product</a>
  221.   </div>
  222. </nav>
  223. <h1 style="text-align: center;">Details for car {{ car.pk }}</h1>
  224. <div class="container" style="padding-top: 40px; background-color: whitesmoke; box-shadow: black;">
  225.     <div class="row">
  226.             <div class="col-md-6">
  227.                 <div style="width: 18rem;">
  228.                     <img src= "{{ MEDIA_URL }} {{ car.image.url }}" alt="..." style="width: 600px; height: 450px">
  229.                 </div>
  230.             </div>
  231.    
  232.    
  233.     <div class="col-md-6">
  234.         <div  style="width: 18rem;">
  235.             <div>
  236.                 <h4><b> {{car.manifacturer}} {{car.model}}</b></h4>
  237.                 <h5>Manifacturor: {{car.manifacturer}}</h5>
  238.                 <h5>Chassis Number: {{car.chassis_number}}</h5>
  239.                 <h5>Color: {{car.price}}</h5>
  240.                 <h5>Year of birth: {{car.price}}</h5>
  241.                 <h5>Distance travelled: {{car.price}} km</h5>
  242.                 <h5>Type: {{car.get_type_display}}</h5>
  243.                 <h5>Price: {{car.price}} $</h5>
  244.                 <a href="{% url 'index.html' %}" class="btn btn-success" >Back</a>
  245.             </div>
  246.         </div>
  247.     </div>
  248.         </div>
  249. </div>
  250. </body>
  251.  
  252. ----------------------------editProduct.html------------------------------------
  253. <body>
  254. <h2 style="text-align: center">Edit the Car {{ car.pk }}</h2>
  255. <div class="container" style="padding-top: 40px; background-color: whitesmoke; box-shadow: black;">
  256.     <form method="post" enctype="multipart/form-data">
  257.         {% csrf_token %}
  258.         {{ form.as_p }}
  259.         <button class="btn btn-primary" type="submit">Submit</button>
  260.     </form>
  261. </div>
  262. </body>
  263.  
  264. ----------------------------deleteProduct.html------------------------------------
  265. <body>
  266. <h2 style="text-align: center">Are you sure you want to delete this car?</h2>
  267. <div class="container" style="padding-top: 40px; background-color: whitesmoke; box-shadow: black;">
  268.     <form method="post">
  269.         {% csrf_token %}
  270.         <button style="horiz-align: center" class="btn btn-danger" type="submit">Submit</button>
  271.         <a href="{% url 'index.html' %}" class="btn btn" >Cancel</a>
  272.     </form>
  273. </div>
  274. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement