我正在做一個專案,我在其中顯示您所在地區所有主要經銷商的串列,但也允許用戶獨立銷售他們的汽車。我正在處理獨立銷售它的用戶部分,但我被困在一件事上。目前,用戶可以創建一個帖子并將其顯示在網站上,帖子旁邊還有一個“查看”按鈕,其中顯示了汽車的詳細資訊,例如里程、聯系方式、發布日期等。問題是我不知道如何讓每個帖子都有自己的查看頁面。如何讓每個帖子都有自己的詳細資訊頁面?
這是我現在的代碼:
視圖.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)
網址.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"),
]
模型.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
模板
{% 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 %}
我只是在學習 Django,所以我經常被卡住和迷路。我將不勝感激任何幫助。
uj5u.com熱心網友回復:
將此添加到您的 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 %}
網址.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
]
現在在您的 userposts.html 中,如果您想獲取頁面詳細資訊,您可以執行以下操作
<a class="btn btn-outline-success" href="{% url 'post_detail' car.pk %}">View</a>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/368332.html
標籤:姜戈
上一篇:使用Reportlab加密pdf
