django - 如何从 django 中基于类的详细视图中获取当前对象/产品?
问题描述
'''Models Code'''
# Product Model
class Products(models.Model):
name = models.CharField(max_length=50)
img = models.ImageField(upload_to='productImage')
CATEGORY = (
('Snacks','Snacks'),
('Juice','Juice'),
)
category = models.CharField(max_length=50, choices=CATEGORY)
description = models.TextField()
price = models.FloatField()
# Rating Model
class Rating(models.Model):
product = models.ForeignKey(Products, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
stars = models.IntegerField(validators=[MinValueValidator(1),MaxValueValidator(5)], blank=True, null=True)
comment = models.TextField(blank=True,null=True)
''' Views Code '''
class ProductListView(ListView):
model = Products
template_name = 'products.html'
context_object_name ='Products'
class ProductDetailView(LoginRequiredMixin,DetailView):
login_url = '/accounts/login'
model = Products
# Using this function I want to take the rating and comment, but how can I access the cuurent object for which the comment and rating is being send by the user.
def review(request,slug):
star=request.POST.get('rating')
comment=request.POST.get('comment')
user = request.user
productId = request.POST.get('productsid') # How to get the Product
product = Products.objects.get(id=productId)
review = Rating(product=product,user=user,stars=star,comment=comment)
review.save()
return redirect('/')
# Urls code
urlpatterns = [
path('',views.home,name='Home'),
path('products',ProductListView.as_view(),name='Products'),
path('product/<int:pk>',ProductDetailView.as_view(),name='Product-Details'),
path('contact',views.contact,name='Contact'),
path('review',views.review,name='review')
#Templates Code
<form method="POST" action="review">
{% csrf_token %}
<input type="hidden" id="rating-value" name="rating">
<textarea style="margin-top:5px;" class="form-control" rows="3" id="comment" placeholder="Enter your review" name="comment"></textarea>
<button type="submit" style="margin-top:10px;margin-left:5px;" class="btn btn-lg btn-success">Submit</button>
</form>
如何从评论功能的详细视图页面中获取当前对象?我在这里添加了代码。在产品详细视图页面中,它正在呈现我想要对产品进行评级和评论的页面。有没有其他方法可以获取 product、user、star 和 rating 字段值并将其存储在数据库中?
解决方案
我可以指出一些方法来检索product_id
你的review
函数。
第一种方法:
您可以将其product_id
作为 URL 参数传递。在这种情况下,我希望review
从产品详细信息页面调用视图。
所以,你的网址应该是这样的:
path('review/<int:product_id>', views.review, name="review),
您的看法:
def review(request, *args, **kwargs):
star=request.POST.get('rating')
comment=request.POST.get('comment')
user = request.user
productId = kwargs.get('product_id') # change is here
product = Products.objects.get(id=productId)
review = Rating(product=product,user=user,stars=star,comment=comment)
review.save()
return redirect('/')
您的模板:
<form method="POST" action="{% url 'review' object.pk %}">
{% csrf_token %}
<input type="hidden" id="rating-value" name="rating">
<textarea style="margin-top:5px;" class="form-control" rows="3" id="comment" placeholder="Enter your review" name="comment"></textarea>
<button type="submit" style="margin-top:10px;margin-left:5px;" class="btn btn-lg btn-success">Submit</button>
</form>
在模板中,object
是您赋予产品对象的 object_name。您可以通过添加以下内容来更改对象名称:
context_object_name = product
在你的ProductDetailView
.
第二种方法:
将 product_id 作为表单数据传递。您可以在模板中创建一个隐藏的输入,该输入将包含 product_id 作为值。例如:
在您的模板中:
<form method="POST" action="review">
{% csrf_token %}
<input type="hidden" id="rating-value" name="rating">
<input type="hidden" name="product_id" value="{{ object.pk }}"> # add a hidden input field
<textarea style="margin-top:5px;" class="form-control" rows="3" id="comment" placeholder="Enter your review" name="comment"></textarea>
<button type="submit" style="margin-top:10px;margin-left:5px;" class="btn btn-lg btn-success">Submit</button>
</form>
object
我之前提到的在哪里。
然后您可以在视图中检索 product_id:
def review(request,slug):
star=request.POST.get('rating')
comment=request.POST.get('comment')
user = request.user
productId = int(request.POST.get('product_id')) # here
product = Products.objects.get(id=productId)
review = Rating(product=product,user=user,stars=star,comment=comment)
review.save()
return redirect('/')
推荐阅读
- c# - 使用异步扩展方法时订阅 observable 未触发
- angular - 用户执行操作或空闲状态时自动注销 - Angular
- c++ - CLion 中的 Boost 安装
- reactjs - 如何在颜色选择器中使用所选颜色制作 div
- spring-boot - 带有 Spring-boot 后端的 Flutter websocket
- java - 我向wordpress rest api发送请求以获取令牌,但我得到了奇怪的回调
- c# - 更改硒中的书签
- xml - 在 XSL 中创建可点击的外部链接以创建 PDF
- c++ - 变量引用以及指针如何与内存交互
- javascript - 嵌套 if 语句 - 如何在迭代时重构条件以使用一个 if 语句