Home > Enterprise >  How do I create link for details in each post
How do I create link for details in each post

Time:11-28

I am doing a project in which I display the listing of all the major dealerships in your region but also allows the user to sell their car independently. I am working on the user selling it independently part and I am stuck in one thing. As of right now, the user can create a post and it will display it in the website, along the post there is also a "View" button in which shows the detail of the car such as mileage, contact, date published etc. The problem is that I don't know how to make each post to have its own view page. How do I make each post have its own detail page?

That's my code as of right now:

views.py

from django.shortcuts import render
from .models import *
from django.http import HttpResponse

def home(request):
    context = {}
    return render(request, 'store/home.html', context)
def store(request):
    context = {}
    return render(request, 'store/store.html', context)

def cart(request):
    context = {}
    return render(request, 'store/cart.html', context)  

def checkout(request):
    context = {}
    return render(request, 'store/checkout.html', context) 

def posts(request):
    cars = Userpost.objects.all()
    context = {'cars':cars}
    return render(request, 'store/userposts.html', context)

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name = "home"),
    path('posts/', views.posts, name = "posts"),
    path('store/', views.store, name = "store"),
]

models.py

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone

class Customer(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank = True)
    name = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200, null= True)

    def __str__(self):
        return self.name

class Userpost(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    Year = models.CharField(max_length = 4)
    Mileage = models.CharField(max_length = 8)
    Make = models.CharField(max_length = 50)
    Model = models.CharField(max_length = 50)
    Price = models.DecimalField(max_digits=15, decimal_places=2)
    email = models.EmailField()
    date_published = models.DateField(default = timezone.now)
    image = models.ImageField(null = True, blank = True)
    

    def __str__(self):
        return self.Year   " "   self.Make   " "   self.Model
    
    @property
    def imageURL(self):
        try:
            url = self.image.url
        except:
            url = ''
        return url

Template

{% extends 'store/main.html' %}
{% load static %}

{% block content %}

<div class="row">
    {% for car in cars %}
    <div class="col-lg-4">
        <img class="thumbnail" src="{{car.imageURL}}">
        <div class="box-element product">
            <h6><strong>{{car.Year}} {{car.Make}} {{car.Model}}</strong></h6>
            <hr>

            <button  class="btn btn-outline-secondary add-btn">Add to Cart</button>
            <a class="btn btn-outline-success" href="#">View</a>
            <h4 style="display: inline-block; float: right"><strong>${{car.Price|floatformat:2}}</strong></h4>

        </div>
    </div>
    {% endfor %}
</div>
{% endblock content %}

I'm just learning Django so I'm often stuck and lost. I would appreciate any help.

CodePudding user response:

add this to your views.py

from django.shortcuts import render,get_object_or_404,redirect
def post_detail(request,post_id):
    cars = get_object_or_404(Userpost,pk=post_id)
    context = {'car':car}
    return render(request, 'store/userpost_detail.html', context)

userpost_detail.html

{% extends 'store/main.html' %}
{% load static %}

{% block content %}

<div class="row">

    <div class="col-lg-4">
        <img class="thumbnail" src="{{car.imageURL}}">
        <div class="box-element product">
            <h6><strong>{{car.Year}} {{car.Make}} {{car.Model}}</strong></h6>
            <hr>

            <button  class="btn btn-outline-secondary add-btn">Add to Cart</button>
           
            <h4 style="display: inline-block; float: right"><strong>${{car.Price|floatformat:2}}</strong></h4>

        </div>
    </div>

</div>
{% endblock content %}

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name = "home"),
    path('posts/', views.posts, name = "posts"),
    path('store/', views.store, name = "store"),
    path('post/<int:post_id>/', views.post_detail, name = "post_detail"),#new
]

now inside your userposts.html if you want to get a page detail you can do something like this

 <a class="btn btn-outline-success" href="{% url 'post_detail' car.pk %}">View</a>
  • Related