Reputation: 33
In the below code I have added a save as pdf button in admin page by customizing it. While I'm clicking the save as pdf button it should display the details of the current page in pdf format. I'm not able to retrieve the separate data of the current page to display it in the pdf form (my_template.html
). How to do it? Thanks in advance.
views.py
:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# Create your views here.
from django.http import HttpResponse
from django.views.generic import View
from django.db import models
from details.utils import render_to_pdf #created in step 4
from .models import UserDetails
class GeneratePdf(View):
def get(self, request, *args, **kwargs):
#THIS IS THE PLACE WHERE I GET STUCK
all = {
"Name": "obj.name",
"Email": "obj.email",
"Address": "obj.address",
"DOB": "obj.dob",
"Gender": "obj.gender",
}
pdf = render_to_pdf('my_template.html', all)
return HttpResponse(pdf, content_type='application/pdf')
urls.py
:
"""myapp URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from details.views import GeneratePdf
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^pdf/$', GeneratePdf.as_view(),name='generatepdf'),
]
Upvotes: 1
Views: 3329
Reputation: 33
Anton Shurashov's answer is correct. In addition to that, in the template while specifying the URL in the required tag, you should know the ID of the data to access it. It will work only when the data is saved in the database.
{%url 'generatepdf' adminform.form.instance.id %}
"""adminform.form.instance.id"""
is the general instance indicating the ID of the current page data.
The button should be visible only after saving the data.
Upvotes: 1
Reputation: 1930
urls.py
:
from details.views import GeneratePdf
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^pdf/(?P<user_id>[0-9]+)/$', GeneratePdf.as_view(), name='generatepdf'),
]
views.py
:
from django.http import HttpResponse
from django.views.generic import View
from django.db import models
from details.utils import render_to_pdf
from .models import UserDetails
class GeneratePdf(View):
def get(self, request, *args, **kwargs):
obj = UserDetails.objects.get(id=kwargs.get('user_id'))
all = {
"Name": obj.name,
"Email": obj.email,
"Address": obj.address,
"DOB": obj.dob,
"Gender": obj.gender,
}
pdf = render_to_pdf('my_template.html', all)
return HttpResponse(pdf, content_type='application/pdf')
Upvotes: 1