webpy template multiple inheritance, possible? - templates

I want to divide index page into small stand alone .html parts like:
up_bar.html:
<p><center>
<h1>home</h1>
Menu: home add import
down_bar.html:
<a href="/path/.."/>
and so on.
Now, to build a new page is it possible to embed those pieces into other page using default webpy templator?
Maybe something like that?:
in admin.html:
$def with(some_parameters):
<title>Admin panel</title>
$include('side_bar.html')
... body stuff ...
$include('down_bar.html')

A basic but good introduction to template inheritance can be found here: http://webpy.org/cookbook/layout_template

Found an answer here: http://groups.google.com/group/webpy/msg/ea6da02dfb9eedc4?dmode=source
Some explanation will be great.

I did this to my code
def GET(self,*args):
param= {'name':'jackie'}
view = web.template.frender("views/someview.html")
content = view(**param)
layout = web.template.frender("views/index.html")
return layout(content=content)
now you just insert $:content in index.html

Related

Django Comments Xtd Comments are not nesting

I followed the tutorial on https://django-comments-xtd.readthedocs.io, everything seems to work find, the forms and the comments are working, but on the website they are not displayed properly.
instead of looking like this:
They all look like this
The reply is not nesting under the comment
but in the admin its working you can see how it nests
take a look:
Please help
Try add css class container in the comment_tree.html
<div class="container media">

Django: a custom template tag to convert links inside of a TextField and change the hyperlink text

The scenario is that there are some dynamic texts on some templates, that will contain hyperlinks.
For this, I have a SiteDataKeyValue model, in which the dynamic texts for different parts of the template are inputted. This is the model:
class SiteDataKeyValue(models.Model):
key = models.CharField(
max_length=200, verbose_name="نام متن مورد نظر", unique=True
)
value = models.TextField(verbose_name="متن")
def __str__(self):
return self.key
A solution that I've found already, is Django urlize template tag. As mentioned in the docs, this tag converts texts like https://www.google.com to www.google.com, which is nice but not what I'd like to achieve. I want to be able to change the hyperlink text, so the output would be something like: Click Here!.
I searched for a bit, came across modules like bleach, which is a fine module, but I couldn't find the answer I was looking for (I skimmed through the docs and there was nothing about the hyperlink text).
Also I saw a comment somewhere telling that this could be achieved by writing a custom Django template tag, but although I tried to do this regarding the custom template filters docs, I didn't have a clue to how to achieve this.
I'm not asking for the code, although it would be really appreciated if you provide instructions for writing this custom template tag, or better, if you could point me to something like this that is already out there.
First of all you can extend urlize tag like the answer in this
or you can change the main code which you can find it in django.utils.html and override its url variable to change it.
But I think the best method is extending the urlize tag
like this:
{% text | urlize | change_a_text_filter:{{ dome_new_a_text }} %}
then you can scrape the text and use regex to find >sample-text</a> then you can change it to the argument that defines in your tag
from django import template
register = template.Library()
#register.simple_tag
def change_a_text_filter(format_string, arg):
# find the url that made in urlize with regex
# change it with arg
# return the result
I was on a completely wrong road to solve this problem. I was trying to urlize a link from TextField, but didn't consider the fact that I only needed to implement html code as Visit link.com! in the TextField, and then use safe template tag to render html directly as below:
{{ text.value|safe }}
So in this solution, there is no need to urlize, and of course there is no need to extend this tag neither.
NOTE: As commented by #rahimz (link to comment) I understand that there are safety concerns regarding safe tag, So I should emphasize that only me and a company-trusted admin will have access to admin panel and there is no worries that this admin will send malicious code through this TextField.

Add list view to Fluid template

I've built a custom extension which displays comments for a page.
But, of course, I want to have the comments section on every page. Is there a way to add it into the Fluid template so that I won't have to add it to every single page?
Looking for something like this:
<f:blabla.bla extension="tx_comment" action="list"/>
You don't need to create your own viewhelper for this. You can use the VHS extension and use render.request
<v:render.request action="[string|NULL]" controller="[string|NULL]" extensionName="[string|NULL]" pluginName="[string|NULL]" vendorName="[string|NULL]" arguments="{foo: 'bar'}" onError="NULL" graceful="1">
<!-- tag content - may be ignored! -->
</v:render.request>
https://fluidtypo3.org/viewhelpers/vhs/master/Render/RequestViewHelper.html

Putting links in list_detail.object_list to list_detail.object_detail

I've started using Django and am going right to generic views. Great architecture! Well, the documents are great, but for the absolute beginner it is a bit like unix docs, where they make the most sense when you already know what you're doing. I've looked about and cannot find this specifically, which is, how do you set up an object_list template so that you can click on an entry in the rendered screen and get the object_detail?
The following is working. The reason I'm asking is to see if I am taking a reasonable route or is there some better, more Djangoish way to do this?
I've got a model which has a unicode defined so that I can identify my database entries in a human readable form. I want to click on a link in the object_list generated page to get to the object_detail page. I understand that a good way to do this is to create a system where the url for the detail looks like http://www.example.com/xxx/5/ which would call up the detail page for row 5 in the database. So, I just came up with the following, and my question is am I on the right track?
I made a template page for the list view that contains the following:
<ul>
{% for aninpatient in object_list %}
<li><a href='/inpatient-detail/{{ aninpatient.id }}/'>{{ aninpatient }}</a></li>
{% endfor %}
</ul>
Here, object_list comes from the list_detail.object_list generic view. The for loop steps through the object list object_list. In each line I create an anchor in html that references the desired href, "/inpatient-detail/nn/", where nn is the id field of each of the rows in the database table. The displayed link is the unicode string which is therefore a clickable link. I've set up templates and this works just fine.
So, am I going in the right direction? It looks like it will be straightforward to extend this to be able to put edit and delete links in the template as well.
Is there a generic view that takes advantage of the model to create the detail page? I used ModelForm helper from django.forms to make the form object, which was great for creating the input form (with automatic validation! wow that was cool!), so is there something like that for creating the detail view page?
Steve
If you're on django < 1.3 then what you are doing is basically perfect. Those generic views are quite good for quickly creating pages. If you're on django 1.3 you'll want to use the class based generic views. Once you get a handle on those they are are crazy good.
Only note I have is that you should use {% url %} tags in your templates instead of hardcoding urls. In your urls.conf file(s) define named urls like:
url('inpatient-detail/(?P<inpatient_id>\d+)/$', 'your_view', name='inpatient_detail')
and in your template (for django < 1.3):
...
In 1.3 a new url tag is available that improves life even more.

All of the disqus comments are showing up on all the blog pages

I've built a blog type app. in django. And I'm trying to integrate DISQUS for comments using montylounge's django-disqus. But comments from all the blog posts are showing up on every blog page. What could be the problem ?
I'm not sure how the django module integrates, but from a Disqus point of view it sounds like you're not setting a unique value for the "disqus_identifier" variable on each page.
You need to have the variable instantiated like this:
var disqus_identifier = 'blogpost_<BLOG POST ID HERE>';
before you include the disqus javascript files.
You can try this. In my case, it solved my problem similar to yours. Cheers!
var disqus_url = 'yoursite_dot_com/path_to/id-of-blog-post';
var disqus_identifier = '/id-of-blog-post/';
I think you should view page source code.
Check disqus_url - is it an absolute URL?
In django-disqus often written like this: {% set_disqus_url object.get_absolute_url %}
but object.get_absolute_url is your settings.