0

How can I get my rendered html text in django? I need it because I want to use the html text in another template variable. My site has a main template and it has two part one is content and other is a column for links and ... . In content I want to put my rendered html such as sign in forms, new posts and ... . How can I do that? A part of my code is here:

def loginfrm(request):
    return render(request,'main.html',
                         {'content':renderdhtmltext()),
                          'colum':'links',}

My exact purpose is to define a function like renderdhtmltext(). And is there any django built in for that?

2 Answers 2

1

The right way to deal with site-wide content is to write custom templatetags so views don't have to deal with it. You views should only deal with view-specific stuff.

Sign up to request clarification or add additional context in comments.

4 Comments

Can you explain a little more about how to use template custom tags in showing content. I'm an amateur in django and I don't know how to that.
A templatetag is just a bit of Python code, and it can either add variables (whatever Python object) to the template's context or return text or html to be inserted in the template. You'll find more in the FineManual(tm): docs.djangoproject.com/en/1.6/howto/custom-template-tags/…. Don't be afraid by the apparent complexity and read further, there are shortcuts for the two most common cases (inclusion tags and assignment tags).
Custom tags are not needed, template inherit helps.
@user995394 : template inheritance wont let you access anything that's not in the context. Templatetags will.
0

render returns an instance of HttpResponse, you can access the html content via response.content.

response = another_view(request)
rendered_html_text = response.content

Doc of HttpResponse

Updated: It's better to define your base.html and extend it, put what in common to base.

7 Comments

I do not think response.content is not matching what he asks for.
You answer worked for me but is there any better way? I think it's a little bad way.
Define your base.html and extend it, put what in common to base.
But tell me how can I put my content in a specified place in base html?
for example: your base.html is "<form>...</form>{% block content %}{% endblock }", in your main.html {% extends "base.html" %}{% block content %}blahblah{% endblock %}, then you reused form in your template.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.