Reputation: 1694
I am trying to access the object used in Django DetailView and used to display Model data.
I can access the object in the html template, by {{ object.product_name }}
.
But, how do I access the object in a function, like below? (within views.py).
def access_object_values(self):
name = object.product_name
(my attempts fail and I get: WSGIRequest' object has no attribute)
. .
My setup:
*models.py*
class Product(models.Model):
product_name = models.Charfield(max_length=20)
product_price = models.FloatField(max_length=12, default=100)
*urls.py*
path('product/<int:pk>/', ProductView.as_view(), name='product-detail)
My attempts:
class ProductView(DetailView):
model = Product
template_name = 'product/product_detail.html'
def get_object(self, queryset=None):
if queryset is None:
queryset = self.get_queryset()
obj = super(ProductView, self).get_object(queryset=queryset)
return obj
def get(self, obj, *args, **kwargs):
return self.generate_pdf(obj)
# TO VISUALLY GET FEEDBACK OF SUCCESS OR NOT
# ==========================================
def generate_pdf(self, obj):
from reportlab.pdfgen import canvas
response = HttpResponse(content_type='application/pdf')
response['pdf'] = 'attachment; filename="summary.pdf"'
p = canvas.Canvas(response)
# name = getattr(obj, 'product_name') # produces 'WSGIRequest' object has no attribute 'product_name'
# name = obj.product_name # produces 'WSGIRequest' object has no attribute 'product_name'
# name = type(obj) # produces 'WSGIRequest' has no attribute 'decode
name = "text" # this works, and it prints
p.drawString(100, 100, name)
p.showPage()
p.save()
print(p)
return response
Upvotes: 0
Views: 1863
Reputation: 1694
A useful answer, if one wants to access context and use it as variables: Accessing global variable in view using context processor in Django
Add your context processor method path (folder.context_processor.application_context) to TEMPLATE_CONTEXT_PROCESSORS.
in my case application_context is the method which i defined inside file context_processor.py and method "application_context" returns {'titles': 'mytitle'}
if you want to use "title" as global variable in views use it in this way
global_var = RequestContext(request).get("app_config") titles = global_var.get("titles") print titles
Another useful answer is here: Django: How to provide context to all views (not templates)?
Upvotes: 0
Reputation: 582
def get_object(self, queryset=None):
if queryset is None:
queryset = self.get_queryset()
obj = super(ProductView, self).get_object(queryset=queryset)
self.generate_pdf(obj)
return obj
def generate_pdf(self, obj):
from reportlab.pdfgen import canvas
p = canvas.Canvas(response)
name = obj.product_name
p.drawString(100, 100, name )
p.showPage()
p.save()
print(p)
This could be what you are looking for
Upvotes: 1
Reputation: 5669
A method get_object
should return an object.
Try:
def get_object(self, queryset=None):
obj = super(ProductView, self).get_object(queryset=queryset)
self.generate_pdf(obj)
return obj
def generate_pdf(self, obj):
from reportlab.pdfgen import canvas
p = canvas.Canvas(response)
name = obj.product_name
p.drawString(100, 100, name )
p.showPage()
p.save()
print(p)
If you want just to print it, no need for response here.
Upvotes: 1