These are chat archives for django/django

19th
Dec 2017
:]
@elcolie
Dec 19 2017 02:20
@MariosPanag What do you mean? Are you still having a question?
emihir0
@emihir0
Dec 19 2017 09:06
how would I go about making a 'live' template - for example contract type of thing so that we can edit the contract live (not as html), so it would be stored in textfield (I assume?) and such that it can still behave as a template so that it can use things like {{ contractee.name }}
e.g. contract between employer and employee. the contract itself can be quite generic, but it can be adjusted (ie. its default) for each specific employee. it could contain stuff like {{ employee.name }} and {{ employee.birth_date }}. then a pdf version could be rendered easily
i'm sure this is possible as it seems like quite a common business logic need; I'm just not sure how to go about this. any guide/suggestion?
Luke-VideoLibre
@LukeVideo
Dec 19 2017 16:49
Well you should have a contract model that has foreignKey's that are related to your employee or contract model and the you might even have some different attibuts according to your different scenarios. So you then have to build a template that builds the contract. Generating the PDF should'nt be to hard depending on rather you want the browser or the server to handle it.
@emihir0
Marios Panagopoulos
@MariosPanag
Dec 19 2017 18:32
@elcolie i found it :)
emihir0
@emihir0
Dec 19 2017 20:40
@LukeVideo the problem I'm not sure how to do is being able to edit this contract (ie. the template itself) such that it can be generic enough (contains stuff like {{ contractee.name }}) and be able to render it with 'contractee' instance (so that it displays the actual name)
how is this usually done?
usually you make a html template that gets rendered with object supplying it context. but i want to be able to edit the template itself (contract) (without having to change html file or such, hence it should be textfield i guess?)
Luke-VideoLibre
@LukeVideo
Dec 19 2017 20:43
You create a URL for that contract based on pk or name... And then you call the template you want.
emihir0
@emihir0
Dec 19 2017 20:43
but the point is that the template IS the textfield, it's not some static html file
Luke-VideoLibre
@LukeVideo
Dec 19 2017 20:44
Or do you want to populate a textarea
emihir0
@emihir0
Dec 19 2017 20:44
so I cannot call render(request, 'contract.html', { 'contractee': some_instance })
I want 'contract.html' to be contract_instance.textfield
Luke-VideoLibre
@LukeVideo
Dec 19 2017 20:45
I just managed to do that with a view filling my form for me before I post
emihir0
@emihir0
Dec 19 2017 20:45
by 'I cannot call' I meant that it doesn't accomplish what I want
If I do it that way then I cannot change the contract in real time through some WYSIWYG editor
I need to be able to change the file dynamically, save it, and then load it with specific instance of 'contractee'
Luke-VideoLibre
@LukeVideo
Dec 19 2017 20:51
Oh. Can you be more precise on what can change in the contract? What would two different contract look like?
Is it like you have multiple employees. You generate contract V1 and get 10 pdf / contract and each can sign but later you want to modify some details of contract V1 so you just have to edit some textfield and boom! Contract V2 generated for your 10 employees?
I guess you need a form to submit the modified contract.
emihir0
@emihir0
Dec 19 2017 20:57
please ignore pdf, forms, views and all the fluff around, I know how to do those parts. I don't really need a step by step guide on how to make a view. I just need to know how can I make 'render' function think that the template I supply to it IS the final template, not a name of the template
so instead of render(request, 'contract.html', some_context) I need to do something like render(request, contract_instance.template, some_context) where contract_instance.template - the template is a textfield such as hello {{ contractee.name }}
If I were to render it in this native manner, it would try to lookup hello {{ contractee.name }} as if it were a html file and it would return TemplateDoesNotExist
emihir0
@emihir0
Dec 19 2017 21:13
ok, got it working, it's relatively simple in the end
def render_contract(request, contractee_pk):
    contractee = get_object_or_404(Contractee, pk=contractee_pk)
    contract = Contract.objects.first()

    template = Template(contract.template)
    context = Context({'contractee': contractee})
    content = template.render(context)

    return HttpResponse(content, None, None)
(of course that's just a simple demo to see if it can work, I would still appreciate input from people who have experience with this area on how do they render such cases)